From 5e0ef36cf2f295146cf2cfcff1b958af46b754f1 Mon Sep 17 00:00:00 2001 From: chengyongru Date: Tue, 4 Aug 2026 10:50:26 +0800 Subject: [PATCH] fix(anthropic): preserve SDK and dated model compatibility --- nanobot/providers/anthropic_provider.py | 12 ++++++--- tests/providers/test_anthropic_thinking.py | 31 +++++++++++++--------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/nanobot/providers/anthropic_provider.py b/nanobot/providers/anthropic_provider.py index 0c3f3ebf2..2cd252846 100644 --- a/nanobot/providers/anthropic_provider.py +++ b/nanobot/providers/anthropic_provider.py @@ -32,7 +32,8 @@ def _gen_tool_id() -> str: _VALID_TOOL_ID = re.compile(r"^[a-zA-Z0-9_-]+$") _CLAUDE_MODEL_VERSION = re.compile( - r"claude-(?P[a-z]+)-(?P\d+)(?:-(?P\d+))?" + r"claude-(?P[a-z]+)-(?P\d+)" + r"(?:-(?P\d{1,2})(?=-|$))?" ) _ADAPTIVE_ONLY_MIN_VERSIONS = { "opus": (4, 7), @@ -617,8 +618,13 @@ class AnthropicProvider(LLMProvider): elif thinking_enabled and adaptive_only: # Newer Claude models removed manual token budgets. Their effort # control is independent from the adaptive thinking mode. - kwargs["thinking"] = {"type": "adaptive"} - kwargs["output_config"] = {"effort": reasoning_effort_lower} + # These fields were added as first-class SDK arguments after our + # minimum anthropic version; extra_body preserves wire compatibility + # across the declared dependency range. + kwargs["extra_body"] = { + "thinking": {"type": "adaptive"}, + "output_config": {"effort": reasoning_effort_lower}, + } elif thinking_enabled: budget_map = {"low": 1024, "medium": 4096, "high": max(8192, max_tokens)} budget = budget_map.get(reasoning_effort_lower, 4096) diff --git a/tests/providers/test_anthropic_thinking.py b/tests/providers/test_anthropic_thinking.py index 0f193c616..be36c20a9 100644 --- a/tests/providers/test_anthropic_thinking.py +++ b/tests/providers/test_anthropic_thinking.py @@ -82,8 +82,8 @@ def test_opus_4_7_omits_temperature_adaptive() -> None: def test_opus_4_7_high_uses_adaptive_effort() -> None: kw = _build(_make_provider("claude-opus-4-7"), "high", max_tokens=4096) assert "temperature" not in kw - assert kw["thinking"] == {"type": "adaptive"} - assert kw["output_config"] == {"effort": "high"} + assert kw["extra_body"]["thinking"] == {"type": "adaptive"} + assert kw["extra_body"]["output_config"] == {"effort": "high"} assert kw["max_tokens"] == 4096 @@ -102,8 +102,8 @@ def test_opus_4_8_omits_temperature_adaptive() -> None: def test_opus_4_8_high_uses_adaptive_effort() -> None: kw = _build(_make_provider("claude-opus-4-8"), "high", max_tokens=4096) assert "temperature" not in kw - assert kw["thinking"] == {"type": "adaptive"} - assert kw["output_config"] == {"effort": "high"} + assert kw["extra_body"]["thinking"] == {"type": "adaptive"} + assert kw["extra_body"]["output_config"] == {"effort": "high"} def test_opus_4_8_omits_temperature_none() -> None: @@ -119,8 +119,8 @@ def test_fable_omits_temperature_adaptive() -> None: def test_fable_high_uses_adaptive_effort() -> None: kw = _build(_make_provider("claude-fable-5"), "high", max_tokens=4096) assert "temperature" not in kw - assert kw["thinking"] == {"type": "adaptive"} - assert kw["output_config"] == {"effort": "high"} + assert kw["extra_body"]["thinking"] == {"type": "adaptive"} + assert kw["extra_body"]["output_config"] == {"effort": "high"} def test_fable_omits_temperature_none() -> None: @@ -137,8 +137,8 @@ def test_sonnet_5_omits_temperature_adaptive() -> None: def test_sonnet_5_high_uses_adaptive_effort() -> None: kw = _build(_make_provider("claude-sonnet-5"), "high", max_tokens=4096) assert "temperature" not in kw - assert kw["thinking"] == {"type": "adaptive"} - assert kw["output_config"] == {"effort": "high"} + assert kw["extra_body"]["thinking"] == {"type": "adaptive"} + assert kw["extra_body"]["output_config"] == {"effort": "high"} def test_sonnet_5_omits_temperature_none() -> None: @@ -156,16 +156,23 @@ def test_opus_5_omits_temperature(reasoning_effort: str | None) -> None: @pytest.mark.parametrize("reasoning_effort", ["low", "medium", "high", "xhigh", "max"]) def test_opus_5_uses_adaptive_thinking_with_effort(reasoning_effort: str) -> None: kw = _build(_make_provider("claude-opus-5"), reasoning_effort, max_tokens=4096) - assert kw["thinking"] == {"type": "adaptive"} - assert kw["output_config"] == {"effort": reasoning_effort} + assert kw["extra_body"]["thinking"] == {"type": "adaptive"} + assert kw["extra_body"]["output_config"] == {"effort": reasoning_effort} assert kw["max_tokens"] == 4096 def test_dated_opus_5_model_uses_family_capabilities() -> None: kw = _build(_make_provider("claude-opus-5-20260724"), "medium") assert "temperature" not in kw - assert kw["thinking"] == {"type": "adaptive"} - assert kw["output_config"] == {"effort": "medium"} + assert kw["extra_body"]["thinking"] == {"type": "adaptive"} + assert kw["extra_body"]["output_config"] == {"effort": "medium"} + + +def test_dated_opus_4_model_does_not_treat_date_as_minor_version() -> None: + kw = _build(_make_provider("claude-opus-4-20250514"), "high") + assert kw["temperature"] == 1.0 + assert kw["thinking"] == {"type": "enabled", "budget_tokens": 8192} + assert "extra_body" not in kw def test_ordinary_model_sends_temperature() -> None: