Skip to content

The agent engineering platform.

fix(anthropic): normalize `tool_search_tool_result` blocks (#39621)

Hunter Lovell3c6c3ba7bffe
diff --git a/libs/partners/anthropic/langchain_anthropic/chat_models.py b/libs/partners/anthropic/langchain_anthropic/chat_models.py
index 9d29118393..5b292975ab 100644
--- a/libs/partners/anthropic/langchain_anthropic/chat_models.py
+++ b/libs/partners/anthropic/langchain_anthropic/chat_models.py
@@ -672,6 +672,23 @@ def _format_messages(
},
),
)
+ elif block["type"] == "tool_search_tool_result":
+ # Omit streaming-only fields, such as `index`, from results.
+ content.append(
+ _normalize_block_tool_use_id(
+ {
+ k: v
+ for k, v in block.items()
+ if k
+ in (
+ "type",
+ "content",
+ "tool_use_id",
+ "cache_control",
+ )
+ },
+ ),
+ )
elif block["type"] == "tool_result":
# Regular tool results that need content formatting
tool_content = _format_messages(
diff --git a/libs/partners/anthropic/tests/unit_tests/test_chat_models.py b/libs/partners/anthropic/tests/unit_tests/test_chat_models.py
index fb4d00ae63..f618eee60b 100644
--- a/libs/partners/anthropic/tests/unit_tests/test_chat_models.py
+++ b/libs/partners/anthropic/tests/unit_tests/test_chat_models.py
@@ -3356,6 +3356,35 @@ def test_tool_search_result_formatting() -> None:
assert tool_result_block["content"][1]["tool_name"] == "weather_forecast"
+# Regression test for https://github.com/langchain-ai/langchain/issues/37584
+def test__format_messages_tool_search_result_drops_streaming_index() -> None:
+ """Test that streaming-only indexes are not sent to Anthropic."""
+ messages = [
+ AIMessage( # type: ignore[misc]
+ [
+ {
+ "type": "tool_search_tool_result",
+ "tool_use_id": "srvtoolu_123",
+ "content": [
+ {"type": "tool_reference", "tool_name": "get_weather"},
+ ],
+ "index": 1,
+ },
+ ],
+ ),
+ ]
+
+ _, formatted = _format_messages(messages)
+
+ assert formatted[0]["content"][0] == {
+ "type": "tool_search_tool_result",
+ "tool_use_id": "srvtoolu_123",
+ "content": [
+ {"type": "tool_reference", "tool_name": "get_weather"},
+ ],
+ }
+
+
def test_auto_append_betas_for_mcp_servers() -> None:
"""Test that `mcp-client-2025-11-20` beta is automatically appended
for `mcp_servers`."""