fix: strip markdown code fences from AI response before JSON parsing#4693
Open
geoClink wants to merge 1 commit into
Open
fix: strip markdown code fences from AI response before JSON parsing#4693geoClink wants to merge 1 commit into
geoClink wants to merge 1 commit into
Conversation
- Added regex substitution to extract JSON content from response text in `RecipeViewSet` and `AiImportView`
Author
|
This is ready for review. The fix strips markdown code fences from the AI response before JSON parsing in both the aiproperties and AI import endpoints. If no code fence is present the string passes through unchanged, so existing OpenAI/Gemini providers are unaffected. |
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.
Bug
When using Anthropic Claude as an AI provider, the
aipropertiesendpoint crashes withJSONDecodeError: Expecting value: line 1 column 1 (char 0). Claude wraps its JSON response in a markdown code fence (json ...) which causesjson.loads()to fail since thefirst character is a backtick, not
{.Root Cause
response_format={"type": "json_object"}is passed to LiteLLM but the Anthropic backend does not enforce JSON-only responses the wayOpenAI does — Claude is free to return JSON wrapped in a markdown code fence.
Fix
Strip markdown code fences from the AI response before calling
json.loads()usingre.sub. If no code fence is present (e.g. OpenAI,Gemini) the string passes through unchanged.
The fix is applied in two places:
aiproperties)Unable to test locally without an Anthropic API key, but the fix follows the exact pattern suggested in the issue report.