Skip to content

removeCodeBlocks fails to strip markdown fences from truncated LLM responses #4401

@bsmoreno2910

Description

@bsmoreno2910

🐛 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

  1. Use mem0ai with a LLM that wraps JSON responses in markdown code blocks
  2. 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)
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions