The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
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.pyindex 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.ContentBlockand 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"] = indexyield tool_call_chunkelse:tool_call_block: types.ToolCall | None = None@@ -299,11 +302,16 @@ def _convert_to_v1_from_anthropic(message: AIMessage) -> list[types.ContentBlockmessage, 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"] = indexyield tool_call_chunkelse:diff --git a/libs/core/langchain_core/messages/block_translators/bedrock_converse.py b/libs/core/langchain_core/messages/block_translators/bedrock_converse.pyindex 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"] = indexyield tool_call_chunkelse: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"] = indexyield tool_call_chunkelse:diff --git a/libs/core/tests/unit_tests/test_tools.py b/libs/core/tests/unit_tests/test_tools.pyindex 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_) == {