diff --git a/src/google/adk/flows/llm_flows/contents.py b/src/google/adk/flows/llm_flows/contents.py index 828c3c63ae4..1f5a6d87886 100644 --- a/src/google/adk/flows/llm_flows/contents.py +++ b/src/google/adk/flows/llm_flows/contents.py @@ -144,15 +144,25 @@ def _rearrange_events_for_async_function_responses_in_history( events: list[Event], ) -> list[Event]: """Rearrange the async function_response events in the history.""" - function_call_id_to_response_events_index: dict[str | None, int] = {} + # Pair responses to calls on (id, name), not id alone. Model-supplied function + # call ids are not guaranteed unique: Vertex Gemini mints ``call_`` ids from + # a small space that can repeat within one session, and ADK preserves such ids + # (only ``adk-`` prefixed ids are generated/stripped by the framework). Keying + # on id alone lets a repeated id pair a ``function_call`` for one tool with the + # ``function_response`` of another, which the model rejects with a bare + # ``400 INVALID_ARGUMENT``. Including the tool name keeps a collision from + # resolving across different tools. See issue #6761. + function_call_key_to_response_events_index: dict[ + tuple[str | None, str | None], int + ] = {} for i, event in enumerate(events): function_responses = event.get_function_responses() if function_responses: for function_response in function_responses: - function_call_id = function_response.id - function_call_id_to_response_events_index[function_call_id] = i + function_call_key = (function_response.id, function_response.name) + function_call_key_to_response_events_index[function_call_key] = i - if not function_call_id_to_response_events_index: + if not function_call_key_to_response_events_index: return events result_events: list[Event] = [] @@ -164,10 +174,10 @@ def _rearrange_events_for_async_function_responses_in_history( function_response_events_indices = set() for function_call in event.get_function_calls(): - function_call_id = function_call.id - if function_call_id in function_call_id_to_response_events_index: + function_call_key = (function_call.id, function_call.name) + if function_call_key in function_call_key_to_response_events_index: function_response_events_indices.add( - function_call_id_to_response_events_index[function_call_id] + function_call_key_to_response_events_index[function_call_key] ) result_events.append(event) if not function_response_events_indices: diff --git a/tests/unittests/flows/llm_flows/test_contents.py b/tests/unittests/flows/llm_flows/test_contents.py index a7d9f7d3a25..fe5382cc9d9 100644 --- a/tests/unittests/flows/llm_flows/test_contents.py +++ b/tests/unittests/flows/llm_flows/test_contents.py @@ -1964,6 +1964,67 @@ def test_rearrange_async_function_responses_early_returns_when_no_responses(): assert result is events +def test_rearrange_async_function_responses_pairs_on_id_and_name(): + """A reused function-call id must not pair a call with another tool's response. + + Model-supplied ids (e.g. Vertex Gemini's ``call_``) can repeat within a + session. Keying only on id let the newest response for a duplicated id attach + to the oldest call carrying that id, mismatching the tool name and producing a + ``400 INVALID_ARGUMENT`` on the next request. See issue #6761. + """ + + def _call_event(invocation_id: str, call_id: str, name: str) -> Event: + return Event( + invocation_id=invocation_id, + author="test_agent", + content=types.Content( + role="model", + parts=[ + types.Part( + function_call=types.FunctionCall( + id=call_id, name=name, args={} + ) + ) + ], + ), + ) + + def _response_event(invocation_id: str, call_id: str, name: str) -> Event: + return Event( + invocation_id=invocation_id, + author="user", + content=types.Content( + role="user", + parts=[ + types.Part( + function_response=types.FunctionResponse( + id=call_id, name=name, response={"tool": name} + ) + ) + ], + ), + ) + + # Both calls reuse id "call_807" for two different tools, interleaved with + # their responses, mirroring the collision captured in the bug report. + call_a = _call_event("inv1", "call_807", "site_security_posture") + response_a = _response_event("inv1", "call_807", "site_security_posture") + call_b = _call_event("inv2", "call_807", "fleet_security_summary") + response_b = _response_event("inv2", "call_807", "fleet_security_summary") + events = [call_a, response_a, call_b, response_b] + + result = contents._rearrange_events_for_async_function_responses_in_history( # pylint: disable=protected-access + events + ) + + # Each call must be immediately followed by the response for the SAME tool. + assert len(result) == 4 + assert result[0] is call_a + assert result[1].get_function_responses()[0].name == "site_security_posture" + assert result[2] is call_b + assert result[3].get_function_responses()[0].name == "fleet_security_summary" + + def _long_running_call_event() -> Event: return Event( invocation_id="inv2",