mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
Merge d60cc56d5d into 60fc121ce8
This commit is contained in:
commit
821e16c391
2 changed files with 153 additions and 1 deletions
|
|
@ -277,6 +277,65 @@ class _ProviderHTTPException(Protocol):
|
|||
_ERROR_BODY_ADAPTER: Final = TypeAdapter(dict[str, object])
|
||||
|
||||
|
||||
def _validated_error_body(error_obj: _ProviderHTTPException) -> Mapping[str, object] | None:
|
||||
"""The exception's parsed error body as a dict, or None when it is not one."""
|
||||
raw_body: Final = attribute_or_none(error_obj, "body")
|
||||
if raw_body is None:
|
||||
return None
|
||||
try:
|
||||
validated: Final = _ERROR_BODY_ADAPTER.validate_python(raw_body)
|
||||
except ValidationError:
|
||||
return None
|
||||
return validated
|
||||
|
||||
|
||||
def _get_error_request_id(error_obj: _ProviderHTTPException) -> str | None:
|
||||
"""
|
||||
Return the provider request id carried in an error object's body, if any.
|
||||
|
||||
Checks `body["request_id"]` (a top-level sibling of `error`, e.g.
|
||||
`{"error": {...}, "request_id": "..."}` from DashScope/Bailian) and
|
||||
`body["error"]["request_id"]`.
|
||||
"""
|
||||
body: Final = _validated_error_body(error_obj)
|
||||
if body is None:
|
||||
return None
|
||||
|
||||
body_request_id: Final = body.get("request_id")
|
||||
if isinstance(body_request_id, str) and body_request_id:
|
||||
return body_request_id
|
||||
|
||||
nested_error: Final = body.get("error")
|
||||
if isinstance(nested_error, dict):
|
||||
nested_request_id: Final = nested_error.get("request_id")
|
||||
if isinstance(nested_request_id, str) and nested_request_id:
|
||||
return nested_request_id
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def _get_error_message_with_request_id(error_obj: _ProviderHTTPException) -> str | None:
|
||||
"""
|
||||
Extract the error message, preserving the provider request id when present.
|
||||
|
||||
Some providers (e.g. DashScope/Bailian) return the request id as a
|
||||
top-level sibling of `error` in the response body, not inside
|
||||
`error.message`. Extracting only the message drops it, leaving users
|
||||
without the provider request id needed to investigate the failure
|
||||
(https://github.com/BerriAI/litellm/issues/47839). Append it to the
|
||||
extracted message when it is available and not already present.
|
||||
"""
|
||||
extracted_message: Final = get_error_message(error_obj=error_obj)
|
||||
if extracted_message is None:
|
||||
return None
|
||||
|
||||
request_id: Final = _get_error_request_id(error_obj)
|
||||
if request_id is not None and request_id not in extracted_message:
|
||||
return f"{extracted_message} (request_id: {request_id})"
|
||||
|
||||
return extracted_message
|
||||
|
||||
|
||||
def _openai_cyber_policy_body(original_exception: _ProviderHTTPException) -> Mapping[str, object] | None:
|
||||
raw_body: Final[object] = getattr(original_exception, "body", None)
|
||||
try:
|
||||
|
|
@ -339,7 +398,7 @@ def _map_openai_exception(
|
|||
body=dict(cyber_policy_body),
|
||||
)
|
||||
# custom_llm_provider is openai, make it OpenAI
|
||||
message = get_error_message(error_obj=original_exception)
|
||||
message = _get_error_message_with_request_id(error_obj=original_exception)
|
||||
if message is None:
|
||||
if hasattr(original_exception, "message"):
|
||||
message = original_exception.message
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ from litellm.exceptions import GuardrailRaisedException
|
|||
from litellm.litellm_core_utils.exception_mapping_utils import (
|
||||
ExceptionCheckers,
|
||||
_get_body_error_code,
|
||||
_get_error_message_with_request_id,
|
||||
_get_response_headers,
|
||||
exception_type,
|
||||
extract_and_raise_litellm_exception,
|
||||
|
|
@ -576,6 +577,98 @@ def test_openai_compatible_429_still_maps_to_rate_limit():
|
|||
assert excinfo.value.status_code == 429
|
||||
|
||||
|
||||
class _FakeErrorObj:
|
||||
"""Minimal stand-in for an SDK exception carrying a parsed error body."""
|
||||
|
||||
def __init__(self, body=None, request_id=None):
|
||||
self.body = body
|
||||
if request_id is not None:
|
||||
self.request_id = request_id
|
||||
|
||||
|
||||
def test_error_message_with_request_id_appends_top_level_request_id():
|
||||
"""DashScope/Bailian returns `request_id` as a top-level sibling of
|
||||
`error` in the response body. Extracting only `error.message` dropped it
|
||||
from the final error text (issue #47839)."""
|
||||
error_obj = _FakeErrorObj(
|
||||
body={
|
||||
"error": {
|
||||
"message": "Incorrect API key provided.",
|
||||
"type": "invalid_request_error",
|
||||
"param": None,
|
||||
"code": "invalid_api_key",
|
||||
},
|
||||
"request_id": "343604ce-3795-9363-8be6-f4aa3f4613ee",
|
||||
}
|
||||
)
|
||||
|
||||
assert _get_error_message_with_request_id(error_obj=error_obj) == (
|
||||
"Incorrect API key provided. (request_id: 343604ce-3795-9363-8be6-f4aa3f4613ee)"
|
||||
)
|
||||
|
||||
|
||||
def test_error_message_with_request_id_appends_request_id_for_flat_body():
|
||||
error_obj = _FakeErrorObj(body={"message": "Something went wrong", "request_id": "req_flat_123"})
|
||||
|
||||
assert _get_error_message_with_request_id(error_obj=error_obj) == "Something went wrong (request_id: req_flat_123)"
|
||||
|
||||
|
||||
def test_error_message_with_request_id_appends_nested_error_request_id():
|
||||
error_obj = _FakeErrorObj(body={"error": {"message": "Bad request", "request_id": "req_nested_456"}})
|
||||
|
||||
assert _get_error_message_with_request_id(error_obj=error_obj) == "Bad request (request_id: req_nested_456)"
|
||||
|
||||
|
||||
def test_error_message_with_request_id_with_non_dict_body_returns_none():
|
||||
error_obj = _FakeErrorObj(body="not-a-dict")
|
||||
|
||||
assert _get_error_message_with_request_id(error_obj=error_obj) is None
|
||||
|
||||
|
||||
def test_error_message_with_request_id_without_request_id_is_unchanged():
|
||||
error_obj = _FakeErrorObj(body={"error": {"message": "Plain error", "code": "some_code"}})
|
||||
|
||||
assert _get_error_message_with_request_id(error_obj=error_obj) == "Plain error"
|
||||
|
||||
|
||||
def test_error_message_with_request_id_does_not_duplicate_request_id():
|
||||
error_obj = _FakeErrorObj(body={"message": "Failed (request_id: req_dup_1)", "request_id": "req_dup_1"})
|
||||
|
||||
assert _get_error_message_with_request_id(error_obj=error_obj) == "Failed (request_id: req_dup_1)"
|
||||
|
||||
|
||||
def test_dashscope_auth_error_message_includes_provider_request_id():
|
||||
"""End-to-end (issue #47839): a DashScope 401 through the
|
||||
OpenAI-compatible exception mapping must keep the provider request id
|
||||
in the final AuthenticationError message."""
|
||||
dashscope_body = {
|
||||
"error": {
|
||||
"message": "Incorrect API key provided. For details, see: "
|
||||
"https://help.aliyun.com/zh/model-studio/error-code#apikey-error",
|
||||
"type": "invalid_request_error",
|
||||
"param": None,
|
||||
"code": "invalid_api_key",
|
||||
},
|
||||
"request_id": "343604ce-3795-9363-8be6-f4aa3f4613ee",
|
||||
}
|
||||
original_exception = OpenAIError(
|
||||
status_code=401,
|
||||
message=f"Error code: 401 - {dashscope_body}",
|
||||
headers={},
|
||||
body=dashscope_body,
|
||||
)
|
||||
|
||||
with pytest.raises(litellm.AuthenticationError) as excinfo:
|
||||
exception_type(
|
||||
model="dashscope/deepseek-v4-flash",
|
||||
original_exception=original_exception,
|
||||
custom_llm_provider="dashscope",
|
||||
)
|
||||
|
||||
assert "343604ce-3795-9363-8be6-f4aa3f4613ee" in str(excinfo.value)
|
||||
assert "Incorrect API key provided" in str(excinfo.value)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"error_message",
|
||||
[
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue