Map OpenAI response JSON decode errors to `ModelAPIError`
This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.
David has not reviewed this change. It was validated by a local repro against main, a mutation check on each regression test, and an automated pre-push review.
- Closes #8843
An OpenAI-compatible endpoint that answered 200 with a body that isn't valid JSON escaped Agent.run() as a raw json.JSONDecodeError, or as UnicodeDecodeError when the bytes weren't valid UTF-8. The openai SDK decodes the body inside _map_api_errors, which only mapped APIStatusError and APIConnectionError. So callers could not catch it as ModelAPIError, and FallbackModel's default fallback_on=(ModelAPIError,) never fell back.
- A new
_map_decode_errorsraisesModelAPIErrorchained from the decode error. It wraps only the SDK's own work: every Chat Completions and Responses call next to_map_api_errors, and the SDK's decoding of each streamed chunk, so a malformed SSE chunk is mapped too. Our own processing of a response still raises raw errors (a malformed MCPargumentsstring stays ajson.JSONDecodeError, pinned by a test). OpenAIResponsesModel.compact_messages(used byOpenAICompaction) maps errors inline rather than through_map_api_errors, so it gets the sameexceptclause.
New public surface: none.
What changes for existing users: a 200 body that can't be decoded as JSON now raises ModelAPIError instead of the raw decode error. FallbackModel falls back on it for non-streamed requests and for streams whose first chunk fails; a later malformed chunk propagates, like any mid-stream failure. Responses that decode are unaffected, and JSON our own code parses from a decoded response (such as MCP arguments) raises exactly as before.
Verification
tests/models/test_openai.py::test_non_json_response_body_raises_model_api_error,[response],[non-utf8]and[stream]cases.tests/models/test_fallback.py::test_fallback_tries_next_model_on_non_json_response_body: the second model answers.tests/models/test_openai_responses.py::test_openai_responses_compact_non_json_response_body_raises_model_api_error,[non-json]and[non-utf8]cases.tests/models/test_openai_responses.py::test_openai_responses_stream_non_json_chunk_raises_model_api_error: the Responses stream counterpart of[stream].tests/models/test_openai_responses.py::test_openai_responses_stream_mcp_call_invalid_arguments_is_not_mapped: decode errors from our own event processing stay unmapped.- Each fails when its production change is reverted (mutation-checked).
Not changed here (each reproduced on main)
- #8722: an error object inside the SSE stream escapes as the base
openai.APIError. Same helper, different exception. - A malformed MCP
argumentsstring in a Responses reply raisesjson.JSONDecodeErrorfrom_map_mcp_call, streamed or not, as before. - A 200 with a non-JSON
content-typeraisesUnexpectedModelBehaviorfromOpenAIChatModel, as before, butAttributeError: 'str' object has no attribute 'output'fromOpenAIResponsesModel. - The same blank 200 body still escapes as
json.JSONDecodeErrorfromOpenAIEmbeddingModel.embed,OpenAIImageGenerationModel.generate,AnthropicModelandGroqModel. Each has its own error mapping outside #8843's scope.
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
- PR title is fit for the release changelog.