Skip to content

Commit ce90661

Browse files
hntrlopen-sweccurme
authored
fix(anthropic): support Claude Sonnet 5.5 compatibility (#40882)
Co-authored-by: Hunter Lovell <[email protected]> Co-authored-by: open-swe[bot] <[email protected]> Co-authored-by: ccurme <[email protected]> Co-authored-by: Chester Curme <[email protected]>
1 parent 78a3cbc commit ce90661

10 files changed

Lines changed: 559 additions & 19 deletions

File tree

‎libs/core/langchain_core/messages/ai.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,8 +617,7 @@ def init_server_tool_calls(self) -> Self:
617617
isinstance(block, dict)
618618
and block.get("type")
619619
in {"server_tool_call", "server_tool_call_chunk"}
620-
and (args_str := block.get("args"))
621-
and isinstance(args_str, str)
620+
and isinstance(args_str := block.get("args") or "{}", str)
622621
):
623622
try:
624623
args = json.loads(args_str)

‎libs/core/langchain_core/messages/block_translators/anthropic.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,9 @@ def _iter_blocks() -> Iterator[types.ContentBlock]:
286286
args=chunk.get("args"),
287287
type="tool_call_chunk",
288288
)
289-
if "caller" in block:
290-
tool_call_chunk["extras"] = {"caller": block["caller"]}
289+
for key in ("caller", "toolset_name"):
290+
if key in block:
291+
tool_call_chunk.setdefault("extras", {})[key] = block[key]
291292

292293
index = chunk.get("index")
293294
if index is not None:
@@ -322,10 +323,9 @@ def _iter_blocks() -> Iterator[types.ContentBlock]:
322323
}
323324
if "index" in block:
324325
tool_call_block["index"] = block["index"]
325-
if "caller" in block:
326-
if "extras" not in tool_call_block:
327-
tool_call_block["extras"] = {}
328-
tool_call_block["extras"]["caller"] = block["caller"]
326+
for key in ("caller", "toolset_name"):
327+
if key in block:
328+
tool_call_block.setdefault("extras", {})[key] = block[key]
329329

330330
yield tool_call_block
331331

‎libs/core/tests/unit_tests/messages/block_translators/test_anthropic.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,34 @@ def test_convert_to_v1_from_anthropic_malformed_citations() -> None:
572572
],
573573
},
574574
]
575+
576+
577+
def test_toolset_namespace_in_content_blocks() -> None:
578+
block = {
579+
"type": "tool_use",
580+
"id": "call_1",
581+
"name": "click",
582+
"input": {},
583+
"toolset_name": "computer",
584+
}
585+
metadata = {"model_provider": "anthropic"}
586+
message = AIMessage([block], response_metadata=metadata)
587+
content_block = message.content_blocks[0]
588+
assert content_block["type"] == "tool_call"
589+
assert content_block["extras"]["toolset_name"] == "computer"
590+
chunk = AIMessageChunk(
591+
content=[{**block, "index": 0}],
592+
tool_call_chunks=[
593+
{
594+
"type": "tool_call_chunk",
595+
"id": "call_1",
596+
"name": "click",
597+
"args": "{}",
598+
"index": 0,
599+
}
600+
],
601+
response_metadata=metadata,
602+
)
603+
chunk_block = chunk.content_blocks[0]
604+
assert chunk_block["type"] == "tool_call_chunk"
605+
assert chunk_block["extras"]["toolset_name"] == "computer"

‎libs/core/tests/unit_tests/messages/test_ai.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,18 @@ def test_content_blocks() -> None:
423423
{"type": "server_tool_call", "name": "foo", "index": 0, "args": {"a": 1}}
424424
]
425425

426+
# Server tool calls with no input stream no args
427+
empty_args_chunk = AIMessageChunk(
428+
content=[
429+
{"type": "server_tool_call_chunk", "index": 0, "name": "foo", "args": ""}
430+
]
431+
) + AIMessageChunk(
432+
content=[], chunk_position="last", response_metadata={"output_version": "v1"}
433+
)
434+
assert empty_args_chunk.content == [
435+
{"type": "server_tool_call", "name": "foo", "index": 0, "args": {}}
436+
]
437+
426438
# Test non-standard + non-standard
427439
chunk_1 = AIMessageChunk(
428440
content=[

‎libs/partners/anthropic/README.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,27 @@ As an open-source project in a rapidly developing field, we are extremely open t
3131

3232
For detailed information on how to contribute, see the [Contributing Guide](https://docs.langchain.com/oss/python/contributing/overview).
3333

34+
## Migrating to Claude Sonnet 5.5
35+
36+
```python
37+
from langchain_anthropic import ChatAnthropic
38+
39+
model = ChatAnthropic(
40+
model="claude-sonnet-5-5",
41+
max_tokens=16000,
42+
output_config={"effort": "medium"},
43+
)
44+
```
45+
46+
- Use `with_structured_output(schema, method="json_schema")` for native structured output. Sonnet 5.5 rejects forced tool choice (`"any"` or a tool name). Function-calling structured output does not force a call and raises a parsing error if the model answers without one.
47+
- Thinking is adaptive by default. For no up-front thinking, use `thinking={"type": "between_tools"}` at `high` effort or below, with no additional thinking fields. `disabled` and budgeted `enabled` thinking are unsupported.
48+
- Omit sampling settings; non-default `temperature`, `top_p`, and `top_k` are rejected. Budget output tokens for both thinking and text.
49+
- Preserve signed thinking blocks, including empty ones, and keep history append-only. Use mid-conversation system messages to change instructions or tools rather than editing earlier turns.
50+
- Progress updates can arrive as thinking blocks. Use adaptive thinking with `display="summarized"` or `display="updates"` to display them; the latter's beta header is added automatically.
51+
- Computer use on the direct Claude API requires `computer_toolset_20260801`. Preserve the returned tool-use content: its `toolset_name` is retained on replay and copied to matching tool results. Remove the old fine-grained streaming beta when using toolsets.
52+
53+
See the [migration guide](https://platform.claude.com/docs/en/models/sonnet-5-5/migration-guide) for platform-specific restrictions, advisor pairings, and refusal/fallback behavior.
54+
3455
## Resources
3556

3657
- [LangChain Academy](https://academy.langchain.com/) — comprehensive, free courses on LangChain libraries and products, made by the LangChain team

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ def _convert_from_v1_to_anthropic(
129129
"input": block.get("args", {}),
130130
"id": block.get("id", ""),
131131
}
132-
if "caller" in block.get("extras", {}):
133-
tool_use_block["caller"] = block["extras"]["caller"]
132+
for key in ("caller", "toolset_name"):
133+
if key in block.get("extras", {}):
134+
tool_use_block[key] = block["extras"][key]
134135
new_content.append(tool_use_block)
135136

136137
elif block["type"] == "tool_call_chunk":
@@ -147,6 +148,11 @@ def _convert_from_v1_to_anthropic(
147148
"name": block.get("name", ""),
148149
"input": input_,
149150
"id": block.get("id", ""),
151+
**{
152+
key: block["extras"][key]
153+
for key in ("caller", "toolset_name")
154+
if key in block.get("extras", {})
155+
},
150156
}
151157
)
152158

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

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,7 @@ def _format_messages(
720720
"""Format messages for Anthropic's API."""
721721
system: str | list[dict] | None = None
722722
formatted_messages: list[dict] = []
723+
toolsets: dict[str, str] = {}
723724
merged_messages = _merge_messages(messages)
724725
last_non_system_index = max(
725726
(i for i, m in enumerate(merged_messages) if m.type != "system"),
@@ -795,11 +796,13 @@ def _format_messages(
795796
for tc in message.tool_calls
796797
if tc["id"] == block["id"]
797798
]
798-
content.extend(
799-
_lc_tool_calls_to_anthropic_tool_use_blocks(
800-
overlapping,
801-
),
799+
tool_blocks = _lc_tool_calls_to_anthropic_tool_use_blocks(
800+
overlapping,
802801
)
802+
if toolset_name := block.get("toolset_name"):
803+
for tool_block in tool_blocks:
804+
tool_block["toolset_name"] = toolset_name
805+
content.extend(tool_blocks)
803806
else:
804807
if tool_input := block.get("input"):
805808
args = tool_input
@@ -818,6 +821,8 @@ def _format_messages(
818821
)
819822
if caller := block.get("caller"):
820823
tool_use_block["caller"] = caller
824+
if toolset_name := block.get("toolset_name"):
825+
tool_use_block["toolset_name"] = toolset_name
821826
content.append(tool_use_block)
822827
elif block["type"] in ("server_tool_use", "mcp_tool_use"):
823828
formatted_block = {
@@ -944,6 +949,8 @@ def _format_messages(
944949
},
945950
),
946951
)
952+
elif block["type"] == "advisor_tool_result":
953+
content.append({k: v for k, v in block.items() if k != "index"})
947954
else:
948955
content.append(block)
949956
else:
@@ -1036,6 +1043,16 @@ def _format_messages(
10361043
system = _format_system_content(pending.content, model=model)
10371044
_warn_system_message_hoisted(model)
10381045
pending_system = []
1046+
if isinstance(content, list):
1047+
for block in content:
1048+
if not isinstance(block, dict):
1049+
continue
1050+
if block.get("type") == "tool_use" and block.get("toolset_name"):
1051+
toolsets[block["id"]] = block["toolset_name"]
1052+
elif block.get("type") == "tool_result" and (
1053+
toolset_name := toolsets.get(block.get("tool_use_id", ""))
1054+
):
1055+
block.setdefault("toolset_name", toolset_name)
10391056
formatted_messages.append({"role": role, "content": content})
10401057

10411058
formatted_messages.extend(
@@ -1136,13 +1153,16 @@ def _supports_mid_conversation_system_messages(model: object) -> bool:
11361153
"claude-mythos-5",
11371154
"claude-opus-4-8",
11381155
"claude-opus-5",
1156+
"claude-sonnet-5-5",
11391157
)
11401158
)
11411159

11421160

11431161
def _supports_forced_tool_choice(model: str) -> bool:
11441162
"""Return whether the model accepts `tool_choice` types `any` and `tool`."""
1145-
return not model.startswith(("claude-fable-5-1", "claude-opus-5-5"))
1163+
return not model.startswith(
1164+
("claude-fable-5-1", "claude-opus-5-5", "claude-sonnet-5-5")
1165+
)
11461166

11471167

11481168
def _is_direct_anthropic_llm_type(llm_type: object) -> bool:
@@ -1793,7 +1813,8 @@ def _assert_valid_model_configuration(self, kwargs: Mapping[str, Any]) -> None:
17931813
output_config["effort"] = effort
17941814

17951815
is_fable_model = self.model.startswith("claude-fable-5")
1796-
if is_fable_model:
1816+
is_sonnet_55 = self.model.startswith("claude-sonnet-5-5")
1817+
if is_fable_model or is_sonnet_55:
17971818
top_k = request_config.get("top_k", self.top_k)
17981819
top_p = request_config.get("top_p", self.top_p)
17991820
temperature = request_config.get("temperature", self.temperature)
@@ -1819,7 +1840,7 @@ def _assert_valid_model_configuration(self, kwargs: Mapping[str, Any]) -> None:
18191840
raise ValueError(msg)
18201841

18211842
if (
1822-
(self.model.startswith("claude-opus-5") or is_fable_model)
1843+
(self.model.startswith("claude-opus-5") or is_fable_model or is_sonnet_55)
18231844
and isinstance(thinking, Mapping)
18241845
and thinking.get("type") == "enabled"
18251846
):
@@ -2331,6 +2352,12 @@ def _make_message_chunk_from_anthropic_event(
23312352
warnings.warn("Received unexpected tool content block.", stacklevel=2)
23322353

23332354
content_block = event.content_block.model_dump()
2355+
if event.content_block.type == "advisor_tool_result":
2356+
content_block = {
2357+
key: content_block[key]
2358+
for key in ("type", "tool_use_id", "content", "cache_control")
2359+
if key in content_block
2360+
}
23342361
if "caller" in content_block and content_block["caller"] is None:
23352362
content_block.pop("caller")
23362363
content_block["index"] = event.index
@@ -2464,6 +2491,8 @@ def _make_message_chunk_from_anthropic_event(
24642491
"stop_reason": event.delta.stop_reason,
24652492
"stop_sequence": event.delta.stop_sequence,
24662493
}
2494+
if stop_details := event.delta.model_dump().get("stop_details"):
2495+
response_metadata["stop_details"] = stop_details
24672496
if context_management := getattr(event, "context_management", None):
24682497
response_metadata["context_management"] = (
24692498
context_management.model_dump()
@@ -2882,8 +2911,8 @@ def with_structured_output(
28822911
- `'function_calling'` (default): Use forced tool calling to get
28832912
structured output. When `thinking` is enabled, or on models
28842913
that don't support forced tool use (Claude Opus 5.5, Claude
2885-
Fable 5.1), the tool call isn't forced, and a missing tool
2886-
call raises `OutputParserException`.
2914+
Fable 5.1, Claude Sonnet 5.5), the tool call isn't forced,
2915+
and a missing tool call raises `OutputParserException`.
28872916
- `'json_schema'`: Use Claude's dedicated
28882917
[structured output](https://platform.claude.com/docs/en/build-with-claude/structured-outputs)
28892918
feature.
@@ -3238,6 +3267,7 @@ class _AnthropicToolUse(TypedDict):
32383267
input: dict
32393268
id: str
32403269
caller: NotRequired[dict[str, Any]]
3270+
toolset_name: NotRequired[str]
32413271

32423272

32433273
def _lc_tool_calls_to_anthropic_tool_use_blocks(

‎libs/partners/anthropic/langchain_anthropic/data/_profiles.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,4 +488,39 @@
488488
],
489489
"reasoning_effort_default": "high",
490490
},
491+
"claude-sonnet-5-5": {
492+
"name": "Claude Sonnet 5.5",
493+
"release_date": "2026-09-28",
494+
"last_updated": "2026-09-28",
495+
"open_weights": False,
496+
"max_input_tokens": 1000000,
497+
"max_output_tokens": 128000,
498+
"text_inputs": True,
499+
"image_inputs": True,
500+
"audio_inputs": False,
501+
"pdf_inputs": True,
502+
"video_inputs": False,
503+
"text_outputs": True,
504+
"image_outputs": False,
505+
"audio_outputs": False,
506+
"video_outputs": False,
507+
"reasoning_output": True,
508+
"tool_calling": True,
509+
"structured_output": True,
510+
"attachment": True,
511+
"temperature": False,
512+
"image_url_inputs": True,
513+
"pdf_tool_message": True,
514+
"image_tool_message": True,
515+
"tool_call_streaming": True,
516+
"tool_choice": False,
517+
"reasoning_effort_levels": [
518+
"low",
519+
"medium",
520+
"high",
521+
"xhigh",
522+
"max",
523+
],
524+
"reasoning_effort_default": "high",
525+
},
491526
}

‎libs/partners/anthropic/langchain_anthropic/data/profile_augmentations.toml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ structured_output = true
5353
reasoning_effort_levels = ["low", "medium", "high", "xhigh", "max"]
5454
reasoning_effort_default = "medium"
5555

56+
[overrides."claude-sonnet-5-5"]
57+
structured_output = true
58+
tool_choice = false
59+
reasoning_effort_levels = ["low", "medium", "high", "xhigh", "max"]
60+
reasoning_effort_default = "high"
61+
5662
[overrides."claude-sonnet-5"]
5763
structured_output = true
5864
reasoning_effort_levels = ["low", "medium", "high", "xhigh", "max"]

0 commit comments

Comments
 (0)