Skip to content

fix(ts): extract content from code blocks instead of deleting it#4317

Merged
whysosaket merged 1 commit intomem0ai:mainfrom
voidborne-d:fix/remove-code-blocks-regex
Mar 14, 2026
Merged

fix(ts): extract content from code blocks instead of deleting it#4317
whysosaket merged 1 commit intomem0ai:mainfrom
voidborne-d:fix/remove-code-blocks-regex

Conversation

@voidborne-d
Copy link
Copy Markdown
Contributor

Problem

Fixes #4108removeCodeBlocks deletes entire code blocks (including their JSON payload) instead of extracting the content inside them.

The regex / ```[^]*``` /g` matches everything between the opening and closing fence markers and replaces all of it with an empty string. When Claude returns:

```json
{"facts": ["user likes TypeScript"]}

the function returns `""` and `JSON.parse` throws `SyntaxError: Unexpected end of JSON input`.

## Fix

Replace the regex with a capturing group that **extracts** the content inside code fences while stripping only the fence markers:

```ts
// Before (deletes content)
text.replace(/```[^`]*```/g, "")

// After (extracts content)
text.replace(/```(?:\w+)?\n?([\s\S]*?)```/g, "$1").trim()

Tests

Added remove-code-blocks.test.ts covering:

  • JSON in ```json fence
  • Bare ``` fence
  • Plain text passthrough (no fences)
  • Multiple code blocks
  • Claude-style response with surrounding text

The removeCodeBlocks regex /```[^`]*```/g replaced entire code
blocks (including their content) with an empty string. When Claude or
other LLMs returned JSON wrapped in markdown code fences like:

```json
{"facts": ["saved fact"]}
```

the parser received an empty string and threw SyntaxError.

Fix: use a capturing group to extract the inner content of code fences
while stripping the fence markers themselves.

Fixes mem0ai#4108
@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.

@utkarsh240799
Copy link
Copy Markdown
Contributor

Thanks for your contribution @voidborne-d . Can you sign the CLA?

@voidborne-d
Copy link
Copy Markdown
Contributor Author

Thanks @utkarsh240799! Just signed the CLA ✅

@voidborne-d
Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Claude: Failed to parse facts from LLM response: SyntaxError: Unexpected end of JSON input

4 participants