Skip to content

The agent that grows with you

refactor(gemini): prefix rule only for no-id slot matching; share the provider-id guard

kshitijk4poora55c972e0917
diff --git a/agent/gemini_native_adapter.py b/agent/gemini_native_adapter.py
index 7ee001d96ab..084522191cc 100644
--- a/agent/gemini_native_adapter.py
+++ b/agent/gemini_native_adapter.py
@@ -436,10 +436,14 @@ def _tool_call_extra_from_part(part: Dict[str, Any]) -> Optional[Dict[str, Any]]
return {"google": {"thought_signature": sig}} if isinstance(sig, str) and sig else None
+def _provider_call_id(fc: Dict[str, Any]) -> Optional[str]:
+ fc_id = fc.get("id")
+ return fc_id if isinstance(fc_id, str) and fc_id else None
+
+
def _new_call_id(fc: Dict[str, Any]) -> str:
"""Echo the functionCall/delta ``id`` when present, else mint an OpenAI-style one."""
- fc_id = fc.get("id")
- return fc_id if isinstance(fc_id, str) and fc_id else f"call_{uuid.uuid4().hex[:12]}"
+ return _provider_call_id(fc) or f"call_{uuid.uuid4().hex[:12]}"
def _dump_call_args(fc: Dict[str, Any], **kwargs: Any) -> str:
@@ -558,46 +562,27 @@ 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,
+def _tool_call_slot(fc: Dict[str, Any], part: Dict[str, Any], part_index: int, 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.
+ Gemini 3 ids each tool call, so the id is the slot identity (``part_index`` and the thought
+ signature drift across events of one call). Gemini 2.5 sends no id and ``part_index`` restarts
+ at 0 per event, so two different calls to one tool in separate events would share a slot and
+ have their arguments concatenated into unparseable JSON: Gemini re-sends full arguments, so a
+ payload that is not a prefix-extension (or resend) of the slot's accumulated arguments is a
+ different call and gets its own ``key#N`` slot, kept reachable so its own resend lands on it.
"""
- 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)
+ if fc_id := _provider_call_id(fc):
+ key = json.dumps({"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)
+ key = json.dumps({"part_index": part_index, "name": fc["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):
+ if slot is None or args_str.startswith(slot["last_arguments"]):
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):
+ if other_key.startswith(f"{key}#") and args_str.startswith(other["last_arguments"]):
return other_key, other
return f"{key}#{len(tool_call_indices)}", None
@@ -621,11 +606,11 @@ 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)
- call_key, slot = _tool_call_slot(fc, part, part_index, name, args_str, tool_call_indices)
+ call_key, slot = _tool_call_slot(fc, part, part_index, 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 "")
+ last_arguments = slot["last_arguments"]
slot["last_arguments"] = args_str
delta = {"index": slot["index"], "id": slot["id"], "name": name, "extra_content": _tool_call_extra_from_part(part),
"arguments": args_str[len(last_arguments):] if args_str.startswith(last_arguments) else args_str}