Skip to content

fix(mem0-ts): extract content from code blocks instead of removing them#4026

Closed
hunterBhough wants to merge 1 commit intomem0ai:mainfrom
hunterBhough:fix/remove-code-blocks-extraction
Closed

fix(mem0-ts): extract content from code blocks instead of removing them#4026
hunterBhough wants to merge 1 commit intomem0ai:mainfrom
hunterBhough:fix/remove-code-blocks-extraction

Conversation

@hunterBhough
Copy link
Copy Markdown

Problem

When an LLM wraps its JSON response in markdown code fences:

\`\`\`json
{"facts": ["The sky is blue."]}
\`\`\`

Explanation: I extracted one fact from the input.

The removeCodeBlocks function strips the fenced block entirely (text.replace(/\``[^\`]*```/g, "")), leaving only the explanation text. The subsequent JSON.parsethen fails withSyntaxError: Unexpected end of JSON input` because it receives:

Explanation: I extracted one fact from the input.

…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.

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.
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@kartik-mem0 kartik-mem0 added the stale No activity, will close label Mar 26, 2026
@kartik-mem0
Copy link
Copy Markdown
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.

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

Labels

stale No activity, will close

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants