-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Fix: Process function_response from separate request by searching full history Fixes #3531 #3553
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
Open
miguelabdonsh
wants to merge
4
commits into
google:main
Choose a base branch
from
miguelabdonsh:fix/function-response-separate-request-3531
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−4
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0d4debb
fix: search full session history for function_call when processing as…
miguelabdonsh 74b034f
fix: correctly map event index from full history to filtered events
miguelabdonsh c697902
fix(llm_flows): use attribute comparison for event
miguelabdonsh 01ba453
fix: apply pyink formatting to
miguelabdonsh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
tests/unittests/flows/llm_flows/test_contents_function_response_separate_request.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Tests for function_response sent in separate request (async tools).""" | ||
|
|
||
| from google.adk.agents.llm_agent import Agent | ||
| from google.adk.events.event import Event | ||
| from google.adk.flows.llm_flows import contents | ||
| from google.adk.models.llm_request import LlmRequest | ||
| from google.genai import types | ||
| import pytest | ||
|
|
||
| from ... import testing_utils | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_function_response_in_separate_request(): | ||
| """Test function_response sent separately finds function_call in history.""" | ||
| agent = Agent(model="gemini-2.5-flash", name="test_agent") | ||
| llm_request = LlmRequest(model="gemini-2.5-flash") | ||
| invocation_context = await testing_utils.create_invocation_context( | ||
| agent=agent | ||
| ) | ||
|
|
||
| function_call = types.FunctionCall( | ||
| id="async_call_123", name="async_tool", args={"job": "start"} | ||
| ) | ||
| function_response = types.FunctionResponse( | ||
| id="async_call_123", | ||
| name="async_tool", | ||
| response={"status": "completed"}, | ||
| ) | ||
|
|
||
| # Simulate async job: function_call in early session history, | ||
| # function_response arrives much later in separate request | ||
| events = [ | ||
| Event( | ||
| invocation_id="inv1", | ||
| author="user", | ||
| content=types.UserContent("Start async job"), | ||
| ), | ||
| Event( | ||
| invocation_id="inv2", | ||
| author="test_agent", | ||
| content=types.ModelContent([types.Part(function_call=function_call)]), | ||
| ), | ||
| Event( | ||
| invocation_id="inv3", | ||
| author="test_agent", | ||
| content=types.ModelContent("Job started, waiting for completion..."), | ||
| ), | ||
| # Much later: function_response arrives (separate SSE request) | ||
| Event( | ||
| invocation_id="inv4", | ||
| author="user", | ||
| content=types.UserContent( | ||
| [types.Part(function_response=function_response)] | ||
| ), | ||
| ), | ||
| ] | ||
| # Simulate event cloning that happens during processing | ||
| events = [e.model_copy(deep=True) for e in events] | ||
| invocation_context.session.events = events | ||
|
|
||
| # Should not raise ValueError | ||
| async for _ in contents.request_processor.run_async( | ||
| invocation_context, llm_request | ||
| ): | ||
| pass | ||
|
|
||
| # Verify function_response is processed | ||
| assert any( | ||
| hasattr(part, "function_response") | ||
| and part.function_response | ||
| and part.function_response.id == "async_call_123" | ||
| for content in llm_request.contents | ||
| for part in content.parts | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
For identifying an event, it's more robust to use the
event.idfield, which is a unique UUID. Comparinginvocation_idandtimestampcan be brittle for the following reasons:invocation_idis not unique to a single event; multiple events can share one.timestampis a float, and direct equality comparison (==) can be unreliable due to floating-point precision issues.Using
event.idprovides a more reliable way to match the exact event.Uh oh!
There was an error while loading. Please reload this page.
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.
This is consistent with how event uniqueness is managed in other parts of the ADK. Can you implement this suggestion.