feat: strip markdown code blocks from JSON responses#288
Closed
rothnic wants to merge 3 commits intodecolua:masterfrom
Closed
feat: strip markdown code blocks from JSON responses#288rothnic wants to merge 3 commits intodecolua:masterfrom
rothnic wants to merge 3 commits intodecolua:masterfrom
Conversation
…ders Providers like Antigravity maintain separate quota buckets per model family (e.g. Claude vs Gemini). A 429 on claude-opus previously locked the entire account, preventing gemini-pro requests even though its quota was full. This adds in-memory per-model locking so that only the specific model is skipped during account selection while other models remain accessible. Changes: - Add model-aware lock tracking in auth.js (Map<connectionId:model, expiry>) - Pass model context from chat handler to auth service - Multi-bucket behavior gated to known providers (MULTI_BUCKET_PROVIDERS set) - No database schema changes — locks are in-memory and clear on restart Closes decolua#110
When models (like kimi) return JSON wrapped in markdown code blocks (\`\`\`json...\`\`\`), strip the markers before sending to client. Changes: - Add markdown stripping to streaming text_delta responses - Add markdown stripping to non-streaming Claude format responses - Translate response_format to system prompts for JSON schema/object modes - Add jsonExtractor utility for consistent markdown removal This fixes AI_APICallError: Invalid JSON response when using AI SDK generateObject with kimi models through 9router.
This was referenced Mar 12, 2026
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
AI SDK
generateObjectfails with "Invalid JSON response" when using kimi models through 9router.Root Cause Investigation
Tested: Direct API calls to kimi with
response_format: {type: "json_object"}Findings:
AI_APICallError: Invalid JSON responsein AI SDK 5.xWhy kimi outputs markdown:
json_objectandjson_schemamodesRelated Work
This PR builds on previous response_format support:
Those PRs added the foundation for translating
response_formatto Claude-compatible system prompts. This PR adds the final layer: stripping markdown that some models (kimi, etc.) add despite explicit instructions.Solution
Strip markdown code block markers from JSON responses only when
response_formatis set.Implementation Details
Conditional Application:
Safety Measures
response_format.typeisjson_schemaorjson_objectjson,JSON,JsonvariationsWhat This Protects
✅ Strips: Code block wrappers in structured output mode
{"key": "value"} // Result after stripping❌ Preserves: Inline backticks in normal chat
config.jsonfile" stays intactauthheader should contain..." stays intactChanges
Files Modified
open-sse/translator/request/openai-to-claude.jsresponse_formatto Claude-compatible system promptsopen-sse/translator/response/claude-to-openai.jstext_deltachunksopen-sse/handlers/chatCore/nonStreamingHandler.jsopen-sse/utils/stream.js&jsonExtractor.js(new)Testing
Verified Scenarios
response_format: {type: "json_object"}→ markdown strippedresponse_format: {type: "json_schema", json_schema: {...}}→ markdown strippedTest Command
Before fix: Returns
\``json\n{"test": "value"}\n```**After fix:** Returns{"test": "value"}` ✓Global Application
These changes are conditionally applied based on request context:
Related
generateObject