Repository navigation
fix: read AI JSON with code fences inside strings - #53
Merged
Merged
Conversation
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
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.
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
createChatStepfound 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 wayJSON.parsewould and reports whether it is complete, cut off or invalid.readJsonObjectreturns 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.parseJsonResponsefallback 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:createChatStepretries 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: usesreadJsonObject.Cost
Tests
test/convex/ai/jsonResponse.test.tscovers:\'escapes;createChatStepattempt counts;Verification
bun run lint,typecheck,build,format:check,check:api,vitest run(2157 pass),git diff --check.🤖 Generated with Claude Code