Repository navigation
types: don't crash ResponseError on non-object JSON error bodies - #788
Open
AP24110011695 wants to merge 1 commit into
Open
AP24110011695 wants to merge 1 commit into
AP24110011695 wants to merge 1 commit into
Conversation
A non-2xx response whose body is valid JSON but not an object (null, a list, a quoted string, a number - common for gateway/proxy error pages) crashed ResponseError construction with AttributeError instead of raising the error: contextlib.suppress only guarded JSONDecodeError, so json.loads(...).get() blew up on non-dict values. Only extract 'error' when the parsed body is a dict; every other JSON shape falls back to the raw body, matching the existing non-JSON fallback. Regression tests cover both client paths with dict, null, list, and string bodies.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A non-2xx response whose body is valid JSON but not an object crashes
ResponseErrorconstruction withAttributeErrorinstead of raising the error:ResponseError.__init__wrapsjson.loads(error).get('error', error)incontextlib.suppress(json.JSONDecodeError), which only guards the parse step — calling.get()on a parsednull/ list / string / number raisesAttributeErrorthat escapes the suppress. Verified onmain(35d9667) by constructingResponseError('null', 502)and by driving both clients against a local HTTP server returning 502 with such bodies.The official server always answers with
{"error": ...}, but this client is pointed at many Ollama-compatible backends, gateways and proxies whose error pages are frequently bare JSONnull/ arrays / strings. The crash masks the real HTTP failure (status code and body) and breaksexcept ollama.ResponseErrorhandling in user code.Fix
Only extract
errorwhen the parsed body is adict; every other JSON shape falls back to the raw body — exactly the existing fallback for non-JSON bodies. Dict-body behavior (including the missing-error-key fallback) is unchanged.Tests
Parametrized regression tests in
tests/test_client.pyover a real local HTTP server returning 502, covering dict,null,[], and quoted-string bodies through both client paths:test_client_response_error_json_body_shapestest_async_client_response_error_json_body_shapesOn
main(35d9667) the six non-dict cases fail withAttributeErrorand the two dict cases pass; with this change all 8 pass. Fulltests/test_client.py+tests/test_type_serialization.py+tests/test_systemone.py: 107 passed, no new failures (the 8 Image/create_blobfailures present locally fail identically on cleanmainon this Windows machine and are unrelated).ruff check+ruff format --checkclean on both touched files.