From 55aea2be86ffd4d50059f2c4e9f1e348297527f0 Mon Sep 17 00:00:00 2001 From: Tanvir Alam Date: Tue, 22 Sep 2026 08:54:01 -0400 Subject: [PATCH] fix(anthropic): keep OpenAI messages valid after tool-result translation Anthropic tool-result continuations without a system prompt translated to assistant+tool only, which OpenAI-compatible providers reject with 400. Also default unrecognized SSE error types to 400 instead of synthetic 500. --- litellm/llms/anthropic/common_utils.py | 5 + .../adapters/transformation.py | 17 ++++ .../messages/streaming_iterator.py | 10 +- litellm/llms/anthropic/files/handler.py | 10 +- ...al_pass_through_adapters_transformation.py | 99 +++++++++++++++++++ .../messages/test_streaming_iterator.py | 3 +- 6 files changed, 138 insertions(+), 6 deletions(-) diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.py index 0c3c8996789..45d4bae2b08 100644 --- a/litellm/llms/anthropic/common_utils.py +++ b/litellm/llms/anthropic/common_utils.py @@ -62,11 +62,16 @@ ANTHROPIC_ERROR_STATUS_CODE_MAP: Final = MappingProxyType( "not_found_error": 404, "rate_limit_error": 429, "api_error": 500, + "internal_server_error": 500, "overloaded_error": 503, "timeout_error": 504, } ) +# Unknown Anthropic / OpenAI-compatible error `type` values default to 400 so a +# client-side/vendor 4xx is not escalated into a synthetic 500 (and cooldown). +ANTHROPIC_ERROR_STATUS_CODE_DEFAULT: Final = 400 + _BEDROCK_VERSION_SUFFIX_RE: Final = re.compile(r"-v\d+(?::\d+)?$") _INFERENCE_PROFILE_MINOR_RE: Final = re.compile(r":\d+$") _DATED_RELEASE_SUFFIX_RE: Final = re.compile(r"-\d{8}$") diff --git a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py index 85431a5a637..10a20d8f9ca 100644 --- a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py +++ b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py @@ -943,6 +943,21 @@ class LiteLLMAnthropicMessagesAdapter: text_parts.append(self._add_prompt_cache_breakpoint_if_present(block, text_obj)) return ChatCompletionSystemMessage(role="system", content=text_parts) if text_parts else None + def _ensure_openai_messages_have_user_or_system( + self, + messages: list[AllMessageValues], + ) -> list[AllMessageValues]: + """Ensure translated messages contain at least one user or system role. + + Anthropic allows a tool-result continuation with no system prompt and no + prior user text. That shape becomes ``assistant`` + ``tool`` only, which + OpenAI-compatible providers reject with HTTP 400. Inject an empty user + message so the request stays valid upstream (#42432). + """ + if any(m.get("role") in ("user", "system") for m in messages): + return messages + return [ChatCompletionUserMessage(role="user", content=""), *messages] + def _add_system_message_to_messages( self, new_messages: list[AllMessageValues], @@ -1243,6 +1258,8 @@ class LiteLLMAnthropicMessagesAdapter: ) ## ADD SYSTEM MESSAGE TO MESSAGES self._add_system_message_to_messages(new_messages, anthropic_message_request) + # OpenAI-compatible providers require at least one user/system role + new_messages = self._ensure_openai_messages_have_user_or_system(new_messages) new_kwargs: Final[ChatCompletionRequest] = { "model": anthropic_message_request["model"], diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/streaming_iterator.py b/litellm/llms/anthropic/experimental_pass_through/messages/streaming_iterator.py index 98c5c6d6d4e..12bd01cdd10 100644 --- a/litellm/llms/anthropic/experimental_pass_through/messages/streaming_iterator.py +++ b/litellm/llms/anthropic/experimental_pass_through/messages/streaming_iterator.py @@ -15,7 +15,10 @@ from litellm.constants import ( from litellm.litellm_core_utils.core_helpers import process_response_headers from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj from litellm.litellm_core_utils.logging_worker import GLOBAL_LOGGING_WORKER -from litellm.llms.anthropic.common_utils import ANTHROPIC_ERROR_STATUS_CODE_MAP +from litellm.llms.anthropic.common_utils import ( + ANTHROPIC_ERROR_STATUS_CODE_DEFAULT, + ANTHROPIC_ERROR_STATUS_CODE_MAP, +) from litellm.proxy.pass_through_endpoints.success_handler import ( PassThroughEndpointLogging, ) @@ -139,7 +142,8 @@ def parse_anthropic_error_event(chunk: object) -> tuple[str, str, int] | None: ``chunk`` is not an error event. The status code is looked up via ANTHROPIC_ERROR_STATUS_CODE_MAP, - defaulting to 500 for an error ``type`` Anthropic hasn't documented yet. + defaulting to 400 for an unrecognized error ``type`` so vendor/client + errors are not escalated into synthetic 500s (#42432). """ error_body: Final = _anthropic_error_body(chunk) if error_body is None: @@ -151,7 +155,7 @@ def parse_anthropic_error_event(chunk: object) -> tuple[str, str, int] | None: return ( error_type, message if isinstance(message, str) else error_type, - ANTHROPIC_ERROR_STATUS_CODE_MAP.get(error_type, 500), + ANTHROPIC_ERROR_STATUS_CODE_MAP.get(error_type, ANTHROPIC_ERROR_STATUS_CODE_DEFAULT), ) diff --git a/litellm/llms/anthropic/files/handler.py b/litellm/llms/anthropic/files/handler.py index e4c75a704ec..627d1100305 100644 --- a/litellm/llms/anthropic/files/handler.py +++ b/litellm/llms/anthropic/files/handler.py @@ -22,7 +22,11 @@ from litellm.types.llms.openai import ( from litellm.types.utils import CallTypes, LlmProviders, ModelResponse from ..chat.transformation import AnthropicConfig -from ..common_utils import ANTHROPIC_ERROR_STATUS_CODE_MAP, AnthropicModelInfo +from ..common_utils import ( + ANTHROPIC_ERROR_STATUS_CODE_DEFAULT, + ANTHROPIC_ERROR_STATUS_CODE_MAP, + AnthropicModelInfo, +) class AnthropicFilesHandler: @@ -221,7 +225,9 @@ class AnthropicFilesHandler: error_message = error_obj.get("message", "Unknown error") error_type = error_obj.get("type", "api_error") - status_code = ANTHROPIC_ERROR_STATUS_CODE_MAP.get(error_type, 500) + status_code = ANTHROPIC_ERROR_STATUS_CODE_MAP.get( + error_type, ANTHROPIC_ERROR_STATUS_CODE_DEFAULT + ) error_body_errored: OpenAIErrorBody = { "error": { diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py index d03174bc2c6..cdac837178f 100644 --- a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py +++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py @@ -5152,3 +5152,102 @@ def test_eager_input_streaming_tool_reaches_bedrock_converse_as_beta(): assert data["additionalModelRequestFields"]["anthropic_beta"] == ["fine-grained-tool-streaming-2025-05-14"] assert data["toolConfig"]["tools"][0]["toolSpec"]["inputSchema"]["json"] == EAGER_INPUT_SCHEMA + + +def test_translate_anthropic_to_openai_tool_result_only_continuation_has_user_role(): + """Regression for #42432: Anthropic tool-result continuation without a + system prompt must still yield a user/system role for OpenAI-compatible + providers (otherwise upstream returns 400 and LiteLLM may cool the + deployment down). + """ + req = { + "model": "openai/gpt-4o", + "max_tokens": 64, + "messages": [ + { + "role": "assistant", + "content": [ + { + "type": "tool_use", + "id": "toolu_01", + "name": "Read", + "input": {"path": "/tmp/x"}, + } + ], + }, + { + "role": "user", + "content": [ + { + "type": "tool_result", + "tool_use_id": "toolu_01", + "content": "file contents", + } + ], + }, + ], + } + + openai_req, _ = LiteLLMAnthropicMessagesAdapter().translate_anthropic_to_openai( + anthropic_message_request=req + ) + roles = [m["role"] for m in openai_req["messages"]] + assert "user" in roles or "system" in roles + assert roles == ["user", "assistant", "tool"] + assert openai_req["messages"][0] == {"role": "user", "content": ""} + + +def test_translate_anthropic_to_openai_empty_user_content_still_has_user_role(): + """Regression for #42432: user message with empty content list must not + produce an empty OpenAI messages array. + """ + req = { + "model": "openai/gpt-4o", + "max_tokens": 64, + "messages": [{"role": "user", "content": []}], + } + openai_req, _ = LiteLLMAnthropicMessagesAdapter().translate_anthropic_to_openai( + anthropic_message_request=req + ) + roles = [m["role"] for m in openai_req["messages"]] + assert roles == ["user"] + assert openai_req["messages"][0]["content"] == "" + + +def test_translate_anthropic_to_openai_with_system_keeps_existing_roles(): + """With a top-level system prompt, tool-result continuation already has + a system role — no extra empty user message should be injected. + """ + req = { + "model": "openai/gpt-4o", + "max_tokens": 64, + "system": "You are helpful.", + "messages": [ + { + "role": "assistant", + "content": [ + { + "type": "tool_use", + "id": "toolu_01", + "name": "Read", + "input": {"path": "/tmp/x"}, + } + ], + }, + { + "role": "user", + "content": [ + { + "type": "tool_result", + "tool_use_id": "toolu_01", + "content": "file contents", + } + ], + }, + ], + } + openai_req, _ = LiteLLMAnthropicMessagesAdapter().translate_anthropic_to_openai( + anthropic_message_request=req + ) + roles = [m["role"] for m in openai_req["messages"]] + assert roles == ["system", "assistant", "tool"] diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_streaming_iterator.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_streaming_iterator.py index 8043496f299..67d7e537d4e 100644 --- a/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_streaming_iterator.py +++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_streaming_iterator.py @@ -206,8 +206,9 @@ def test_parse_anthropic_error_event_from_sse_bytes(): def test_parse_anthropic_error_event_defaults_status_for_unknown_type(): + # Unknown vendor/client error types must not escalate to synthetic 500 (#42432). chunk = {"type": "error", "error": {"type": "some_future_error_type", "message": "?"}} - assert parse_anthropic_error_event(chunk) == ("some_future_error_type", "?", 500) + assert parse_anthropic_error_event(chunk) == ("some_future_error_type", "?", 400) def test_parse_anthropic_error_event_missing_message_falls_back_to_type():