Skip to content

fix(agents): use shallow copy in BaseGenerator.with_params()#2319

Merged
kevinmessiaen merged 2 commits intomainfrom
fix/with-params-deepcopy-crash
Mar 20, 2026
Merged

fix(agents): use shallow copy in BaseGenerator.with_params()#2319
kevinmessiaen merged 2 commits intomainfrom
fix/with-params-deepcopy-crash

Conversation

@Hartorn
Copy link
Copy Markdown
Member

@Hartorn Hartorn commented Mar 20, 2026

model_copy(deep=True) crashes with TypeError: cannot pickle '_asyncio.Future' when the generator has a rate limiter with max_concurrent set, because asyncio.Semaphore cannot be deep-copied.

Switch to a shallow copy (sufficient since params is replaced, not mutated) and add a defensive __deepcopy__ on BaseRateLimiter to prevent future deep-copy attempts from crashing.

Closes ENG-1474

Made-with: Cursor

Description

Related Issue

Type of Change

  • 📚 Examples / docs / tutorials / dependencies update
  • 🔧 Bug fix (non-breaking change which fixes an issue)
  • 🥂 Improvement (non-breaking change which improves an existing feature)
  • 🚀 New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to change)
  • 🔐 Security fix

Checklist

  • I've read the CODE_OF_CONDUCT.md document.
  • I've read the CONTRIBUTING.md guide.
  • I've written tests for all new methods and classes that I created.
  • I've written the docstring in Google format for all the methods and classes that I used.
  • I've updated the pdm.lock running pdm update-lock (only applicable when pyproject.toml has been
    modified)

@Hartorn Hartorn requested a review from kevinmessiaen March 20, 2026 09:28
@Hartorn Hartorn self-assigned this Mar 20, 2026
@linear
Copy link
Copy Markdown

linear bot commented Mar 20, 2026

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical bug where attempting to deep-copy generator instances equipped with rate limiters would lead to a TypeError because asyncio.Semaphore objects are not picklable. The solution involves a two-pronged approach: first, by switching to a shallow copy operation in the with_params method of BaseGenerator where a deep copy is unnecessary, and second, by explicitly defining a deepcopy method for BaseRateLimiter to gracefully handle deep-copy requests by returning the instance itself, thus avoiding errors with unpicklable asyncio primitives. This ensures the stability of generator instances when their parameters are updated.

Highlights

  • Resolved Deep Copy Issue: Fixed a TypeError occurring when deep-copying BaseGenerator instances that contain BaseRateLimiter due to unpicklable asyncio.Semaphore objects.
  • Shallow Copy Adoption: Modified BaseGenerator.with_params() to use a shallow copy (model_copy()) instead of a deep copy, as only parameters are updated, not mutated.
  • Defensive Deep Copy Handling: Implemented a deepcopy method in BaseRateLimiter to return self, preventing future deep-copy attempts from crashing and ensuring rate limiter state is not duplicated.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request effectively addresses a critical TypeError that occurred during deep-copying of BaseGenerator instances containing asyncio.Semaphore within rate limiters. The change to use a shallow copy in BaseGenerator.model_copy() and the addition of a defensive __deepcopy__ method in BaseRateLimiter are appropriate and robust solutions. These changes improve the stability and maintainability of the generator and rate limiter components.

Comment on lines +127 to +131
@override
def __deepcopy__(self, memo: dict[int, Any] | None = None) -> Self:
"""Return self so that deep-copying a generator doesn't duplicate
rate-limiter state (which contains unpicklable asyncio primitives)."""
return self
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The addition of the __deepcopy__ method to BaseRateLimiter is a crucial defensive measure. By returning self, it correctly handles deep-copy attempts for rate limiters, preventing crashes due to unpicklable asyncio primitives. This directly addresses the root cause of the TypeError mentioned in the PR description.

`model_copy(deep=True)` crashes with `TypeError: cannot pickle
'_asyncio.Future'` when the generator has a rate limiter with
max_concurrent set, because asyncio.Semaphore cannot be deep-copied.

Switch to a shallow copy (sufficient since params is replaced, not
mutated) and add a defensive `__deepcopy__` on BaseRateLimiter to
prevent future deep-copy attempts from crashing.

Closes ENG-1474

Made-with: Cursor
@Hartorn Hartorn force-pushed the fix/with-params-deepcopy-crash branch from 850e393 to 0e1ab98 Compare March 20, 2026 09:51
@kevinmessiaen kevinmessiaen enabled auto-merge (squash) March 20, 2026 15:25
@kevinmessiaen kevinmessiaen disabled auto-merge March 20, 2026 15:25
@kevinmessiaen kevinmessiaen merged commit 89e6f69 into main Mar 20, 2026
23 checks passed
@kevinmessiaen kevinmessiaen deleted the fix/with-params-deepcopy-crash branch March 20, 2026 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

2 participants