The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gemini): give each native tool call its own streaming slot
jmiguellucas91665b5e7994
diff --git a/agent/gemini_native_adapter.py b/agent/gemini_native_adapter.pyindex 2c43bc89b83..7ee001d96ab 100644--- a/agent/gemini_native_adapter.py+++ b/agent/gemini_native_adapter.py@@ -558,6 +558,50 @@ def _iter_sse_events(response: httpx.Response) -> Iterator[Dict[str, Any]]:yield payload+def _tool_call_slot_accepts(slot: Dict[str, Any], args_str: str) -> bool:+ """Whether ``args_str`` continues (or resends) the call already in ``slot``.++ Only consulted when the event carries no provider call id. A continuation extends the+ accumulated arguments and a resend repeats them, so both are prefix matches; a slot still+ holding half-sent JSON is mid-stream and keeps whatever follows. Anything else arriving after+ a complete JSON object is a different call.+ """+ previous = str(slot.get("last_arguments") or "")+ if not previous or args_str.startswith(previous):+ return True+ try:+ json.loads(previous)+ except (json.JSONDecodeError, TypeError, ValueError):+ return True+ return False+++def _tool_call_slot(fc: Dict[str, Any], part: Dict[str, Any], part_index: int, name: str, args_str: str,+ tool_call_indices: Dict[str, Dict[str, Any]]) -> tuple[str, Optional[Dict[str, Any]]]:+ """``(key, existing slot or None)`` for a streamed functionCall.++ Gemini 3 assigns one ``id`` per tool call, so it is the slot identity (``part_index`` and the+ thought signature both drift across events of one call). Gemini 2.5 sends no id and+ ``part_index`` restarts at 0 on every event, so two *different* calls to the same tool in+ separate events would share a slot and have their arguments concatenated into unparseable+ JSON; tell them apart by value, and keep the extra slots reachable (``key#N``) so a later+ continuation or resend lands on the slot it opened.+ """+ fc_id = fc.get("id")+ if isinstance(fc_id, str) and fc_id:+ key = json.dumps({"name": name, "provider_call_id": fc_id}, sort_keys=True)+ return key, tool_call_indices.get(key)+ thought_signature = part.get("thoughtSignature") if isinstance(part.get("thoughtSignature"), str) else ""+ key = json.dumps({"part_index": part_index, "name": name, "thought_signature": thought_signature}, sort_keys=True)+ slot = tool_call_indices.get(key)+ if slot is None or _tool_call_slot_accepts(slot, args_str):+ return key, slot+ for other_key, other in tool_call_indices.items():+ if other_key.startswith(f"{key}#") and _tool_call_slot_accepts(other, args_str):+ return other_key, other+ return f"{key}#{len(tool_call_indices)}", None++def translate_stream_event(event: Dict[str, Any], model: str, tool_call_indices: Dict[str, Dict[str, Any]]) -> List[_GeminiStreamChunk]:candidates = event.get("candidates") or []if not candidates:@@ -577,9 +621,8 @@ def translate_stream_event(event: Dict[str, Any], model: str, tool_call_indices:if fc := _part_function_call(part):name = str(fc["name"])args_str = _dump_call_args(fc, sort_keys=True)- thought_signature = part.get("thoughtSignature") if isinstance(part.get("thoughtSignature"), str) else ""- call_key = json.dumps({"part_index": part_index, "name": name, "thought_signature": thought_signature}, sort_keys=True)- if (slot := tool_call_indices.get(call_key)) is None:+ call_key, slot = _tool_call_slot(fc, part, part_index, name, args_str, tool_call_indices)+ if slot is None:slot = tool_call_indices[call_key] = {"index": len(tool_call_indices), "id": _new_call_id(fc), "last_arguments": ""}# Gemini re-sends the full args each event; emit only the new suffix.last_arguments = str(slot.get("last_arguments") or "")diff --git a/tests/agent/test_gemini_native_adapter.py b/tests/agent/test_gemini_native_adapter.pyindex 6ad2c95068e..cbb3be0e890 100644--- a/tests/agent/test_gemini_native_adapter.py+++ b/tests/agent/test_gemini_native_adapter.py@@ -400,6 +400,54 @@ def test_stream_event_translation_emits_tool_call_delta_with_stable_index():assert first[-1].choices[0].finish_reason == "tool_calls"+def _fc_event(name, args, call_id=None, finish=None):+ fc = {"name": name, "args": args}+ if call_id:+ fc["id"] = call_id+ cand = {"content": {"parts": [{"functionCall": fc}]}}+ if finish:+ cand["finishReason"] = finish+ return {"candidates": [cand]}+++def _tool_call_deltas(chunks):+ return [(tc.index, tc.function.arguments) for c in chunks for tc in (c.choices[0].delta.tool_calls or [])]+++def test_distinct_same_tool_calls_in_separate_events_get_their_own_slots():+ """Gemini 2.5 (no call id) streams each parallel call as parts[0] of its own event; two+ different calls to one tool must not share a slot, and a resend of the second must land on+ the slot it opened instead of concatenating onto the first."""+ from agent.gemini_native_adapter import translate_stream_event++ slots = {}+ out = []+ for args in ({"q": "A"}, {"q": "B"}, {"q": "B"}):+ out += translate_stream_event(_fc_event("search", args), model="gemini-2.5-flash", tool_call_indices=slots)++ assert _tool_call_deltas(out) == [(0, '{"q": "A"}'), (1, '{"q": "B"}'), (1, "")]+ for (_, arguments) in _tool_call_deltas(out)[:2]:+ json.loads(arguments)+++def test_provider_call_id_is_the_slot_identity():+ """Gemini 3 ids two calls with identical name and args as two calls; the same id across events+ (with drifting part_index / signature) is one call."""+ from agent.gemini_native_adapter import translate_stream_event++ slots = {}+ out = translate_stream_event(_fc_event("search", {"q": "A"}, call_id="c1"), model="gemini-3-pro", tool_call_indices=slots)+ out += translate_stream_event(_fc_event("search", {"q": "A"}, call_id="c2"), model="gemini-3-pro", tool_call_indices=slots)+ resend = {"candidates": [{"content": {"parts": [{"text": "x"}, {"functionCall": {"name": "search", "args": {"q": "A"}, "id": "c1"},+ "thoughtSignature": "sig"}]}}]}+ out += translate_stream_event(resend, model="gemini-3-pro", tool_call_indices=slots)++ deltas = _tool_call_deltas(out)+ assert deltas == [(0, '{"q": "A"}'), (1, '{"q": "A"}'), (0, "")]+ ids = [tc.id for c in out for tc in (c.choices[0].delta.tool_calls or [])]+ assert ids == ["c1", "c2", "c1"]++def test_build_gemini_request_preserves_explicit_max_tokens_without_thinking():from agent.gemini_native_adapter import build_gemini_request