Skip to content

Comments

Fix #4509: Strip markdown code-block fences from LLM JSON responses in Converter#4510

Open
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
devin/1771420355-fix-markdown-json-parsing
Open

Fix #4509: Strip markdown code-block fences from LLM JSON responses in Converter#4510
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
devin/1771420355-fix-markdown-json-parsing

Conversation

@devin-ai-integration
Copy link
Contributor

Fix #4509: Strip markdown code-block fences from LLM JSON responses

Summary

Some LLMs wrap their JSON responses in markdown code-block fences (```json ... ```), which causes model_validate_json() to fail with a Pydantic validation error. This manifests as "Failed to add to long term memory" errors when memory is enabled.

The fix adds a _strip_markdown_fences() helper that removes these delimiters before JSON parsing, applied in three locations within converter.py:

  • Converter.to_pydantic() — function-calling path
  • Converter.to_pydantic() — non-function-calling path
  • convert_to_model() — before json.loads()

Additionally, the function-calling path now has a handle_partial_json() fallback (previously only present in the non-function-calling path), and the except clauses now catch ValueError alongside ValidationError since model_validate_json can raise either.

11 new unit tests cover the helper function, both converter paths, the TaskEvaluation model, and convert_to_model.

Review & Testing Checklist for Human

  • Regex edge cases: The fence pattern requires a \n before the closing ```. Verify this matches all real-world LLM outputs (e.g., some models may omit the trailing newline before closing fences).
  • Fallback chain in function-calling path: When handle_partial_json is called with agent=None and regex extraction fails, it will fall through to convert_with_instructions() which raises TypeError. This is caught by the outer retry loop, but the error message could be confusing. Consider whether this needs a cleaner error path.
  • Widened except clause: (ValidationError, ValueError) — confirm no legitimate errors are being silently swallowed that should propagate.
  • End-to-end verification: Run the reproduction script from the issue with memory=True against a real LLM that wraps JSON in markdown fences, and confirm long-term memory saves succeed without errors.
  • Unused import: Entity is imported but unused in test_converter_to_pydantic_strips_fences_multiline_no_function_calling.

Notes

…n Converter

When LLMs return JSON wrapped in markdown code blocks (e.g. ```json...```),
the Converter.to_pydantic() method failed to parse the response, causing
Pydantic validation errors when saving to long-term memory.

Changes:
- Add _strip_markdown_fences() helper to remove markdown code-block delimiters
- Apply fence stripping before model_validate_json() in both function-calling
  and non-function-calling paths of Converter.to_pydantic()
- Add handle_partial_json() fallback in the function-calling path
- Apply fence stripping in convert_to_model() before json.loads()
- Add 11 tests covering the fix

Co-Authored-By: João <joao@crewai.com>
@devin-ai-integration
Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

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

0 participants