-
Notifications
You must be signed in to change notification settings - Fork 2.7k
fix: persist inline_data replacement across turns when optimize_data_file=True #4024
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?
fix: persist inline_data replacement across turns when optimize_data_file=True #4024
Conversation
…file=True
Problem:
- When optimize_data_file=True, inline_data (CSV files) were replaced
with text placeholders on the first turn
- Subsequent turns restored original inline_data from session.events
- Full CSV was sent to LLM on every turn, defeating the optimization
Root Cause:
- _extract_and_replace_inline_files() modified llm_request.contents (a copy)
- session.events were never updated with the replacement
- Each turn deep-copied unmodified session.events, restoring inline_data
Solution:
- Modified _extract_and_replace_inline_files() to accept invocation_context
- Track which session events need updates via events_to_update dict
- After replacing in llm_request, find and update matching session.events
- Use event.model_copy(update={...}) for Pydantic model updates
- Added early-exit check for already-processed placeholders
Testing:
- Added test_code_execution_persistence.py with two test cases:
1. test_inline_data_replacement_in_extract_function - verifies both
llm_request and session.events are modified
2. test_persistence_across_simulated_turns - verifies inline_data
doesn't reappear on subsequent turns
- All tests pass
- Verified files not re-injected on subsequent turns
Impact:
- Significantly reduces token usage for sessions with uploaded files
- Lower LLM API costs
- Better performance (smaller request payloads)
- No breaking changes
Fixes google#4013
Summary of ChangesHello @ishanrajsingh, 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 resolves a critical bug in the data optimization flow where inline data, such as CSV files, was not correctly persisted as text placeholders across multiple conversation turns. Previously, enabling Highlights
🧠 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 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 addresses a critical bug where inline data was not being persisted correctly across conversation turns when optimize_data_file=True. The solution of updating session.events in addition to llm_request.contents is correct and well-implemented. The addition of a comprehensive test suite is excellent and ensures the fix is robust and prevents future regressions.
My review includes a few suggestions to improve code style, maintainability, and address a potential performance concern. These are mostly minor points and don't detract from the quality of the fix.
tests/unittests/flows/llm_flows/test_code_execution_persistence.py
Outdated
Show resolved
Hide resolved
- Remove development comments and make docstring more professional - Use f-strings instead of %-formatting for better readability - Define _AVAILABLE_FILE_PREFIX constant to avoid magic strings - Add length check before data comparison for performance optimization - Remove debug print() statements from tests - Improve code documentation and maintainability Changes requested by gemini-code-assist review.
|
/gemini review |
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 modifies _code_execution.py to ensure that inline data files within LLM requests are replaced with file name placeholders persistently across conversation turns. The _extract_and_replace_inline_files function was updated to accept invocation_context and now modifies both the current llm_request and the invocation_context.session.events to store these placeholders, preventing inline data from reappearing in subsequent interactions. A new test file, test_code_execution_persistence.py, was added to validate this persistence, confirming that inline data is correctly replaced in both the request and session, and that this replacement holds true over multiple simulated turns. The reviewer praised the new persistence test but recommended removing several unused variables to improve its clarity.
Summary
Fixes #4013 - Ensures
optimize_data_file=Truecorrectly persists inline_data replacement across conversation turns, preventing redundant CSV file transmission to the LLM.Problem
When
optimize_data_file=Truewas enabled on a customBaseCodeExecutor, inline_data (CSV files) were replaced with text placeholders (Available file: 'xxx.csv') on the first turn. However, on subsequent turns, the original inline_data reappeared, causing the full CSV to be sent to the LLM again.Debug Evidence from Issue #4013
=== AFTER _extract_and_replace_inline_files ===
replaced with: Available file: data_1_2.csv
=== LLM Request Contents (in before_model_callback) ===
inline_data: mime=text/csv, size=61194 ← inline_data restored!
text
Object ID comparison showed different objects between processing steps, confirming the deep copy issue.
Root Cause
_extract_and_replace_inline_files()modifiedllm_request.contents(a temporary copy)session.events(the persistent source) were never updated_run_one_step_async()inbase_llm_flow.pycreates a newLlmRequestviacopy.deepcopy(session.events)session.eventsstill contained original inline_data, it was restoredSolution
Modified
_extract_and_replace_inline_files()to update both:llm_request.contents(for current turn)session.events(for persistence across turns)Key Changes in
_code_execution.py:invocation_contextparameter to accesssession.eventsevents_to_updatedictionarymodel_copy()instead ofdataclasses.replace()Code Changes:
Before (line ~173)
all_input_files = _extract_and_replace_inline_files(
code_executor_context, llm_request
)
After
all_input_files = _extract_and_replace_inline_files(
code_executor_context, llm_request, invocation_context
)
text
Function now updates
session.eventsdirectly:session.events[event_idx] = event.model_copy(
update={'content': updated_content}
)
text
Testing
Added comprehensive test suite in
test_code_execution_persistence.py:Test 1:
test_inline_data_replacement_in_extract_function_extract_and_replace_inline_files()modifies bothllm_requestandsession.eventsTest 2:
test_persistence_across_simulated_turnsTest Results:
✓ All new tests pass
✓ Existing code_execution tests pass
✓ No regressions detected
text
Impact
Benefits:
Risk Assessment:
optimize_data_file=Truecode pathoptimize_data_file=FalseChecklist
optimize_data_file=Truedoes not persist inline_data replacement - CSV sent on every LLM call #4013Additional Notes
This fix is particularly important for:
BaseCodeExecutorsubclassesThe issue was reproduced with Claude via Bedrock, confirming it affects production use cases.