fix(mem0-ts): extract content from code blocks instead of removing them#4026
Closed
hunterBhough wants to merge 1 commit intomem0ai:mainfrom
Closed
fix(mem0-ts): extract content from code blocks instead of removing them#4026hunterBhough wants to merge 1 commit intomem0ai:mainfrom
hunterBhough wants to merge 1 commit intomem0ai:mainfrom
Conversation
When an LLM wraps its JSON response in markdown code fences (e.g. ```json ... ```), the removeCodeBlocks function strips the fenced block entirely, leaving only any text outside the fences. The subsequent JSON.parse then fails because it receives the non-JSON explanation text instead of the actual JSON content. This is common with open-source/local models (Qwen, Llama, Mistral) which often wrap structured output in code fences even when response_format is set to json_object. The fix extracts the content inside the first code fence rather than deleting it. Falls back to the original strip behavior if no fenced block is found.
|
|
Contributor
|
Hey, a more comprehensive fix for this was included in #4044 which covers the code block extraction along with the two-stage JSON parsing. Going to close this in favor of that one — thanks for the PR though. |
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.
Problem
When an LLM wraps its JSON response in markdown code fences:
The
removeCodeBlocksfunction strips the fenced block entirely (text.replace(/\``[^\`]*```/g, "")), leaving only the explanation text. The subsequentJSON.parsethen fails withSyntaxError: Unexpected end of JSON input` because it receives:…instead of the actual JSON.
This silently breaks
memory.add()— facts are never extracted, and the response shows "No new memories extracted" even though the LLM produced valid output.Who this affects
Anyone using open-source/local models (Qwen, Llama, Mistral, Phi, etc.) which commonly wrap structured output in code fences even when
response_format: { type: "json_object" }is set. This is one of the most common behaviors with local inference servers (llama.cpp, vLLM, Ollama, TabbyAPI, MLX).Fix
When a code fence is present, extract its content rather than deleting it. Falls back to the original strip behavior if no fenced block is found.
Testing
Tested with Qwen2.5-32B via a local inference server. Before fix: all
memory.add()calls silently fail ("no new memories extracted"). After fix: facts are correctly extracted and stored.