fix: strip markdown code fences from LLM JSON responses (fixes #959)#1042
Open
willtwilson wants to merge 1 commit intoItzCrazyKns:masterfrom
Open
fix: strip markdown code fences from LLM JSON responses (fixes #959)#1042willtwilson wants to merge 1 commit intoItzCrazyKns:masterfrom
willtwilson wants to merge 1 commit intoItzCrazyKns:masterfrom
Conversation
Some LLM providers (Claude, models via LiteLLM/OpenRouter) wrap JSON output in markdown code fences (\\\json ... \\\). The streamObject() paths in both OpenAI and Ollama providers pass accumulated text directly to partial-json's parse(), which fails on the fence characters. Add stripMarkdownFences() and safeParseJson() utilities in src/lib/utils/parseJson.ts. Applied to: - streamObject() in OpenAI and Ollama providers (partial JSON parsing) - generateText() tool call argument parsing in OpenAI provider The existing generateObject() paths already use repairJson() with extractJson: true, which handles this case. Fixes ItzCrazyKns#959 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
No issues found across 3 files
Since this is your first cubic review, here's how it works:
- cubic automatically reviews your code and comments on bugs and improvements
- Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
- Add one-off context when rerunning by tagging
@cubic-dev-aiwith guidance or docs links (includingllms.txt) - Ask questions if you need clarification on any suggestion
There was a problem hiding this comment.
Pull request overview
Fixes a regression where some LLM providers return JSON wrapped in markdown code fences, causing partial-json / JSON.parse failures during streaming object parsing and tool-call argument parsing.
Changes:
- Added
stripMarkdownFences()andsafeParseJson()utilities to sanitize fenced JSON before parsing. - Applied fence stripping to
streamObject()parsing in the OpenAI and Ollama providers. - Applied safe JSON parsing to OpenAI
generateText()tool-call argument parsing.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/lib/utils/parseJson.ts | Adds shared helpers to strip markdown code fences and safely parse JSON. |
| src/lib/models/providers/openai/openaiLLM.ts | Uses fence stripping/safe parsing for streamed object parsing and tool-call argument parsing. |
| src/lib/models/providers/ollama/ollamaLLM.ts | Uses fence stripping for streamed object parsing. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
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.
Problem
Some LLM providers (Claude, models via LiteLLM/OpenRouter) wrap JSON output in markdown code fences:
\
\\json
{ "query": "...", "sources": [...] }
\
\\
The \streamObject()\ paths in both OpenAI and Ollama providers pass accumulated text directly to \partial-json's \parse(), which fails on the fence characters. This affects all users running Claude or any model behind LiteLLM/OpenRouter that wraps JSON in markdown.
Solution
Added \stripMarkdownFences()\ and \safeParseJson()\ utilities in \src/lib/utils/parseJson.ts\ that strip markdown code fences (\\json ... \\ and \\ ... \) before parsing. Applied to:
The existing \generateObject()\ paths already use
epairJson()\ with \�xtractJson: true, which handles this case, so those are left unchanged.
Testing
Tested with Claude 3.5 Sonnet via LiteLLM and OpenRouter — JSON responses with and without fences are now parsed correctly. The fix is a no-op for models that already return clean JSON.
Fixes #959
Summary by cubic
Strip markdown code fences from LLM JSON responses to prevent parse errors in OpenAI and Ollama streaming and OpenAI tool-call paths. Fixes #959.
stripMarkdownFences()andsafeParseJson()insrc/lib/utils/parseJson.ts.streamObject()for OpenAI and Ollama before callingparsefrompartial-json.generateText()tool-call arguments;generateObject()unchanged (already handled by@toolsycc/json-repairwithextractJson: true).Written for commit 19f4057. Summary will update on new commits.