Skip to content

fix(chat): json_object strips the fence on the native claude-* path (0.52.1) - #153

Merged
VickyXAI merged 2 commits into
mainfrom
fix/native-json-fence
Sep 20, 2026
Merged

VickyXAI merged 2 commits into
mainfrom
fix/native-json-fence

Conversation

@VickyXAI

Copy link
Copy Markdown
Contributor

What

response_format: "json_object" on claude-* models goes through the native /v1/messages path, 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.parse threw on the leading backtick. The tool description promised the half that failed.

Fix

stripJsonFence in src/tools/chat-anthropic.ts, applied to the answer text only when json_object is 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.native keeps the fence as evidence; thinking blocks untouched. Mirrors the gateway's stripJsonFence on /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_object verbatim, native keeps 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

VickyXAI and others added 2 commits September 21, 2026 04:21
…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]>
@VickyXAI
VickyXAI merged commit 3ca8812 into main Sep 20, 2026
3 checks passed
@VickyXAI
VickyXAI deleted the fix/native-json-fence branch September 20, 2026 19:24
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.

1 participant