Parsing can fail during failure handling
An intermediary may return HTML, an empty body or a response cut off mid-string. Error handling that immediately calls response.json can throw another exception and lose the useful status. This is a transport robustness concern in your application.
What to do next
- 01
Capture metadata first
Record the HTTP status, content type and safe correlation context before consuming the body. Enforce an error-body size limit and avoid logging the entire response automatically, since it may contain reflected private input.
- 02
Parse with a fallback
Attempt structured error decoding only when appropriate, catching decoding failures separately. Return a stable application error with the known transport facts and a safe fallback message rather than a fabricated provider error code.
- 03
Keep acceptance uncertainty intact
If the affected operation could already have been accepted, preserve its existing recovery identity. A malformed error body alone does not authorize a new generation or establish that credits were returned.
A concrete example
Illustrative example: a gateway returns HTTP 503 with half of a JSON string. The client records 503 and body parse failure, then shows a safe service message. It does not replace the event with an unexplained SyntaxError.
Before you call it ready
- Empty and HTML bodies are tested.
- Original HTTP status survives parsing failure.
- Response text is bounded and redacted.
A locally executed, reproducible check
A complete JSON document containing an error and a truncated JSON document fail at different layers.
We executed this exact synthetic fixture on November 16, 2026 with Python 3.12.10 and SQLite 3.49.1. It ran locally with no network requests, account data or model generation. The recorded output below is an observation from that environment, not a platform benchmark. Copy the code into a separate Python file to repeat it in a suitable local environment; compare the mechanism and check your runtime version.
import json
bodies=['{"error":{"message":"synthetic rejection"}}','{"error":{"message":"unfinished']
for raw in bodies:
try:
parsed=json.loads(raw)
print('decoded error object:',isinstance(parsed.get('error'),dict))
except json.JSONDecodeError as error:
print('decode failed at character:',error.pos)
Recorded output and interpretation
The supplied bodies are local strings, with no HTTP status or service response. A parser failure alone does not determine whether a generation was accepted. Your client still needs status, a bounded body, task identity and the existing retry policy.
A matching local output establishes only the illustrated library or database behavior under these inputs. It does not certify a production integration. Preserve the approved action, authorization boundaries and current AISwapLab request contract when applying the idea.
decoded error object: True
decode failed at character: 20
When to stop or choose another method
Stop allowing a secondary JSON parse exception to erase the transport context needed to decide the next safe recovery action.
