Skip to content

Conversation

@coleleavitt
Copy link
Contributor

@coleleavitt coleleavitt commented Jan 12, 2026

Fixes #7889
Fixes #7715

Summary

  • Fix "Unexpected end of JSON input" error at client.gen.ts:167 when server returns empty response body

Root Cause

Two issues causing the JSON parsing error:

  1. Server-side: OPTIONS handler in /packages/console/app/src/routes/zen/v1/models.ts returned status: 200 with null body
  2. Client-side: SDK only handled empty responses with status: 204 or Content-Length: "0", but didn't handle empty body with 200 status and no Content-Length header

Changes

Server (packages/console/app/src/routes/zen/v1/models.ts)

  • Changed OPTIONS response from status: 200status: 204 (proper "No Content" status)

SDK Client (both v1 and v2 client.gen.ts)

  • Read response as text first, only parse JSON if non-empty
  • Added try-catch with contextual error messages for malformed JSON:
case "json": {
  const text = await response.text()
  if (!text) {
    data = {}
  } else {
    try {
      data = JSON.parse(text)
    } catch (parseError) {
      const errorMessage = parseError instanceof Error ? parseError.message : String(parseError)
      throw new Error(
        `Failed to parse JSON response: ${errorMessage}. Status: ${response.status}, URL: ${response.url}`
      )
    }
  }
  break
}

Comparison with #7618

Aspect #7618 This PR
Empty body handling Still crashes (better message) Returns {} gracefully
Malformed JSON Better error message Better error message
Server fix No Yes (204 status)
Lines added +64 +39

This PR prevents the crash entirely AND provides good error messages for actual malformed JSON.

Note

These files are auto-generated by @hey-api/openapi-ts. This fix will need to be reapplied after regeneration, or ideally contributed upstream to @hey-api/client-fetch.

@github-actions
Copy link
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions
Copy link
Contributor

The following comment was made by an LLM, it may be inaccurate:

Potential Duplicate Found:

PR #7618: fix(sdk): improve JSON parsing error handling with contextual messages
#7618

Why it's related: This PR also addresses JSON parsing errors in the SDK. Both PRs are fixing issues with JSON parsing in the SDK client, though PR #7618 appears to focus on error handling with contextual messages while PR #7888 (the current PR) specifically handles empty response bodies. These may be tackling related or overlapping issues in the same codebase area.

Fixes 'Unexpected end of JSON input' error when server returns empty body.

Root cause: OPTIONS handler at /zen/v1/models returned status 200 with null body,
but SDK only handled empty responses for status 204 or Content-Length: 0.

Changes:
- Server: OPTIONS handler now returns 204 (No Content) instead of 200
- SDK (v1 & v2): JSON parsing now reads text first and handles empty bodies
  by returning {} instead of failing on response.json()
@coleleavitt coleleavitt force-pushed the fix/json-parse-empty-response branch from 2ed9637 to 2b848ef Compare January 12, 2026 00:48
@coleleavitt
Copy link
Contributor Author

Created issue #7889 to track this bug. PR description updated with Fixes #7889.

After Anthropic OAuth login, sync.data.agent is empty while data loads.
This caused crashes when accessing local.agent.current().name before
agents were populated.

Changes:
- Make agentStore.current nullable with optional chaining on init
- Add fallback to first agent in current() when not found
- Add early return guards in submit(), cycle(), cycleFavorite(), set()
- Add null checks in highlight(), spinnerDef() memos
- Use optional chaining in JSX for agent name display
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant