fix: strip markdown code block wrappers before JSON parsing#1032
Open
MaxwellCalkin wants to merge 1 commit intoItzCrazyKns:masterfrom
Open
fix: strip markdown code block wrappers before JSON parsing#1032MaxwellCalkin wants to merge 1 commit intoItzCrazyKns:masterfrom
MaxwellCalkin wants to merge 1 commit intoItzCrazyKns:masterfrom
Conversation
…teObject Claude and other models accessed via OpenAI-compatible proxies (LiteLLM, OpenRouter) wrap JSON output in markdown code blocks (```json ... ```), causing JSON.parse to fail with 'Unexpected token' errors. Add stripCodeBlockWrappers() to both OpenAI and Ollama LLM providers to remove these wrappers before passing response text to repairJson/JSON.parse. Fixes ItzCrazyKns#959
Contributor
There was a problem hiding this comment.
1 issue 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/openai/openaiLLM.ts">
<violation number="1" location="src/lib/models/providers/openai/openaiLLM.ts:30">
P2: Code-block stripping misses leading whitespace/newline before opening fence, so markdown-wrapped JSON can still fail to parse.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| */ | ||
| function stripCodeBlockWrappers(text: string): string { | ||
| return text | ||
| .replace(/^```(?:json)?\s*\n?/i, '') |
Contributor
There was a problem hiding this comment.
P2: Code-block stripping misses leading whitespace/newline before opening fence, so markdown-wrapped JSON can still fail to parse.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/lib/models/providers/openai/openaiLLM.ts, line 30:
<comment>Code-block stripping misses leading whitespace/newline before opening fence, so markdown-wrapped JSON can still fail to parse.</comment>
<file context>
@@ -20,6 +20,17 @@ import {
+ */
+function stripCodeBlockWrappers(text: string): string {
+ return text
+ .replace(/^```(?:json)?\s*\n?/i, '')
+ .replace(/\n?```\s*$/i, '');
+}
</file context>
Suggested change
| .replace(/^```(?:json)?\s*\n?/i, '') | |
| .replace(/^\s*```(?:json)?\s*\n?/i, '') |
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
After the v1.12.0 migration from LangChain to Vercel AI SDK, Claude models (and potentially others) fail with
SyntaxError: Unexpected tokenwhen accessed via OpenAI-compatible proxies like LiteLLM or OpenRouter. The root cause is that these models wrap JSON output in markdown code blocks (```json ... ```), andJSON.parsecannot handle the wrapper.Changes
stripCodeBlockWrappers()helper function to bothopenaiLLM.tsandollamaLLM.tsrepairJson/JSON.parseingenerateObject()(both providers)parse()instreamObject()final chunk handling (OpenAI provider)```json ... ```,``` ... ```, and surrounding whitespace/newlinesOpenAILLM, the fix covers all OpenAI-compatible providers automaticallyWhy not just rely on
repairJson({ extractJson: true })?While
repairJsonwithextractJson: trueis already used, it doesn't reliably handle the markdown code block wrapper pattern (```json\n{...}\n```), as evidenced by the bug report. Adding explicit stripping before repair ensures robustness.Test plan
claude-haiku-4.5) work via LiteLLM/OpenRouter without JSON parse errorsNote: This PR was authored by Claude (AI), operated by @MaxwellCalkin.
Summary by cubic
Fixes JSON parsing failures by stripping markdown code block fences from model responses before parsing. Prevents SyntaxError when Claude and other OpenAI-compatible providers return fenced JSON via proxies like LiteLLM or OpenRouter.
stripCodeBlockWrappers()inopenaiLLM.tsandollamaLLM.ts.repairJson/JSON.parseingenerateObject(both providers).parsefor final text chunk instreamObject(OpenAI provider).json ...and generic...with optional whitespace.OpenAILLM(e.g., Anthropic, Gemini, Groq, Lemonade, LM Studio).@toolsycc/json-repairwithextractJson: truefor robustness.Written for commit 49a5a74. Summary will update on new commits.