Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/google/adk/flows/llm_flows/contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_<n>`` 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] = []
Expand All @@ -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:
Expand Down
61 changes: 61 additions & 0 deletions tests/unittests/flows/llm_flows/test_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_<n>``) 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",
Expand Down