Fix Issue #1885: Extract JSON from Code Blocks and Handle Malformed JSON with json_repair #1978
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.
Description
This PR improves the robustness of JSON handling in
memory/main.py
by introducing two key enhancements:Fixes #1885 with Regular Expression Parsing: Extracts JSON from LLM responses wrapped in markdown code blocks (e.g.,
```json ... ```
). This addresses the issue where models often return JSON in such formats, ensuring accurate parsing and processing.Integrates
json_repair
: Replacesjson.loads
withjson_repair.loads
to fix minor JSON formatting errors (e.g., missing parentheses, commas, or added words). This prevents reprocessing entire LLM requests due to small JSON issues.The
json-repair
dependency has been added topyproject.toml
to support these improvements. These changes enhance the system’s stability and data integrity when handling dynamic JSON data.Why json_repair?
Some LLMs, even with structured output, occasionally produce JSON that isn't fully valid. Common mistakes include missing quotes, misplaced commas, or malformed arrays and objects. Although these errors are typically minor, they can break JSON parsing and force unnecessary retries of entire requests.
I initially searched for a lightweight Python package that could fix such issues reliably but couldn't find one. So I developed
json_repair
, which addresses:Incorporating this into mem0 improves the system's ability to handle edge cases where malformed JSON would otherwise lead to failed requests and retries.
Type of change
How Has This Been Tested?
Checklist: