fix: strip markdown code fences before JSON parsing in structured output#1037
Open
VibhorGautam wants to merge 3 commits intoItzCrazyKns:masterfrom
Open
fix: strip markdown code fences before JSON parsing in structured output#1037VibhorGautam wants to merge 3 commits intoItzCrazyKns:masterfrom
VibhorGautam wants to merge 3 commits intoItzCrazyKns:masterfrom
Conversation
Some LLM providers (notably Claude via compatible APIs and certain Ollama models) wrap their structured output in markdown code fences like ```json ... ```. This causes JSON.parse and partial-json to fail with a parse error, breaking generateObject and streamObject calls. Added a stripMarkdownFences helper that removes these fences before the content reaches repairJson / parse. Applied to both OpenAI and Ollama provider implementations in generateObject() and streamObject(). Fixes ItzCrazyKns#959
Contributor
There was a problem hiding this comment.
2 issues found across 2 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/lib/models/providers/ollama/ollamaLLM.ts">
<violation number="1" location="src/lib/models/providers/ollama/ollamaLLM.ts:23">
P2: Fence stripping is too strict for streamed partials and misses common fence header variants, causing repeated parse failures / empty partial outputs.</violation>
</file>
<file name="src/lib/models/providers/openai/openaiLLM.ts">
<violation number="1" location="src/lib/models/providers/openai/openaiLLM.ts:272">
P2: Streaming partial parsing still breaks for fenced JSON because fence stripping only works after a closing fence is present, so most deltas parse as invalid and fall back to `{}`.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
The regex previously required a closing ``` to match, so during streaming the opening fence stayed in the text and broke partial JSON parsing. Now we also strip a leading-only fence when no closing fence is present yet.
Contributor
There was a problem hiding this comment.
1 issue found across 2 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/lib/models/providers/openai/openaiLLM.ts">
<violation number="1" location="src/lib/models/providers/openai/openaiLLM.ts:32">
P2: Regex change regressed fence stripping by requiring a newline after opening backticks, so same-line fenced JSON is no longer handled.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
The previous commit required \n after the opening backticks, which
missed cases like ```json{"key":"value"}``` where there's no newline.
Changed \n to \n? in both the full-pair and leading-only patterns.
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
Fixes #959
Some LLM providers (notably Claude via OpenAI-compatible APIs and certain Ollama models) wrap their structured output in markdown code fences:
This causes
JSON.parseandpartial-jsonto throw parse errors, breakinggenerateObject()andstreamObject()calls with these models.Changes
stripMarkdownFences()helper that removes```json ... ```wrappers before the content reachesrepairJson/parsegenerateObject()(full response) andstreamObject()(streaming partial + final)OpenAILLMso they inherit the fix automaticallyWhy
repairJson({ extractJson: true })isn't enoughThe existing
repairJsonwithextractJsonhandles some cases but doesn't reliably strip markdown fences in all edge cases — particularly when the fence includes a language tag (json) or has varying whitespace. The explicit strip before repair makes the pipeline more robust.How to test
npm install && npm run dev, open http://localhost:3000deepseek-r1)generateObject()in the classifierstripMarkdownFencesshould be a no-op when there are no fences