Repository navigation
Description
Summary
The README "Error Handling" section shows InferenceGatewayAPIError carrying the HTTP status and error body (e.status_code, e.response_data). With the default requests backend both are always lost: every HTTP error is raised with status_code=0 and response_data={}.
The root cause is the error handler in _make_request (inference_gateway/client.py lines 171-182). After response.raise_for_status() raises, it checks whether the response object is truthy:
- line 173:
error_data = response.json() if response and response.content else {} - line 177:
status_code = response.status_code if response else 0
requests.Response.__bool__ returns self.ok, which is False for every 4xx/5xx response, so both lines always fall back. httpx.Response defines no __bool__, so use_httpx=True is not affected. The tests do not catch this because they use Mock() responses, which are always truthy.
The three streaming methods (create_chat_completion_stream, create_response_stream, create_message_stream) have a related gap on both backends. They raise InferenceGatewayAPIError(f"Request failed: {str(e)}") without status_code or response_data (lines 479, 491, 696, 708, 1165, 1177). They also leave connection failures from self.session.post(...) / self.client.stream(...) unwrapped, so these escape as raw requests / httpx exceptions instead of InferenceGatewayError.
Steps to Reproduce
- Confirm the root cause offline:
r = requests.Response(); r.status_code = 404; bool(r)returnsFalse._make_requestchecks this value before it readsstatus_codeand the body. - Create a client with the default backend as the README does:
client = InferenceGatewayClient("http://localhost:8080/v1"). - Trigger any 4xx/5xx from the gateway, for example call
client.create_chat_completion(...)with an invalid token while gateway auth is enabled. - Catch the error as in the README "Error Handling" example and print
e.status_codeande.response_data. - Repeat steps 3-4 with
client.create_chat_completion_stream(...)(iterate the generator), then once more with the gateway stopped.
Expected Behavior
As the README "Error Handling" example shows, e.status_code holds the HTTP status (for example 401) and e.response_data holds the parsed error body. This should hold on both backends, for streaming and non-streaming calls. Connection failures during streaming calls should be raised as InferenceGatewayError, as they already are for non-streaming calls.
Actual Behavior
- Non-streaming calls on the default
requestsbackend:e.status_code == 0ande.response_data == {}for every HTTP error. The status only appears inside the message string (Request failed: 401 Client Error: ...). - Streaming calls on either backend:
e.status_code is Noneande.response_data is None. - Streaming calls with the gateway unreachable: a raw
requests.exceptions.ConnectionError/httpx.ConnectErroris raised, and the README'sexcept InferenceGatewayErrorbranch does not catch it.
Metadata
Metadata
Assignees
Type
Projects
- StatusShow more project fieldsDone
Activity
edenreich commentedon Sep 25, 2026
@infer Can you fix this?
inference-gateway-maintainer commentedon Sep 25, 2026
Model:
ollama_cloud/glm-5.3-flash· View JobTodos
Pull Request
#139
✅ Infer Result: Success
Fixed. PR: #139 (ready for review,
fix(client): carry HTTP status and error body into InferenceGatewayAPIError)Root cause and fix
requests.Response.__bool__returnsself.ok, so every 4xx/5xx response is falsy - the error handler in_make_requesttested truthiness before readingstatus_codeand the body, so they always fell back to0/{}.InferenceGatewayClient._api_error()which builds the error usingresponse is not None(not truthiness), so the real HTTP status and parsed JSON error body are carried on both backends; non-JSON bodies safely fall back to{}._make_requestnow delegates to it.create_chat_completion_stream,create_response_stream,create_message_stream) now raiseInferenceGatewayAPIErrorwithstatus_codeandresponse_dataon both backends (httpx error bodies are read viaresponse.read()inside the stream context before parsing), and connection failures fromsession.post()/client.stream()are wrapped asInferenceGatewayErrorinstead of escaping as rawrequests/httpxexceptions.Tests
requests.Responseobjects (Mock is always truthy, which is exactly why the old tests missed the bug): non-streaming and streaming on both backends assertstatus_code == 401and the parsed body, plus a non-JSON-body case and connection-failure cases for all three stream methods on both backends.Exception("API Error")stream scenario was replaced with a realisticrequests.exceptions.ConnectionError.task lint(black, isort, mypy) andtask testpass: 47 passed, 1 xfailed (pre-existing, unrelated).Tracking
Model:
ollama_cloud/glm-5.3-flash· Exit Code:0· Duration: 7m 56sTokens: 4,387,795 in · 36,113 out · 4,423,908 total (42 requests)
Tool calls: 53 total · 100% success rate
Traces
Stats
Triggered by edenreich · Infer Action · OSS
inference-gateway-releaser commentedon Sep 26, 2026
🎉 This issue has been resolved in version 0.21.1 🎉
The release is available on GitHub release
Your semantic-release bot 📦🚀