fix(chat): json_object strips the fence on the native claude-* path (0.52.1) - #153
Merged
Merged
Conversation
…as the description promised `response_format: "json_object"` is described as "force valid JSON output (no markdown fences). Works across all providers." On /v1/chat/completions the gateway does both halves — injects the instruction and unwraps the ```json block Claude puts round JSON regardless. /v1/messages has no response_format, so this repo injected the instruction itself and stopped there. Reproduced 2026-09-20 on claude-haiku-4.5: the instruction landed (keys went from unquoted to quoted, +24 input tokens), the fence stayed, and JSON.parse(response) threw on the leading backtick — on the models most likely to be asked. The strip unwraps a reply that IS one fenced block and nothing else: prose round a fence means the model ignored the instruction, and hiding that would hand back JSON that was never the whole answer. `native` in structuredContent keeps the fence; it is the upstream evidence. Thinking blocks are untouched — the alternative, routing JSON-mode claude-* through the compat path, would have traded the native thinking channel for a regex. Reported by a sibling session reading the gateway's two stripJsonFence call sites; neither is on the route this path uses. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
response_format: "json_object"onclaude-*models goes through the native/v1/messagespath, which injected the "no fences" instruction but never stripped the fence Claude adds anyway. Reproduced on claude-haiku-4.5 (2026-09-20): keys went from unquoted to quoted (+24 input tokens — the instruction landed), the fence stayed,JSON.parsethrew on the leading backtick. The tool description promised the half that failed.Fix
stripJsonFenceinsrc/tools/chat-anthropic.ts, applied to the answer text only whenjson_objectis set. Unwraps a reply that is one fenced block (any language tag, CRLF tolerant); leaves prose-around-a-fence alone so nothing is silently discarded;structuredContent.nativekeeps the fence as evidence; thinking blocks untouched. Mirrors the gateway'sstripJsonFenceon/v1/chat/completions, which this path never reaches.Option not taken: route JSON-mode
claude-*through the compat path — the gateway strips there, but it costs the native thinking channel.Tests
test/chat-anthropic-json-fence.test.ts— six cases, written first and watched fail (1 and 3 red, the fence still present), then green: verbatim reproduction, unfenced untouched, untagged + CRLF fence, prose-around-fence left alone, no-json_objectverbatim,nativekeeps the fence. Full suite 1318/1318; typecheck clean.Release
0.52.1 bump + CHANGELOG in the second commit. Nothing else changes.
🤖 Generated with Claude Code