Skip to content

fix: read AI JSON with code fences inside strings - #53

Merged
hyochan merged 1 commit into
mainfrom
fix/format-json-extraction
Nov 24, 2026
Merged

hyochan merged 1 commit into
mainfrom
fix/format-json-extraction

Conversation

@hyochan

@hyochan hyochan commented Nov 24, 2026

Copy link
Copy Markdown
Member

Summary

Production issue formatting failed twice with analyze_formatting: AI response JSON appears incomplete (flutter_calendar_carousel #436, react-native-nitro-sound #871). Each failure took three billed attempts, about $0.19 in total.

The completeness check in createChatStep found a fenced answer's JSON with a non-greedy fence match. When the formatted body itself contained a Markdown code fence, the JSON was cut at that inner fence and judged incomplete, so every retry failed the same way. Learning extraction used the same pattern.

Changes

  • convex/_ai/utils.ts: one small JSON prefix reader, readJsonPrefix, reads a response's object the way JSON.parse would and reports whether it is complete, cut off or invalid.
    • readJsonObject returns the object of a response. Fences, prose and examples around the object are ignored, and code fences inside JSON strings stay intact.
    • isJsonResponseCutOff: whether asking again could help.
    • The parseJsonResponse fallback repairs only a cut-off object. It keeps finished entries, or a string value cut midway, and refuses complete but invalid JSON instead of turning it into a partial object.
  • convex/_ai/workflow.ts: createChatStep retries only a missing or cut-off object. Complete but invalid JSON is passed through to the parse step as before, because every attempt is a billed request with its own Convex billing mutations.
  • convex/_ai/learning/extractLearning.ts: uses readJsonObject.

Cost

  • No new Convex queries, mutations, actions, scheduled work or reads.
  • A comparison against the previous code (45,620 mutated inputs, plus cut-point sweeps and named cases) found:
    • no complete-but-invalid response takes more attempts than before;
    • no valid response is parsed differently;
    • the only extra attempts are on responses that really were cut off, which the old check judged complete and then returned as truncated data;
    • fenced responses with inner fences now succeed on the first attempt instead of failing after three.
  • Performance: 5,000 levels of nesting take about 0.2 s, and a 2.6 MB response about 0.1 s.

Tests

test/convex/ai/jsonResponse.test.ts covers:

  • fenced, bare-fenced, prose-wrapped and CRLF responses with inner fences;
  • prose, quotes and examples after the object;
  • \' escapes;
  • repair of cut-off objects, including dangling keys and strings that hold braces;
  • refusal of invalid JSON (trailing or missing commas, mismatched closers, unescaped quotes);
  • the createChatStep attempt counts;
  • learning extraction.

Verification

bun run lint, typecheck, build, format:check, check:api, vitest run (2157 pass), git diff --check.

🤖 Generated with Claude Code

Issue formatting failed after three billed attempts whenever its JSON held
a Markdown code fence: the completeness check cut the response at the first
inner fence. Learning extraction had the same pattern.

- One JSON prefix reader in convex/_ai/utils.ts says whether a response's
  object is complete, cut off or invalid; readJsonObject,
  isJsonResponseCutOff and parseJsonResponse share it
- createChatStep retries only a cut-off or missing object, never complete
  but invalid JSON, so no response takes more billed attempts than before
- Repair keeps finished entries and never turns invalid JSON into a
  partial object
@hyochan hyochan added the 🐛 bug Something isn't working label Nov 24, 2026

@gitdogai gitdogai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found in b289684

Approved because no issues were found in fully reviewed changes. Tests were not run. Gitdog

@hyochan
hyochan merged commit ca0a609 into main Nov 24, 2026
7 checks passed
@hyochan
hyochan deleted the fix/format-json-extraction branch November 24, 2026 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐛 bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant