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
Open
Fix #4509: Strip markdown code-block fences from LLM JSON responses in Converter#4510devin-ai-integration[bot] wants to merge 1 commit intomainfrom
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
Conversation
…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>
Contributor
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
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.
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 causesmodel_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 withinconverter.py:Converter.to_pydantic()— function-calling pathConverter.to_pydantic()— non-function-calling pathconvert_to_model()— beforejson.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 catchValueErroralongsideValidationErrorsincemodel_validate_jsoncan raise either.11 new unit tests cover the helper function, both converter paths, the
TaskEvaluationmodel, andconvert_to_model.Review & Testing Checklist for Human
\nbefore the closing```. Verify this matches all real-world LLM outputs (e.g., some models may omit the trailing newline before closing fences).handle_partial_jsonis called withagent=Noneand regex extraction fails, it will fall through toconvert_with_instructions()which raisesTypeError. This is caught by the outer retry loop, but the error message could be confusing. Consider whether this needs a cleaner error path.(ValidationError, ValueError)— confirm no legitimate errors are being silently swallowed that should propagate.memory=Trueagainst a real LLM that wraps JSON in markdown fences, and confirm long-term memory saves succeed without errors.Entityis imported but unused intest_converter_to_pydantic_strips_fences_multiline_no_function_calling.Notes