-
Notifications
You must be signed in to change notification settings - Fork 2.7k
fix: Fix infinite loop when using code_executor #3930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
The code_executor was causing an infinite loop because: 1. `_contains_empty_content()` was not checking for `executable_code` and `code_execution_result` parts, causing CODE_EXECUTION_RESULT events to be filtered out as "empty content". 2. Consecutive model contents (executable_code and code_execution_result) were sent as separate Content objects, but the Gemini API expects them to be in the same Content with multiple parts. This fix: - Adds checks for `executable_code` and `code_execution_result` in `_contains_empty_content()` - Merges consecutive model contents into a single Content object Closes google#3921
Summary of ChangesHello @hiroakis, 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 that caused agents utilizing the Highlights
Using Gemini Code AssistThe 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
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 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. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request effectively resolves a critical infinite loop issue that occurred when using a code_executor. The problem was well-diagnosed, stemming from two distinct issues: code_execution_result events being incorrectly filtered as empty, and consecutive model contents not being merged as the Gemini API expects. The fix is clean and directly addresses both root causes by updating _contains_empty_content to recognize code execution parts and by adding logic to _get_contents to merge consecutive model contents. The addition of the test_events_with_code_execution_are_included unit test is excellent, as it thoroughly validates both aspects of the fix. The changes are correct, well-implemented, and improve the robustness of the code execution flow.
|
Hi @hiroakis , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Can you fix lint errors by running autoformat.sh |
Resolve conflict in contents.py by integrating _is_part_invisible() refactoring with executable_code and code_execution_result checks.
|
@ryanaiagent Thank you for your review! I've addressed the feedback in ea4515d and also resolved the merge conflicts in 13f8063. |
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
2. Or, if no issue exists, describe the change:
Problem:
When using
code_executor(e.g.,UnsafeLocalCodeExecutor), the agent enters an infinite loop. The LLM generates code, the code executor runs it and returns a result, but then the LLM generates the same code again instead of providing a final response.Solution:
The infinite loop was caused by two issues in
_get_contents()flow:_contains_empty_content()was not checking forexecutable_codeandcode_execution_resultparts. This causedCODE_EXECUTION_RESULTevents to be filtered out as "empty content", so the LLM never saw the execution results.Consecutive model contents were sent as separate
Contentobjects. The Gemini API expectsexecutable_codeandcode_execution_resultto be in the sameContentwith multiple parts. When sent separately, the LLM doesn't understand that the code was already executed.This fix:
p.executable_codeandp.code_execution_resultin_contains_empty_content()Contentobject in_get_contents()Testing Plan
Unit Tests:
$ uv run pytest tests/unittests --tb=no -q
Manual End-to-End (E2E) Tests:
Based on the code from issue #3921:
mkdir contributing/samples/issue3921-fix uv run contributing/samples/issue3921-fix/agent.py # Reproduction Code aboveChecklist
Additional context
The new test test_events_with_code_execution_are_included verifies that: