diff --git a/litellm/anthropic_interface/exceptions/__init__.py b/litellm/anthropic_interface/exceptions/__init__.py index 7f2de0e60dc..3f42f30b587 100644 --- a/litellm/anthropic_interface/exceptions/__init__.py +++ b/litellm/anthropic_interface/exceptions/__init__.py @@ -3,6 +3,7 @@ from .exception_mapping_utils import ( ANTHROPIC_ERROR_TYPE_MAP, AnthropicExceptionMapping, + anthropic_error_sse_frame, ) from .exceptions import ( AnthropicErrorDetail, @@ -16,4 +17,5 @@ __all__ = [ "AnthropicErrorResponse", "AnthropicErrorType", "AnthropicExceptionMapping", + "anthropic_error_sse_frame", ] diff --git a/litellm/anthropic_interface/exceptions/exception_mapping_utils.py b/litellm/anthropic_interface/exceptions/exception_mapping_utils.py index d9c9925275b..b6a8ffe40e7 100644 --- a/litellm/anthropic_interface/exceptions/exception_mapping_utils.py +++ b/litellm/anthropic_interface/exceptions/exception_mapping_utils.py @@ -4,6 +4,7 @@ Utilities for mapping exceptions to Anthropic error format. Similar to litellm/litellm_core_utils/exception_mapping_utils.py but for Anthropic response format. """ +import json from typing import Final from litellm.litellm_core_utils.safe_json_loads import safe_json_loads @@ -166,3 +167,16 @@ class AnthropicExceptionMapping: message=message, request_id=request_id, ) + + +def anthropic_error_sse_frame(status_code: int, raw_message: str) -> str: + """One `event: error` frame, for a stream that fails once the response headers are out. + + Anthropic clients pick stream events by the `event:` name, so a frame carrying only a `data:` + line is skipped and the failure never reaches the caller + """ + error_response: Final = AnthropicExceptionMapping.transform_to_anthropic_error( + status_code=status_code, + raw_message=raw_message, + ) + return f"event: error\ndata: {json.dumps(error_response)}\n\n" diff --git a/litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py b/litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py index 4486eb0985a..008a2230c83 100644 --- a/litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py +++ b/litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py @@ -68,15 +68,11 @@ def _error_status_and_message(exc: Exception) -> tuple[int, str]: def _mid_stream_error_sse_event(exc: Exception) -> bytes: from litellm.anthropic_interface.exceptions.exception_mapping_utils import ( - AnthropicExceptionMapping, + anthropic_error_sse_frame, ) status_code, message = _error_status_and_message(exc) - error_response = AnthropicExceptionMapping.transform_to_anthropic_error( - status_code=status_code, - raw_message=message, - ) - return f"event: error\ndata: {json.dumps(error_response)}\n\n".encode() + return anthropic_error_sse_frame(status_code=status_code, raw_message=message).encode() def _delta_payload_field(delta_type: StreamingContentBlockDeltaType) -> str: diff --git a/litellm/proxy/common_request_processing.py b/litellm/proxy/common_request_processing.py index 48f548faf03..6ff0e5a7d72 100644 --- a/litellm/proxy/common_request_processing.py +++ b/litellm/proxy/common_request_processing.py @@ -31,6 +31,7 @@ from starlette.types import Receive, Scope, Send import litellm from litellm._logging import redact_internal_details_from_client_message, verbose_proxy_logger from litellm._uuid import uuid +from litellm.anthropic_interface.exceptions import anthropic_error_sse_frame from litellm.constants import ( DD_TRACER_STREAMING_CHUNK_YIELD_RESOURCE, DEFAULT_MAX_RECURSE_DEPTH, @@ -71,7 +72,6 @@ from litellm.proxy.auth.auth_checks import ( tag_max_budget_check_for_tags, ) from litellm.proxy.auth.auth_utils import check_response_size_is_safe, get_request_route -from litellm.proxy.common_utils.anthropic_error_payload import anthropic_error_sse_frame from litellm.proxy.common_utils.callback_utils import ( get_logging_caching_headers, get_remaining_tokens_and_requests_from_request_data, @@ -3911,8 +3911,8 @@ class ProxyBaseLLMRequestProcessing: proxy_logging_obj=proxy_logging_obj, serialize_chunk=ProxyBaseLLMRequestProcessing._sse_chunk_serializer(restamper), serialize_error=lambda proxy_exc: anthropic_error_sse_frame( - message=proxy_exc.message, status_code=error_status_code(proxy_exc, status.HTTP_500_INTERNAL_SERVER_ERROR), + raw_message=proxy_exc.message, ), request=request, flush_tail=None if restamper is None else restamper.flush, diff --git a/litellm/proxy/common_utils/anthropic_error_payload.py b/litellm/proxy/common_utils/anthropic_error_payload.py deleted file mode 100644 index 1606e8df50e..00000000000 --- a/litellm/proxy/common_utils/anthropic_error_payload.py +++ /dev/null @@ -1,35 +0,0 @@ -"""The SSE error frame for a ``/v1/messages`` stream that fails once the response is already open. - -Anthropic clients pick stream events by the ``event:`` name, so a frame with only a ``data:`` line -is skipped and the failure never reaches the caller""" - -import json -from collections.abc import Mapping -from types import MappingProxyType -from typing import Final - -from fastapi import status - -from litellm.llms.anthropic.common_utils import ANTHROPIC_ERROR_STATUS_CODE_MAP - -_ANTHROPIC_ERROR_TYPE_BY_STATUS: Final[Mapping[int, str]] = MappingProxyType( - {status_code: error_type for error_type, status_code in ANTHROPIC_ERROR_STATUS_CODE_MAP.items()} -) - - -def anthropic_error_type(status_code: int) -> str: - """Anthropic only uses the types in the map above, so a status outside it is grouped the way - Anthropic groups them: client statuses are request errors, everything else is an API error""" - mapped: Final = _ANTHROPIC_ERROR_TYPE_BY_STATUS.get(status_code) - if mapped is not None: - return mapped - if status_code < status.HTTP_500_INTERNAL_SERVER_ERROR: - return "invalid_request_error" - return "api_error" - - -def anthropic_error_sse_frame(message: str, status_code: int) -> str: - """One error frame, ready to write into an open stream""" - error_type: Final = json.dumps(anthropic_error_type(status_code)) - body: Final = json.dumps(message) - return f'event: error\ndata: {{"type": "error", "error": {{"type": {error_type}, "message": {body}}}}}\n\n' diff --git a/tests/test_litellm/proxy/test_common_request_processing.py b/tests/test_litellm/proxy/test_common_request_processing.py index d32995e221d..506e7c68d48 100644 --- a/tests/test_litellm/proxy/test_common_request_processing.py +++ b/tests/test_litellm/proxy/test_common_request_processing.py @@ -9364,9 +9364,10 @@ class TestAnthropicMessagesStreamErrorFrame: "status_code, expected_error_type", [ (429, "rate_limit_error"), - (503, "overloaded_error"), + (529, "overloaded_error"), + (413, "request_too_large"), (500, "api_error"), - (529, "api_error"), + (502, "api_error"), (400, "invalid_request_error"), ], )