opencode
opencode copied to clipboard
SDK throws 'Unexpected end of JSON input' on empty response bodies
Bug Description
The SDK crashes with SyntaxError: Unexpected end of JSON input when the server returns an empty response body without proper Content-Length: 0 header or 204 status code.
Steps to Reproduce
- Make an API call that returns an empty body with status 200
- SDK attempts to parse the empty body as JSON
- Crashes at
client.gen.ts:167
Error Message
SyntaxError: Unexpected end of JSON input
at json (unknown)
at <anonymous> (../sdk/js/src/v2/gen/client/client.gen.ts:167:33)
Expected Behavior
- Empty responses should be handled gracefully (return
{}) - Malformed JSON should provide actionable error messages with status code and URL
Root Cause
-
Client-side: SDK only checks for
status === 204orContent-Length === "0"before attempting JSON parse. Empty body with200status and no Content-Length header falls through toresponse.json()which throws. -
Server-side: OPTIONS handler in
/packages/console/app/src/routes/zen/v1/models.tsreturnsstatus: 200with null body instead ofstatus: 204.
Proposed Fix
- Read response as text first, only parse if non-empty
- Add try-catch with contextual error messages for malformed JSON
- Fix server to return proper 204 status for empty responses
Related Issues
- #7715 - Same bug report (duplicate)
- #7618 - Similar fix attempt (error messages only, doesn't prevent the crash)
Not Related (different root causes)
- #2188, #3034, #5890, #7692 - These are about model-generated malformed JSON, not SDK response parsing