fix(exception-mapping): preserve request IDs from raw provider errors

This commit is contained in:
Zhong Zhiyi 2026-11-03 11:04:27 +08:00
parent 03757815f5
commit c780fef1c2
2 changed files with 83 additions and 36 deletions

View file

@ -278,54 +278,43 @@ 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
def _body_request_id(raw_body: object) -> str | None:
try:
validated: Final = _ERROR_BODY_ADAPTER.validate_python(raw_body)
body: 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
nested_error: Final = as_str_mapping(body.get("error"))
if nested_error is None:
return None
nested_request_id: Final = nested_error.get("request_id")
return nested_request_id if isinstance(nested_request_id, str) and nested_request_id else None
return None
def _get_error_request_id(error_obj: _ProviderHTTPException) -> str | None:
body_request_id: Final = _body_request_id(attribute_or_none(error_obj, "body"))
if body_request_id is not None:
return body_request_id
exception_request_id: Final = attribute_or_none(error_obj, "request_id")
if isinstance(exception_request_id, str) and exception_request_id:
return exception_request_id
response: Final = attribute_or_none(error_obj, "response")
if not isinstance(response, httpx.Response):
return None
try:
response_body: Final = _ERROR_BODY_ADAPTER.validate_json(response.content)
except (ValidationError, httpx.ResponseNotRead):
return None
return _body_request_id(response_body)
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

View file

@ -9,6 +9,7 @@ import openai
import pytest
from fastapi import HTTPException
from openai import AsyncOpenAI, OpenAI
from pydantic import TypeAdapter
import litellm
from litellm import completion
@ -637,6 +638,63 @@ def test_error_message_with_request_id_does_not_duplicate_request_id():
assert _get_error_message_with_request_id(error_obj=error_obj) == "Failed (request_id: req_dup_1)"
def test_error_message_includes_the_exception_request_id() -> None:
request_id: Final = "provider-request-from-exception"
original_exception: Final = _FakeErrorObj(body={"message": "Rejected request"}, request_id=request_id)
assert _get_error_message_with_request_id(original_exception) == f"Rejected request (request_id: {request_id})"
@pytest.mark.parametrize("response_content", (b"not-json", b"[]", b'{"request_id": 42}', None))
def test_error_message_survives_a_response_without_a_readable_request_id(response_content: bytes | None) -> None:
request: Final = httpx.Request("POST", "https://provider.example/v1/chat/completions")
response: Final = (
httpx.Response(401, request=request, content=response_content)
if response_content is not None
else httpx.Response(401, request=request, stream=httpx.ByteStream(b"unread"))
)
original_exception: Final = OpenAIError(
status_code=401, message="Rejected request", body={"message": "Rejected request"}, response=response
)
assert _get_error_message_with_request_id(original_exception) == "Rejected request"
@pytest.mark.parametrize("include_request_id_header", (False, True))
def test_dashscope_auth_error_keeps_request_id_from_the_sdk_response(include_request_id_header: bool) -> None:
request_id: Final = "provider-request-from-response"
message: Final = "Incorrect API key provided."
provider_body: Final = {"error": {"message": message, "type": "invalid_request_error"}, "request_id": request_id}
def reject_request(request: httpx.Request) -> httpx.Response:
return httpx.Response(
401,
request=request,
json=provider_body,
headers={"x-request-id": request_id} if include_request_id_header else {},
)
with (
httpx.Client(transport=httpx.MockTransport(reject_request)) as http_client,
OpenAI(api_key="unit-test-credential", http_client=http_client, max_retries=0) as client,
pytest.raises(openai.AuthenticationError) as sdk_error,
):
client.chat.completions.create(model="test-model", messages=[{"role": "user", "content": "Hello"}])
original_exception: Final = OpenAIError(
status_code=sdk_error.value.status_code,
message=sdk_error.value.message,
response=sdk_error.value.response,
body=TypeAdapter(dict[str, object]).validate_python(sdk_error.value.body),
)
with pytest.raises(litellm.AuthenticationError) as mapped:
exception_type(model="test-model", original_exception=original_exception, custom_llm_provider="dashscope")
assert mapped.value.message.count(request_id) == 1
assert message in mapped.value.message
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