fix: validate response has request before using in BadRequestError

Previously, BadRequestError would use any passed httpx.Response object
directly. However, httpx.Response objects require a request attribute
to be set. When a response without a request was passed (e.g., in tests),
it would cause a RuntimeError: 'The request instance has not been set
on this response.'

Now we validate that the response has a valid request attribute before
using it. If not, we fall back to the cached minimal error response,
matching the behavior before the optimization commit 4e3e1d54f5.

Fixes test_context_window_exceeded_error_from_litellm_proxy test failure.
This commit is contained in:
Alexsander Hamir 2026-01-21 15:28:51 -08:00
parent 4e3e1d54f5
commit c0d79a6d1c

View file

@ -142,7 +142,12 @@ class BadRequestError(openai.BadRequestError): # type: ignore
self.litellm_debug_info = litellm_debug_info
self.max_retries = max_retries
self.num_retries = num_retries
if response is not None and isinstance(response, httpx.Response):
if (
response is not None
and isinstance(response, httpx.Response)
and hasattr(response, "request")
and response.request is not None
):
self.response = response
else:
self.response = _get_minimal_error_response()