encode/httpx · error · RuntimeError
Cannot call `raise_for_status` as the request instance has n
Error message
Cannot call `raise_for_status` as the request instance has not been set on this response.
What it means
Raised as `RuntimeError` at the top of `Response.raise_for_status()` when `self._request is None`. `raise_for_status` needs the originating Request to attach to the `HTTPStatusError` it raises, so it bails out early if the link is missing. As with error 40, this indicates a Response that was not produced through the normal send pipeline.
Source
Thrown at httpx/_models.py:800
# 302 (Uncacheable redirect. Method may change to GET.)
codes.FOUND,
# 303 (Client should make a GET or HEAD request.)
codes.SEE_OTHER,
# 307 (Equiv. 302, but retain method)
codes.TEMPORARY_REDIRECT,
# 308 (Equiv. 301, but retain method)
codes.PERMANENT_REDIRECT,
)
and "Location" in self.headers
)
def raise_for_status(self) -> Response:
"""
Raise the `HTTPStatusError` if one occurred.
"""
request = self._request
if request is None:
raise RuntimeError(
"Cannot call `raise_for_status` as the request "
"instance has not been set on this response."
)
if self.is_success:
return self
if self.has_redirect_location:
message = (
"{error_type} '{0.status_code} {0.reason_phrase}' for url '{0.url}'\n"
"Redirect location: '{0.headers[location]}'\n"
"For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/{0.status_code}"
)
else:
message = (
"{error_type} '{0.status_code} {0.reason_phrase}' for url '{0.url}'\n"
"For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/{0.status_code}"
)View on GitHub (pinned to b5addb64f0)
Solutions
- Construct the Response with a request: `httpx.Response(404, request=httpx.Request('GET', url))` before calling `raise_for_status()`.
- Use `httpx.MockTransport` to fabricate responses so the request link is populated automatically.
- Guard the call: `if getattr(response, '_request', None) is not None: response.raise_for_status()`.
- In custom transports, always set `response.request = request` before returning.
Example fix
// before
resp = httpx.Response(404)
resp.raise_for_status() # RuntimeError
// after
req = httpx.Request('GET', 'https://example.com/')
resp = httpx.Response(404, request=req)
resp.raise_for_status() # HTTPStatusError, as intended Defensive patterns
Strategy: validation
Validate before calling
def can_raise(resp: httpx.Response) -> bool:
return getattr(resp, '_request', None) is not None Type guard
import httpx
def response_has_request(resp: httpx.Response) -> bool:
return getattr(resp, '_request', None) is not None Try / catch
try:
response.raise_for_status()
except RuntimeError:
# no request attached - reattach or handle synthetically
response.request = httpx.Request('GET', 'https://example.com/')
response.raise_for_status() Prevention
- Always pass request= when constructing error Responses for tests.
- Use MockTransport so the request link is automatic.
- In custom transports, set response.request = request before returning.
When it happens
Trigger: Calling `httpx.Response(404).raise_for_status()` on a hand-built Response, or calling `raise_for_status()` on a Response returned by a custom transport that did not set `request=`.
Common situations: Tests with synthetic error responses; custom transports / mocks that emulate server errors without wiring the request; middleware that wraps and rebuilds Responses.
Related errors
- The request instance has not been set on this response.
- '.elapsed' may only be accessed after the response has been
- Attempted to access streaming response content, without havi
- Attempted to read or stream some content, but the content ha
- Attempted to read or stream content, but the stream has been
AI-assisted analysis of encode/httpx@b5addb64f0 (2026-08-04).
Data as JSON: /data/errors/aeb248bf4985e8bf.json.
Report an issue: GitHub.