fix(oss): validate LLM fact output via FactRetrievalSchema before embedding#4083
Merged
deshraj merged 1 commit intomem0ai:mainfrom Feb 23, 2026
Merged
Conversation
…edding
Local/smaller LLMs (e.g. llama3.1:8b via Ollama) sometimes return facts
as objects ({ fact: "..." }) instead of plain strings. addToVectorStore
passed these directly to the embedder, crashing Ollama's Go server:
ResponseError: json: cannot unmarshal object into Go struct
field EmbeddingRequest.prompt of type string
Fix: use the existing (but unused) FactRetrievalSchema with a z.union
transform to accept string | { fact } | { text } shapes and normalize
to string[]. Add a one-line safety net in OllamaEmbedder.embed().
Fixes mem0ai#4081
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
Great fix for a real production issue! 👍Thank you for tackling this crash with local LLMs. The approach is solid and the implementation is clean. What I like:
Code quality observations:mem0-ts/src/oss/src/prompts/index.ts: The mem0-ts/src/oss/src/memory/index.ts: Good replacement of raw JSON parsing with proper schema validation. Error handling maintains existing behavior. mem0-ts/src/oss/src/embeddings/ollama.ts: The defensive The fix is focused, low-risk, and addresses the root cause appropriately. Nice work! 🚀 |
deshraj
approved these changes
Feb 23, 2026
jamebobob
pushed a commit
to jamebobob/mem0-vigil-recall
that referenced
this pull request
Mar 29, 2026
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.
Summary
Fixes #4081
addToVectorStorepasses raw LLM-extracted facts directly toembedder.embed()without validating they are strings. Local/smaller LLMs (e.g.llama3.1:8bvia Ollama) sometimes return facts as objects instead of plain strings:This causes Ollama's Go server to crash with:
Approach
FactRetrievalSchemaalready exists inprompts/index.tsbut was never used at the parse site. This PR:prompts/index.ts— ExtendsFactRetrievalSchemawith az.uniontransform that acceptsstring | { fact: string } | { text: string }and normalizes tostring[], filtering empties.memory/index.ts— Replaces rawJSON.parse().factswithFactRetrievalSchema.parse()so the schema is actually used.embeddings/ollama.ts— One-line safety net (typeof text === "string" ? text : JSON.stringify(text)) in case non-string values reach the embedder from other callers.Test plan
memory.add()withllama3.1:8breturning[{ fact: "..." }]— normalizes and stores correctlymemory.add()with models returning["string"]— no behavior change.parse()catches completely invalid JSON and falls through tofacts = []🤖 Generated with Claude Code