Skip to content

Commit 69dd39c

Browse files
authored
fix(anthropic): ignore null values of caller on tool_use blocks (#34286)
1 parent 41cebfe commit 69dd39c

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

libs/partners/anthropic/langchain_anthropic/chat_models.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ def _format_messages(
439439
if (
440440
isinstance(message, AIMessage)
441441
and (block["id"] in [tc["id"] for tc in message.tool_calls])
442-
and "caller" not in block # take caller from content
442+
and not block.get("caller")
443443
):
444444
overlapping = [
445445
tc
@@ -467,8 +467,8 @@ def _format_messages(
467467
input=args,
468468
id=block["id"],
469469
)
470-
if "caller" in block:
471-
tool_use_block["caller"] = block["caller"]
470+
if caller := block.get("caller"):
471+
tool_use_block["caller"] = caller
472472
content.append(tool_use_block)
473473
elif block["type"] in ("server_tool_use", "mcp_tool_use"):
474474
formatted_block = {
@@ -2349,19 +2349,17 @@ def _format_output(self, data: Any, **kwargs: Any) -> ChatResult:
23492349

23502350
# Remove citations if they are None - introduced in anthropic sdk 0.45
23512351
for block in content:
2352-
if (
2353-
isinstance(block, dict)
2354-
and "citations" in block
2355-
and block["citations"] is None
2356-
):
2357-
block.pop("citations")
2358-
if (
2359-
isinstance(block, dict)
2360-
and block.get("type") == "thinking"
2361-
and "text" in block
2362-
and block["text"] is None
2363-
):
2364-
block.pop("text")
2352+
if isinstance(block, dict):
2353+
if "citations" in block and block["citations"] is None:
2354+
block.pop("citations")
2355+
if "caller" in block and block["caller"] is None:
2356+
block.pop("caller")
2357+
if (
2358+
block.get("type") == "thinking"
2359+
and "text" in block
2360+
and block["text"] is None
2361+
):
2362+
block.pop("text")
23652363

23662364
llm_output = {
23672365
k: v for k, v in data_dict.items() if k not in ("content", "role", "type")
@@ -3278,6 +3276,8 @@ def _make_message_chunk_from_anthropic_event(
32783276
warnings.warn("Received unexpected tool content block.", stacklevel=2)
32793277

32803278
content_block = event.content_block.model_dump()
3279+
if "caller" in content_block and content_block["caller"] is None:
3280+
content_block.pop("caller")
32813281
content_block["index"] = event.index
32823282
if event.content_block.type == "tool_use":
32833283
if (

0 commit comments

Comments
 (0)