Skip to content

fix: strip markdown code blocks from LLM JSON responses in memory#4534

Open
hztBUAA wants to merge 1 commit intocrewAIInc:mainfrom
hztBUAA:fix/memory-json-parsing
Open

fix: strip markdown code blocks from LLM JSON responses in memory#4534
hztBUAA wants to merge 1 commit intocrewAIInc:mainfrom
hztBUAA:fix/memory-json-parsing

Conversation

@hztBUAA
Copy link

@hztBUAA hztBUAA commented Feb 20, 2026

Summary

Fixes #4509

When LLMs return JSON wrapped in markdown code blocks (```json ... ```), Pydantic validation fails with Invalid JSON error during memory storage. This is a common behavior across many LLM providers.

  • Added _strip_markdown_code_blocks() helper that removes triple-backtick wrappers before JSON parsing
  • Applied the helper at all JSON parsing entry points in converter.py: Converter.to_pydantic() (both function-calling and non-function-calling paths), convert_to_model(), and handle_partial_json()
  • Handles ```json, ```, and case-insensitive language tags

Test Plan

  • Added unit tests for _strip_markdown_code_blocks() (json blocks, plain blocks, no blocks, multiline, empty string, plain text)
  • Added integration tests for convert_to_model() with markdown-wrapped JSON
  • Added integration tests for Converter.to_pydantic() with both function-calling and non-function-calling LLMs
  • End-to-end test reproducing the exact TaskEvaluation scenario from [BUG] Pydantic Validation error while saving in Memory #4509
  • Integration test for handle_partial_json() with markdown code blocks
  • All 11 new tests pass; all existing tests continue to pass

🤖 Generated with Claude Code

@cursor
Copy link

cursor bot commented Feb 20, 2026

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.

@chatgpt-codex-connector
Copy link

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
@hztBUAA hztBUAA force-pushed the fix/memory-json-parsing branch from ee8e39f to 3b050f3 Compare February 20, 2026 04:47
@hztBUAA hztBUAA changed the title fix: strip markdown code block fences from LLM JSON responses fix: strip markdown code blocks from LLM JSON responses in memory Feb 20, 2026
@hztBUAA
Copy link
Author

hztBUAA commented Feb 25, 2026

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.

@hztBUAA
Copy link
Author

hztBUAA commented Feb 25, 2026

Quick follow-up: I am reviewing the feedback and will update this PR shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Pydantic Validation error while saving in Memory

1 participant