mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 10:17:00 +00:00
fix(anthropic): ignore null values of caller on tool_use blocks (#34286)
This commit is contained in:
@@ -439,7 +439,7 @@ def _format_messages(
|
|||||||
if (
|
if (
|
||||||
isinstance(message, AIMessage)
|
isinstance(message, AIMessage)
|
||||||
and (block["id"] in [tc["id"] for tc in message.tool_calls])
|
and (block["id"] in [tc["id"] for tc in message.tool_calls])
|
||||||
and "caller" not in block # take caller from content
|
and not block.get("caller")
|
||||||
):
|
):
|
||||||
overlapping = [
|
overlapping = [
|
||||||
tc
|
tc
|
||||||
@@ -467,8 +467,8 @@ def _format_messages(
|
|||||||
input=args,
|
input=args,
|
||||||
id=block["id"],
|
id=block["id"],
|
||||||
)
|
)
|
||||||
if "caller" in block:
|
if caller := block.get("caller"):
|
||||||
tool_use_block["caller"] = block["caller"]
|
tool_use_block["caller"] = caller
|
||||||
content.append(tool_use_block)
|
content.append(tool_use_block)
|
||||||
elif block["type"] in ("server_tool_use", "mcp_tool_use"):
|
elif block["type"] in ("server_tool_use", "mcp_tool_use"):
|
||||||
formatted_block = {
|
formatted_block = {
|
||||||
@@ -2349,19 +2349,17 @@ class ChatAnthropic(BaseChatModel):
|
|||||||
|
|
||||||
# Remove citations if they are None - introduced in anthropic sdk 0.45
|
# Remove citations if they are None - introduced in anthropic sdk 0.45
|
||||||
for block in content:
|
for block in content:
|
||||||
if (
|
if isinstance(block, dict):
|
||||||
isinstance(block, dict)
|
if "citations" in block and block["citations"] is None:
|
||||||
and "citations" in block
|
block.pop("citations")
|
||||||
and block["citations"] is None
|
if "caller" in block and block["caller"] is None:
|
||||||
):
|
block.pop("caller")
|
||||||
block.pop("citations")
|
if (
|
||||||
if (
|
block.get("type") == "thinking"
|
||||||
isinstance(block, dict)
|
and "text" in block
|
||||||
and block.get("type") == "thinking"
|
and block["text"] is None
|
||||||
and "text" in block
|
):
|
||||||
and block["text"] is None
|
block.pop("text")
|
||||||
):
|
|
||||||
block.pop("text")
|
|
||||||
|
|
||||||
llm_output = {
|
llm_output = {
|
||||||
k: v for k, v in data_dict.items() if k not in ("content", "role", "type")
|
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(
|
|||||||
warnings.warn("Received unexpected tool content block.", stacklevel=2)
|
warnings.warn("Received unexpected tool content block.", stacklevel=2)
|
||||||
|
|
||||||
content_block = event.content_block.model_dump()
|
content_block = event.content_block.model_dump()
|
||||||
|
if "caller" in content_block and content_block["caller"] is None:
|
||||||
|
content_block.pop("caller")
|
||||||
content_block["index"] = event.index
|
content_block["index"] = event.index
|
||||||
if event.content_block.type == "tool_use":
|
if event.content_block.type == "tool_use":
|
||||||
if (
|
if (
|
||||||
|
|||||||
Reference in New Issue
Block a user