AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): dream recombine dies on Anthropic temperature deprecation (#13342)
Nicholas Tindle00dd070e6659
diff --git a/autogpt_platform/backend/backend/util/llm/providers.py b/autogpt_platform/backend/backend/util/llm/providers.pyindex 155d0058ee..69acbf99f7 100644--- a/autogpt_platform/backend/backend/util/llm/providers.py+++ b/autogpt_platform/backend/backend/util/llm/providers.py@@ -101,6 +101,28 @@ ExecutionMode = Literal["sync", "batch", "flex"]# sync fallback with a log line._FLEX_SUPPORTED_PROVIDERS: set[str] = {"openai", "open_router"}+# Anthropic deprecated ``temperature`` on its newest model generation —+# the API rejects it outright with "`temperature` is deprecated for+# this model." (verified live: opus-4-7 and opus-4-8 reject;+# sonnet-4-6 accepts). Strip the param for known-rejecting families.+# The sync path additionally retries once without it on that exact+# error, so unknown future models self-heal; this list only matters+# for batch submissions, whose errors come back hours later in the+# result rows.+_ANTHROPIC_TEMPERATURE_DEPRECATED_PREFIXES = (+ "claude-opus-4-7",+ "claude-opus-4-8",+)+++def _anthropic_accepts_temperature(model: str) -> bool:+ return not model.startswith(_ANTHROPIC_TEMPERATURE_DEPRECATED_PREFIXES)+++def _is_temperature_deprecation_error(exc: anthropic.BadRequestError) -> bool:+ error_text = str(exc).lower()+ return "temperature" in error_text and "deprecated" in error_text+# ---------------------------------------------------------------------------# Result + submission carriers@@ -505,7 +527,7 @@ async def _call_anthropic_messages(tools=an_tools,timeout=timeout_seconds,)- if temperature is not None:+ if temperature is not None and _anthropic_accepts_temperature(model):create_kwargs["temperature"] = temperatureif tool_choice is not None:create_kwargs["tool_choice"] = tool_choice@@ -520,7 +542,21 @@ async def _call_anthropic_messages(}]- resp = await client.messages.create(**create_kwargs)+ try:+ resp = await client.messages.create(**create_kwargs)+ except anthropic.BadRequestError as exc:+ # Self-heal for models the deny-list doesn't know yet.+ if "temperature" not in create_kwargs or not (+ _is_temperature_deprecation_error(exc)+ ):+ raise+ logger.warning(+ "Anthropic model %s rejected temperature — retrying without it. "+ "Add the model to _ANTHROPIC_TEMPERATURE_DEPRECATED_PREFIXES.",+ model,+ )+ create_kwargs.pop("temperature")+ resp = await client.messages.create(**create_kwargs)if not resp.content:raise ValueError("No content returned from Anthropic.")@@ -1092,7 +1128,9 @@ async def _submit_anthropic_batch("messages": anth_messages,"max_tokens": max_tokens,}- if temperature is not None:+ # Batch errors surface hours later in the result rows — never submit+ # temperature to models that reject it (no cheap retry exists here).+ if temperature is not None and _anthropic_accepts_temperature(model):params["temperature"] = temperature# Only attach tools / tool_choice when the caller actually passed# them — Anthropic rejects empty arrays with HTTP 400.diff --git a/autogpt_platform/backend/backend/util/llm/providers_test.py b/autogpt_platform/backend/backend/util/llm/providers_test.pyindex a3a54b3804..3161518d1f 100644--- a/autogpt_platform/backend/backend/util/llm/providers_test.py+++ b/autogpt_platform/backend/backend/util/llm/providers_test.py@@ -13,6 +13,7 @@ from contextlib import contextmanagerfrom types import SimpleNamespacefrom unittest.mock import AsyncMock, MagicMock, patch+import anthropicimport pytestfrom backend.util.llm.providers import (@@ -1887,3 +1888,154 @@ class TestCancelBatch:provider="anthropic", provider_batch_id="b1", api_key="sk-test")assert ok is False+++class TestAnthropicTemperatureDeprecation:+ """Anthropic rejects ``temperature`` on its newest models with+ '`temperature` is deprecated for this model.' (verified live:+ claude-opus-4-7 and claude-opus-4-8 reject; sonnet-4-6 accepts).+ The dream pass's recombine phase died on this nightly — the param+ must be stripped for known-rejecting models, and the sync path+ must self-heal for unknown future ones."""++ def _fake_response(self):+ return SimpleNamespace(+ content=[SimpleNamespace(type="text", text="ok")],+ usage=SimpleNamespace(input_tokens=1, output_tokens=1),+ stop_reason="end_turn",+ )++ @pytest.mark.asyncio+ async def test_sync_omits_temperature_for_deprecating_model(self):+ async_create = AsyncMock(return_value=self._fake_response())+ fake_client = SimpleNamespace(messages=SimpleNamespace(create=async_create))+ with patch(+ "backend.util.llm.providers.anthropic.AsyncAnthropic",+ return_value=fake_client,+ ):+ await call_provider(+ provider="anthropic",+ model="claude-opus-4-7",+ api_key="sk-test",+ messages=[_msg("user", "hi")],+ max_tokens=10,+ temperature=0.9,+ )+ assert "temperature" not in async_create.call_args.kwargs++ @pytest.mark.asyncio+ async def test_sync_retries_without_temperature_on_deprecation_error(self):+ """Self-healing for future models the deny-list doesn't know:+ one retry without the param, same call otherwise."""+ import httpx++ err = anthropic.BadRequestError(+ message="`temperature` is deprecated for this model.",+ response=httpx.Response(+ 400, request=httpx.Request("POST", "https://api.anthropic.com")+ ),+ body={"error": {"message": "`temperature` is deprecated for this model."}},+ )+ async_create = AsyncMock(side_effect=[err, self._fake_response()])+ fake_client = SimpleNamespace(messages=SimpleNamespace(create=async_create))+ with patch(+ "backend.util.llm.providers.anthropic.AsyncAnthropic",+ return_value=fake_client,+ ):+ result = await call_provider(+ provider="anthropic",+ model="claude-sonnet-9-9",+ api_key="sk-test",+ messages=[_msg("user", "hi")],+ max_tokens=10,+ temperature=0.9,+ )+ assert isinstance(result, ProviderResponse)+ assert async_create.await_count == 2+ first, second = async_create.await_args_list+ assert first.kwargs["temperature"] == 0.9+ assert "temperature" not in second.kwargs++ @pytest.mark.asyncio+ async def test_sync_retry_detection_is_case_insensitive(self):+ """The deprecation message wording/casing isn't contractual — a+ future model returning 'Temperature ... Deprecated' must still+ trigger the self-healing retry, not fall through and fail."""+ import httpx++ err = anthropic.BadRequestError(+ message="Temperature is Deprecated for this model.",+ response=httpx.Response(+ 400, request=httpx.Request("POST", "https://api.anthropic.com")+ ),+ body={"error": {"message": "Temperature is Deprecated for this model."}},+ )+ async_create = AsyncMock(side_effect=[err, self._fake_response()])+ fake_client = SimpleNamespace(messages=SimpleNamespace(create=async_create))+ with patch(+ "backend.util.llm.providers.anthropic.AsyncAnthropic",+ return_value=fake_client,+ ):+ result = await call_provider(+ provider="anthropic",+ model="claude-sonnet-9-9",+ api_key="sk-test",+ messages=[_msg("user", "hi")],+ max_tokens=10,+ temperature=0.9,+ )+ assert isinstance(result, ProviderResponse)+ assert async_create.await_count == 2+ first, second = async_create.await_args_list+ assert first.kwargs["temperature"] == 0.9+ assert "temperature" not in second.kwargs++ @pytest.mark.asyncio+ async def test_batch_omits_temperature_for_deprecating_model(self):+ """Batch errors come back hours later in the result rows — the+ param must never be submitted for known-rejecting models."""+ fake_batch = SimpleNamespace(id="msgbatch_x")+ async_create = AsyncMock(return_value=fake_batch)+ fake_client = SimpleNamespace(+ messages=SimpleNamespace(batches=SimpleNamespace(create=async_create)),+ )+ with patch(+ "backend.util.llm.providers.anthropic.AsyncAnthropic",+ return_value=fake_client,+ ):+ await call_provider(+ provider="anthropic",+ model="claude-opus-4-7",+ api_key="sk-ant-test",+ messages=[_msg("user", "recombine this")],+ max_tokens=100,+ temperature=0.9,+ execution_mode="batch",+ custom_id="p1_recombine",+ )+ params = async_create.call_args.kwargs["requests"][0]["params"]+ assert "temperature" not in params++ @pytest.mark.asyncio+ async def test_batch_keeps_temperature_for_supporting_model(self):+ fake_batch = SimpleNamespace(id="msgbatch_y")+ async_create = AsyncMock(return_value=fake_batch)+ fake_client = SimpleNamespace(+ messages=SimpleNamespace(batches=SimpleNamespace(create=async_create)),+ )+ with patch(+ "backend.util.llm.providers.anthropic.AsyncAnthropic",+ return_value=fake_client,+ ):+ await call_provider(+ provider="anthropic",+ model="claude-sonnet-4-6",+ api_key="sk-ant-test",+ messages=[_msg("user", "consolidate this")],+ max_tokens=100,+ temperature=0.2,+ execution_mode="batch",+ custom_id="p1_consolidate",+ )+ params = async_create.call_args.kwargs["requests"][0]["params"]+ assert params["temperature"] == 0.2