mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
fix(proxy): use the shared Anthropic error mapping for the stream error frame
The first pass added a third copy of the status to error-type table, and it disagreed with the documented one: 529 came out as `api_error` rather than `overloaded_error`, and 413 as `invalid_request_error` rather than `request_too_large`, which hides the two failures a client can actually act on. Drop that copy and put the frame builder next to the table litellm already keeps in anthropic_interface/exceptions. The bridged adapter path was building the same frame inline, so it uses the shared one now too
This commit is contained in:
parent
eeb40c20d0
commit
ff141b3840
6 changed files with 23 additions and 45 deletions
|
|
@ -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",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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'
|
||||
|
|
@ -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"),
|
||||
],
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue