Skip to content

The agent engineering platform.

fix(core): shore up indexing in genai v1 streaming content (#39964)

ccurme38e211359f0b
diff --git a/libs/core/langchain_core/messages/block_translators/google_genai.py b/libs/core/langchain_core/messages/block_translators/google_genai.py
index c2070334c9..d0f5f6b6c1 100644
--- a/libs/core/langchain_core/messages/block_translators/google_genai.py
+++ b/libs/core/langchain_core/messages/block_translators/google_genai.py
@@ -296,6 +296,27 @@ def _convert_to_v1_from_genai_input(
return list(_iter_blocks())
+def _tool_call_indices(message: AIMessage) -> dict[str, str]:
+ """Map tool call id to its streaming index.
+
+ ChatGoogleGenerativeAI AIMessage .content does not carry function call parts, so
+ we recover sequencing information from the tool_call_chunks field.
+
+ Args:
+ message: The message whose tool call chunks should be indexed.
+
+ Returns:
+ Mapping of tool call id to prefixed index, for chunks that have both.
+ """
+ indices: dict[str, str] = {}
+ for chunk in getattr(message, "tool_call_chunks", None) or []:
+ id_ = chunk.get("id")
+ index = chunk.get("index")
+ if id_ and index is not None:
+ indices[id_] = f"lc_tc_{index}" # prefix to keep disjoint w other indices
+ return indices
+
+
def _convert_to_v1_from_genai(message: AIMessage) -> list[types.ContentBlock]:
"""Convert Google GenAI message content to v1 format.
@@ -321,6 +342,7 @@ def _convert_to_v1_from_genai(message: AIMessage) -> list[types.ContentBlock]:
for block in string_blocks
if isinstance(block, dict) and block.get("type") == "tool_call"
}
+ tool_call_indices = _tool_call_indices(message)
for tool_call in message.tool_calls:
id_ = tool_call.get("id")
if id_ and id_ not in content_tool_call_ids:
@@ -330,6 +352,8 @@ def _convert_to_v1_from_genai(message: AIMessage) -> list[types.ContentBlock]:
"name": tool_call["name"],
"args": tool_call["args"],
}
+ if (index := tool_call_indices.get(id_)) is not None:
+ string_tool_call_block["index"] = index
string_blocks.append(string_tool_call_block)
# Handle audio from additional_kwargs if present (for empty content cases)
@@ -361,6 +385,8 @@ def _convert_to_v1_from_genai(message: AIMessage) -> list[types.ContentBlock]:
converted_blocks: list[types.ContentBlock] = []
for item in message.content:
+ # Where this item's blocks start, so they can inherit its `index` below.
+ block_start = len(converted_blocks)
if isinstance(item, str):
# Conversation history strings
@@ -487,6 +513,16 @@ def _convert_to_v1_from_genai(message: AIMessage) -> list[types.ContentBlock]:
# Non-dict, non-string content
converted_blocks.append({"type": "non_standard", "value": item}) # type: ignore[unreachable]
+ # Each item yields exactly one block today. Guarded, not assumed:
+ # duplicate indices would merge the blocks back together.
+ new_blocks = converted_blocks[block_start:]
+ if (
+ len(new_blocks) == 1
+ and isinstance(item, dict)
+ and (index := item.get("index")) is not None
+ ):
+ new_blocks[0].setdefault("index", index)
+
grounding_metadata = message.response_metadata.get("grounding_metadata")
if grounding_metadata:
citations = translate_grounding_metadata_to_citations(grounding_metadata)
@@ -513,6 +549,7 @@ def _convert_to_v1_from_genai(message: AIMessage) -> list[types.ContentBlock]:
for block in converted_blocks
if isinstance(block, dict) and block.get("type") == "tool_call"
}
+ tool_call_indices = _tool_call_indices(message)
for tool_call in message.tool_calls:
id_ = tool_call.get("id")
if id_ and id_ not in content_tool_call_ids:
@@ -522,6 +559,8 @@ def _convert_to_v1_from_genai(message: AIMessage) -> list[types.ContentBlock]:
"name": tool_call["name"],
"args": tool_call["args"],
}
+ if (index := tool_call_indices.get(id_)) is not None:
+ missing_tool_call_block["index"] = index
converted_blocks.append(missing_tool_call_block)
return converted_blocks
diff --git a/libs/core/tests/unit_tests/messages/block_translators/test_google_genai.py b/libs/core/tests/unit_tests/messages/block_translators/test_google_genai.py
index 5679d1fd9e..fcb009a868 100644
--- a/libs/core/tests/unit_tests/messages/block_translators/test_google_genai.py
+++ b/libs/core/tests/unit_tests/messages/block_translators/test_google_genai.py
@@ -1,8 +1,12 @@
"""Tests for Google GenAI block translator."""
+from typing import Any
+
+from langchain_core.messages import AIMessageChunk
from langchain_core.messages.block_translators.google_genai import (
translate_grounding_metadata_to_citations,
)
+from langchain_core.messages.tool import tool_call_chunk as create_tool_call_chunk
def test_translate_grounding_metadata_web() -> None:
@@ -216,3 +220,114 @@ def test_translate_grounding_metadata_multiple_chunks() -> None:
assert (
citations[1].get("extras", {})["google_ai_metadata"]["place_id"] == "places/123"
)
+
+
+def _image_block(data: str, index: int) -> dict[str, Any]:
+ return {
+ "type": "image_url",
+ "image_url": {"url": f"data:image/png;base64,{data}"},
+ "index": index,
+ }
+
+
+def _genai_chunk(**kwargs: Any) -> AIMessageChunk:
+ return AIMessageChunk(
+ response_metadata={"model_provider": "google_genai"}, **kwargs
+ )
+
+
+def test_content_blocks_preserve_index_for_images() -> None:
+ """Streaming index must survive the `image_url` -> `image` conversion.
+
+ Consumers that key blocks by index (the `v3` event stream) merge blocks that
+ share one, so dropping the index collapses distinct images into one.
+ """
+ message = _genai_chunk(
+ content=[_image_block("QUFBQQ==", 0), _image_block("QkJCQg==", 1)]
+ )
+
+ blocks = message.content_blocks
+
+ assert [block["type"] for block in blocks] == ["image", "image"]
+ assert [block.get("index") for block in blocks] == [0, 1]
+
+
+def test_content_blocks_preserve_index_for_parallel_tool_calls() -> None:
+ """Tool calls rebuilt from `tool_calls` must recover their chunk index."""
+ message = _genai_chunk(
+ content=[],
+ tool_call_chunks=[
+ create_tool_call_chunk(name="f1", args='{"a": 1}', id="id-1", index=0),
+ create_tool_call_chunk(name="f2", args='{"b": 2}', id="id-2", index=1),
+ ],
+ )
+
+ blocks = message.content_blocks
+
+ assert [block["type"] for block in blocks] == ["tool_call", "tool_call"]
+ assert [block.get("id") for block in blocks] == ["id-1", "id-2"]
+ assert [block.get("index") for block in blocks] == ["lc_tc_0", "lc_tc_1"]
+
+
+def test_content_blocks_tool_call_index_cannot_collide_with_content() -> None:
+ """Tool call indices must not land in the content block index namespace.
+
+ Tool call indices count from zero independently of content block indices, so
+ an unprefixed copy lets an image at index 0 and the first tool call share a
+ wire block -- the image starts the block and the tool call finishes it,
+ silently dropping the image. A per-message translator cannot dedupe against
+ indices used by earlier chunks, so the namespaces must be disjoint.
+ """
+ image_chunk = _genai_chunk(content=[_image_block("QUFBQQ==", 0)])
+ tool_chunk = _genai_chunk(
+ content=[],
+ tool_call_chunks=[
+ create_tool_call_chunk(name="f1", args='{"a": 1}', id="id-1", index=0)
+ ],
+ )
+
+ image_index = image_chunk.content_blocks[0]["index"]
+ tool_index = tool_chunk.content_blocks[0]["index"]
+
+ assert image_index == 0
+ assert tool_index != image_index
+
+
+def test_content_blocks_omit_index_when_source_has_none() -> None:
+ """Blocks without a source index must not gain a spurious one."""
+ message = _genai_chunk(
+ content=[
+ {
+ "type": "image_url",
+ "image_url": {"url": "data:image/png;base64,QUFBQQ=="},
+ }
+ ],
+ tool_call_chunks=[
+ create_tool_call_chunk(name="f1", args='{"a": 1}', id="id-1", index=None)
+ ],
+ )
+
+ for block in message.content_blocks:
+ assert "index" not in block
+
+
+def test_content_blocks_index_propagation_is_one_to_one() -> None:
+ """Each source item must map to exactly one indexed block.
+
+ Index propagation copies an item's index onto the block it produced. If an
+ item ever expanded into several blocks, copying one index onto all of them
+ would recreate the very collision this propagation prevents, so indices must
+ never be duplicated across blocks.
+ """
+ message = _genai_chunk(
+ content=[
+ {"type": "thinking", "thinking": "hmm", "index": 0},
+ _image_block("QUFBQQ==", 1),
+ {"type": "text", "text": "hi", "index": 2},
+ ]
+ )
+
+ indices = [block["index"] for block in message.content_blocks if "index" in block]
+
+ assert indices == [0, 1, 2]
+ assert len(indices) == len(set(indices))
diff --git a/libs/langchain/tests/unit_tests/chat_models/test_base.py b/libs/langchain/tests/unit_tests/chat_models/test_base.py
index dad87c9830..47acaab835 100644
--- a/libs/langchain/tests/unit_tests/chat_models/test_base.py
+++ b/libs/langchain/tests/unit_tests/chat_models/test_base.py
@@ -271,6 +271,7 @@ def test_configurable_with_default() -> None:
"anthropic_api_url": "https://api.anthropic.com",
"anthropic_proxy": None,
"context_management": None,
+ "container": None,
"anthropic_api_key": SecretStr("bar"),
"betas": None,
"default_headers": None,
diff --git a/libs/langchain_v1/tests/unit_tests/chat_models/test_chat_models.py b/libs/langchain_v1/tests/unit_tests/chat_models/test_chat_models.py
index 3d7b67b91a..e9a194dcd9 100644
--- a/libs/langchain_v1/tests/unit_tests/chat_models/test_chat_models.py
+++ b/libs/langchain_v1/tests/unit_tests/chat_models/test_chat_models.py
@@ -399,6 +399,7 @@ def test_configurable_with_default() -> None:
"context_management": None,
"anthropic_api_key": SecretStr("bar"),
"betas": None,
+ "container": None,
"default_headers": None,
"model_kwargs": {},
"reuse_last_container": None,