fix: merge server_side_tool_invocations by id in stream_chunk_builder

The naive last-wins merge for list-type provider_specific_fields caused
Gemini streaming to corrupt server_side_tool_invocations and
thought_signatures. Replace with merge-by-id for tool invocations
(first-wins per key, filling None values from later chunks) and
accumulate thought_signatures across chunks.

Fixes #25869
This commit is contained in:
Nilesh Patil 2026-04-21 00:57:32 +05:30
parent b8f7d61400
commit e1f3befa76

View file

@ -7604,6 +7604,19 @@ def stream_chunk_builder( # noqa: PLR0915
for key, value in fields.items():
if key not in combined_provider_fields:
combined_provider_fields[key] = value
elif key == "server_side_tool_invocations" and isinstance(value, list):
existing = {e.get("id", ""): e for e in combined_provider_fields[key]}
for inv in value:
inv_id = inv.get("id", "")
if inv_id in existing:
for k, v in inv.items():
if v is not None and (k not in existing[inv_id] or existing[inv_id][k] is None):
existing[inv_id][k] = v
else:
existing[inv_id] = dict(inv)
combined_provider_fields[key] = list(existing.values())
elif key == "thought_signatures" and isinstance(value, list):
combined_provider_fields[key] = combined_provider_fields[key] + value
elif isinstance(value, list) and isinstance(
combined_provider_fields[key], list
):