SDK throws 'Unexpected end of JSON input' on empty response bodies #4790

Closed
opened 2026-02-16 17:45:26 -05:00 by yindo · 3 comments
Owner

Originally created by @coleleavitt on GitHub (Jan 11, 2026).

Originally assigned to: @thdxr on GitHub.

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

  1. Make an API call that returns an empty body with status 200
  2. SDK attempts to parse the empty body as JSON
  3. 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

  1. Client-side: SDK only checks for status === 204 or Content-Length === "0" before attempting JSON parse. Empty body with 200 status and no Content-Length header falls through to response.json() which throws.

  2. Server-side: OPTIONS handler in /packages/console/app/src/routes/zen/v1/models.ts returns status: 200 with null body instead of status: 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
Originally created by @coleleavitt on GitHub (Jan 11, 2026). Originally assigned to: @thdxr on GitHub. ## 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 1. Make an API call that returns an empty body with status 200 2. SDK attempts to parse the empty body as JSON 3. 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 1. **Client-side**: SDK only checks for `status === 204` or `Content-Length === "0"` before attempting JSON parse. Empty body with `200` status and no Content-Length header falls through to `response.json()` which throws. 2. **Server-side**: OPTIONS handler in `/packages/console/app/src/routes/zen/v1/models.ts` returns `status: 200` with null body instead of `status: 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
yindo closed this issue 2026-02-16 17:45:26 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Jan 11, 2026):

This issue might be a duplicate of existing issues. Please check:

  • #7715: Unexpected end of json

Feel free to ignore if none of these address your specific case.

@github-actions[bot] commented on GitHub (Jan 11, 2026): This issue might be a duplicate of existing issues. Please check: - #7715: Unexpected end of json Feel free to ignore if none of these address your specific case.
Author
Owner

@jimgrubb commented on GitHub (Jan 11, 2026):

Affected Model: Zen GLM-4.7
Reproduced: 2 times
Rate limit error before crash:
High concurrency usage of this API, please reduce concurrency or contact customer... retrying in 17s attempt #5
Additional context: The JSON parsing error occurred after seeing rate limiting messages. May be related to truncated/retry responses under load.

@jimgrubb commented on GitHub (Jan 11, 2026): **Affected Model:** Zen GLM-4.7 **Reproduced:** 2 times **Rate limit error before crash:** High concurrency usage of this API, please reduce concurrency or contact customer... retrying in 17s attempt #5 **Additional context:** The JSON parsing error occurred after seeing rate limiting messages. May be related to truncated/retry responses under load.
Author
Owner

@dhruvkej9 commented on GitHub (Jan 12, 2026):

@thdxr any easy temp. way to fix it ??

Have to restart opencode mid-session due to this.

@dhruvkej9 commented on GitHub (Jan 12, 2026): @thdxr any easy temp. way to fix it ?? Have to restart opencode mid-session due to this.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#4790