🐛 Describe the bug
Bug Description
removeCodeBlocks uses a regex that requires both opening and closing backtick fences to match:
return text.replace(/```(?:\w+)?\n?([\s\S]*?)```/g, "$1").trim();
When the LLM returns a very long JSON response wrapped in ```json ``` that gets truncated (no closing fence arrives), the regex does not match. The raw text — starting with ```json — is passed directly to JSON.parse, causing a crash.
Error
Failed to parse memory actions from LLM response: ```json
{
"memory": [
...
{
"id": SyntaxError: Unexpected token '`', "```json
{
"... is not valid JSON
at JSON.parse (<anonymous>)
at _Memory.addToVectorStore (mem0ai/src/oss/src/memory/index.ts:370:28)
at _Memory.add (mem0ai/src/oss/src/memory/index.ts:243:31)
at OSSProvider.add (index.ts:263:20)
Steps to Reproduce
- Use
mem0ai with a LLM that wraps JSON responses in markdown code blocks
- Trigger a memory
add operation where the LLM response is long enough to be truncated before the closing ``` (e.g., agent replies containing large code blocks or architecture documents in the same conversation)
- Observe
Failed to parse memory actions from LLM response in logs
Root Cause
The regex /```(?:\w+)?\n?([\s\S]*?)```/g is non-greedy and requires a closing ``` to capture the inner content. A truncated response only has the opening fence — the regex silently skips it and returns the original text unchanged, which still starts with ```json.
Suggested Fix
function removeCodeBlocks(text) {
// Handle complete code blocks (opening + closing fence)
let result = text.replace(/```(?:\w+)?\n?([\s\S]*?)```/g, "$1").trim();
// Handle truncated responses where closing ``` never arrives
result = result.replace(/^```(?:\w+)?\n?/, "").replace(/\n?```$/, "").trim();
return result;
}
This is a two-pass approach:
- First pass: strips complete
```lang ... ``` blocks (existing behavior)
- Second pass: strips any remaining leading/trailing fence markers (handles truncation)
Environment
| Field |
Value |
| mem0ai version |
0.3.3 (npm) |
| LLM |
claude-haiku-4-5 via LiteLLM proxy |
| Affected function |
removeCodeBlocks in dist/oss/index.js line ~2648 |
| Affected callers |
addToVectorStore (line ~4764), updateMemory (line ~4804) |
Notes
- The
removeCodeBlocks function is already used in the right places — only the implementation needs to be updated
- This fix is backwards-compatible and does not affect responses that are already clean JSON
- Workaround applied locally by patching
dist/oss/index.js directly
🐛 Describe the bug
Bug Description
removeCodeBlocksuses a regex that requires both opening and closing backtick fences to match:When the LLM returns a very long JSON response wrapped in
```json ```that gets truncated (no closing fence arrives), the regex does not match. The raw text — starting with```json— is passed directly toJSON.parse, causing a crash.Error
Steps to Reproduce
mem0aiwith a LLM that wraps JSON responses in markdown code blocksaddoperation where the LLM response is long enough to be truncated before the closing```(e.g., agent replies containing large code blocks or architecture documents in the same conversation)Failed to parse memory actions from LLM responsein logsRoot Cause
The regex
/```(?:\w+)?\n?([\s\S]*?)```/gis non-greedy and requires a closing```to capture the inner content. A truncated response only has the opening fence — the regex silently skips it and returns the original text unchanged, which still starts with```json.Suggested Fix
This is a two-pass approach:
```lang ... ```blocks (existing behavior)Environment
removeCodeBlocksindist/oss/index.jsline ~2648addToVectorStore(line ~4764),updateMemory(line ~4804)Notes
removeCodeBlocksfunction is already used in the right places — only the implementation needs to be updateddist/oss/index.jsdirectly