From c0d79a6d1c787991bc68ee41cfea5c3cd0ca74d2 Mon Sep 17 00:00:00 2001 From: Alexsander Hamir Date: Wed, 21 Jan 2026 15:28:51 -0800 Subject: [PATCH] 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 4e3e1d54f517d663a903bc5240dff8cc14af2f68. Fixes test_context_window_exceeded_error_from_litellm_proxy test failure. --- litellm/exceptions.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/litellm/exceptions.py b/litellm/exceptions.py index 420df336893..f5fcded5134 100644 --- a/litellm/exceptions.py +++ b/litellm/exceptions.py @@ -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()