Skip to content

The agent engineering platform.

chore(core): fix some types related to ToolCallChunk (#34283)

Christophe Bornet914730cf8d8f
diff --git a/libs/core/langchain_core/messages/block_translators/anthropic.py b/libs/core/langchain_core/messages/block_translators/anthropic.py
index d3a297e202..7f514948bf 100644
--- a/libs/core/langchain_core/messages/block_translators/anthropic.py
+++ b/libs/core/langchain_core/messages/block_translators/anthropic.py
@@ -245,17 +245,20 @@ def _convert_to_v1_from_anthropic(message: AIMessage) -> list[types.ContentBlock
and message.chunk_position != "last"
):
# Isolated chunk
- tool_call_chunk: types.ToolCallChunk = (
- message.tool_call_chunks[0].copy() # type: ignore[assignment]
+ chunk = message.tool_call_chunks[0]
+
+ tool_call_chunk = types.ToolCallChunk(
+ name=chunk.get("name"),
+ id=chunk.get("id"),
+ args=chunk.get("args"),
+ type="tool_call_chunk",
)
if "caller" in block:
- if "extras" not in tool_call_chunk:
- tool_call_chunk["extras"] = {}
- tool_call_chunk["extras"]["caller"] = block["caller"]
-
- if "type" not in tool_call_chunk:
- tool_call_chunk["type"] = "tool_call_chunk"
+ tool_call_chunk["extras"] = {"caller": block["caller"]}
+ index = chunk.get("index")
+ if index is not None:
+ tool_call_chunk["index"] = index
yield tool_call_chunk
else:
tool_call_block: types.ToolCall | None = None
@@ -299,11 +302,16 @@ def _convert_to_v1_from_anthropic(message: AIMessage) -> list[types.ContentBlock
message, AIMessageChunk
):
if len(message.tool_call_chunks) == 1:
- tool_call_chunk = (
- message.tool_call_chunks[0].copy() # type: ignore[assignment]
+ chunk = message.tool_call_chunks[0]
+ tool_call_chunk = types.ToolCallChunk(
+ name=chunk.get("name"),
+ id=chunk.get("id"),
+ args=chunk.get("args"),
+ type="tool_call_chunk",
)
- if "type" not in tool_call_chunk:
- tool_call_chunk["type"] = "tool_call_chunk"
+ index = chunk.get("index")
+ if index is not None:
+ tool_call_chunk["index"] = index
yield tool_call_chunk
else:
diff --git a/libs/core/langchain_core/messages/block_translators/bedrock_converse.py b/libs/core/langchain_core/messages/block_translators/bedrock_converse.py
index c44ef6ca53..226edc9de9 100644
--- a/libs/core/langchain_core/messages/block_translators/bedrock_converse.py
+++ b/libs/core/langchain_core/messages/block_translators/bedrock_converse.py
@@ -209,11 +209,16 @@ def _convert_to_v1_from_converse(message: AIMessage) -> list[types.ContentBlock]
and message.chunk_position != "last"
):
# Isolated chunk
- tool_call_chunk: types.ToolCallChunk = (
- message.tool_call_chunks[0].copy() # type: ignore[assignment]
+ chunk = message.tool_call_chunks[0]
+ tool_call_chunk = types.ToolCallChunk(
+ name=chunk.get("name"),
+ id=chunk.get("id"),
+ args=chunk.get("args"),
+ type="tool_call_chunk",
)
- if "type" not in tool_call_chunk:
- tool_call_chunk["type"] = "tool_call_chunk"
+ index = chunk.get("index")
+ if index is not None:
+ tool_call_chunk["index"] = index
yield tool_call_chunk
else:
tool_call_block: types.ToolCall | None = None
@@ -253,11 +258,16 @@ def _convert_to_v1_from_converse(message: AIMessage) -> list[types.ContentBlock]
and isinstance(message, AIMessageChunk)
and len(message.tool_call_chunks) == 1
):
- tool_call_chunk = (
- message.tool_call_chunks[0].copy() # type: ignore[assignment]
+ chunk = message.tool_call_chunks[0]
+ tool_call_chunk = types.ToolCallChunk(
+ name=chunk.get("name"),
+ id=chunk.get("id"),
+ args=chunk.get("args"),
+ type="tool_call_chunk",
)
- if "type" not in tool_call_chunk:
- tool_call_chunk["type"] = "tool_call_chunk"
+ index = chunk.get("index")
+ if index is not None:
+ tool_call_chunk["index"] = index
yield tool_call_chunk
else:
diff --git a/libs/core/tests/unit_tests/test_tools.py b/libs/core/tests/unit_tests/test_tools.py
index 587151f57b..f880555114 100644
--- a/libs/core/tests/unit_tests/test_tools.py
+++ b/libs/core/tests/unit_tests/test_tools.py
@@ -1830,10 +1830,7 @@ def test_tool_inherited_injected_arg() -> None:
"type": "tool_call",
}
) == ToolMessage("bar", tool_call_id="123", name="foo")
- expected_error = (
- ValidationError if not isinstance(tool_, InjectedTool) else TypeError
- )
- with pytest.raises(expected_error):
+ with pytest.raises(ValidationError):
tool_.invoke({"x": 5})
assert convert_to_openai_function(tool_) == {