Skip to content

fix: JSON-encode error body in non-streaming 500 handlers - #171

Merged
solderzzc merged 1 commit into
SharpAI:mainfrom
CodeAndCanvas728:fix/nonstreaming-error-json
Sep 24, 2026
Merged

solderzzc merged 1 commit into
SharpAI:mainfrom
CodeAndCanvas728:fix/nonstreaming-error-json

Conversation

@CodeAndCanvas728

Copy link
Copy Markdown
Contributor

Follow-up to #170, as discussed there.

Depends on #170 — this branch is stacked on it (reuses its JSON encoding). Until #170 merges, the diff here also shows #170's commits; the change for this PR is only the last commit, 1468068.

Problem

The non-streaming catch blocks for /v1/chat/completions and /v1/completions built the 500 body by string interpolation, replacing only " with '. A backslash or newline in the error text made the body invalid JSON.

Change

  • Moved the JSONSerialization body out of sseErrorChunk into a new errorJSON(_:). sseErrorChunk now wraps it in the SSE data: framing.
  • Both 500 handlers now return errorJSON(error). The status code, headers and fields (message / type / code) are the same as before.
  • Added ServerSSETests.testErrorJSONEncodesMessageAsValidJSON, which checks a message containing a quote, a backslash and a newline.

Testing

  • swift test: 0 failures, including the new test and the existing testErrorChunkEncodesMessageAsValidJSON.

🤖 Generated with Claude Code

The /v1/chat/completions and /v1/completions catch blocks built the
error body by string interpolation with only " replaced by ', so a
backslash or newline in the error text produced invalid JSON. Extract
the JSONSerialization body from sseErrorChunk into errorJSON(_:) and
use it in both handlers.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@solderzzc
solderzzc force-pushed the fix/nonstreaming-error-json branch from 1468068 to 765b990 Compare September 24, 2026 02:03
@solderzzc

Copy link
Copy Markdown
Member

Rebased this onto main now that #170 has merged, and force-pushed (765b990). Your commit is unchanged; it just no longer carries fe9da73's ~7k-token Test 38 prompt, which was crashing gemma-4-e2b on the CI runner (7.4 GB Metal malloc vs the 3.5 GB cap). main already has the ~2k-token version from #170.

Locally on Xcode 26.3: builds clean, 179/179 tests pass, including testErrorJSONEncodesMessageAsValidJSON.

@solderzzc
solderzzc merged commit 2862677 into SharpAI:main Sep 24, 2026
14 checks passed
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.

2 participants