fix: strip markdown code blocks from LLM JSON responses in memory#4534
Open
hztBUAA wants to merge 1 commit intocrewAIInc:mainfrom
Open
fix: strip markdown code blocks from LLM JSON responses in memory#4534hztBUAA wants to merge 1 commit intocrewAIInc:mainfrom
hztBUAA wants to merge 1 commit intocrewAIInc:mainfrom
Conversation
|
You have run out of free Bugbot PR reviews for this billing cycle. This will reset on February 28. To receive reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial. |
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
When LLMs return JSON wrapped in markdown code blocks (```json ... ```), Pydantic validation fails because the surrounding markers are not valid JSON. This is common with many LLM providers. Add _strip_markdown_code_blocks() helper that removes triple-backtick wrappers before JSON parsing in Converter.to_pydantic(), convert_to_model(), and handle_partial_json(). The helper is applied at all JSON parsing entry points so markdown-wrapped responses are handled consistently. Fixes crewAIInc#4509
ee8e39f to
3b050f3
Compare
Author
|
Thanks for the review and feedback. I am following up on this PR now and will either push the requested changes or reply point-by-point shortly. |
Author
|
Quick follow-up: I am reviewing the feedback and will update this PR shortly. |
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 #4509
When LLMs return JSON wrapped in markdown code blocks (
```json ... ```), Pydantic validation fails withInvalid JSONerror during memory storage. This is a common behavior across many LLM providers._strip_markdown_code_blocks()helper that removes triple-backtick wrappers before JSON parsingconverter.py:Converter.to_pydantic()(both function-calling and non-function-calling paths),convert_to_model(), andhandle_partial_json()```json,```, and case-insensitive language tagsTest Plan
_strip_markdown_code_blocks()(json blocks, plain blocks, no blocks, multiline, empty string, plain text)convert_to_model()with markdown-wrapped JSONConverter.to_pydantic()with both function-calling and non-function-calling LLMsTaskEvaluationscenario from [BUG] Pydantic Validation error while saving in Memory #4509handle_partial_json()with markdown code blocks🤖 Generated with Claude Code