Skip to content

Commit a36ddd4

Browse files
ccurmeopen-swe
andauthored
fix(anthropic): filter invalid tool calls from v1 content (#39803)
Co-authored-by: open-swe[bot] <[email protected]>
1 parent 3478c28 commit a36ddd4

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

‎libs/partners/anthropic/langchain_anthropic/_compat.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ def _convert_from_v1_to_anthropic(
140140
}
141141
)
142142

143+
elif block["type"] == "invalid_tool_call":
144+
continue
145+
143146
elif block["type"] == "reasoning" and model_provider == "anthropic":
144147
new_block = {}
145148
if "reasoning" in block:
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Tests for Anthropic content compatibility helpers."""
2+
3+
from langchain_core.messages import content as types
4+
5+
from langchain_anthropic._compat import _convert_from_v1_to_anthropic
6+
7+
8+
def test_convert_from_v1_filters_invalid_tool_calls() -> None:
9+
"""Test that invalid tool calls are not sent to Anthropic."""
10+
content: list[types.ContentBlock] = [
11+
{"type": "text", "text": "Let me check."},
12+
{
13+
"type": "invalid_tool_call",
14+
"id": "toolu_invalid",
15+
"name": "get_weather",
16+
"args": '{"city":',
17+
"error": "Invalid JSON",
18+
},
19+
{
20+
"type": "tool_call",
21+
"id": "toolu_valid",
22+
"name": "get_weather",
23+
"args": {"city": "San Francisco"},
24+
},
25+
]
26+
27+
result = _convert_from_v1_to_anthropic(content, [], "anthropic")
28+
29+
assert result == [
30+
{"type": "text", "text": "Let me check."},
31+
{
32+
"type": "tool_use",
33+
"id": "toolu_valid",
34+
"name": "get_weather",
35+
"input": {"city": "San Francisco"},
36+
},
37+
]

0 commit comments

Comments
 (0)