mem0ai/mem0 · error · Error
Provided Langchain 'instance' in the 'model' field does not
Error message
Provided Langchain 'instance' in the 'model' field does not appear to be a valid Langchain Embeddings instance (missing embedQuery or embedDocuments method).
What it means
Thrown by the LangchainEmbedder constructor when config.model IS an object but lacks functioning embedQuery or embedDocuments methods — the duck-type check for a real LangChain Embeddings instance. This catches near-misses: a plain options object, a partially built instance, a mock, or an object from an incompatible @langchain/core major version whose method surface changed.
Source
Thrown at mem0-ts/src/oss/src/embeddings/langchain.ts:21
import { EmbeddingConfig } from "../types";
export class LangchainEmbedder implements Embedder {
private embedderInstance: Embeddings;
private batchSize?: number; // Some LC embedders have batch size
constructor(config: EmbeddingConfig) {
// Check if config.model is provided and is an object (the instance)
if (!config.model || typeof config.model !== "object") {
throw new Error(
"Langchain embedder provider requires an initialized Langchain Embeddings instance passed via the 'model' field in the embedder config.",
);
}
// Basic check for embedding methods
if (
typeof (config.model as any).embedQuery !== "function" ||
typeof (config.model as any).embedDocuments !== "function"
) {
throw new Error(
"Provided Langchain 'instance' in the 'model' field does not appear to be a valid Langchain Embeddings instance (missing embedQuery or embedDocuments method).",
);
}
this.embedderInstance = config.model as Embeddings;
// Store batch size if the instance has it (optional)
this.batchSize = (this.embedderInstance as any).batchSize;
}
async embed(text: string): Promise<number[]> {
try {
// Use embedQuery for single text embedding
return await this.embedderInstance.embedQuery(text);
} catch (error) {
console.error("Error embedding text with Langchain Embedder:", error);
throw error;
}
}
View on GitHub (pinned to 001c235229)
Solutions
- Extend @langchain/core/embeddings Embeddings (or use a built-in like OpenAIEmbeddings) so both methods exist as real functions
- If wrapping, forward both methods: embedQuery = (t) => this.inner.embedQuery(t); embedDocuments = (d) => this.inner.embedDocuments(d);
- Construct the instance in the same process that builds Memory — never serialize it through IPC/JSON
Example fix
// before
class MyEmbedder { async embed(text: string) { /* ... */ } }
embedder: { provider: 'langchain', config: { model: new MyEmbedder() } }
// after
import { Embeddings } from '@langchain/core/embeddings';
class MyEmbedder extends Embeddings {
async embedQuery(text: string) { /* ... */ return [0.1]; }
async embedDocuments(docs: string[]) { /* ... */ return docs.map(() => [0.1]); }
}
embedder: { provider: 'langchain', config: { model: new MyEmbedder() } } Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof (instance as any).embedQuery !== 'function' || typeof (instance as any).embedDocuments !== 'function') {
throw new Error('Object is not a LangChain Embeddings instance: implement both embedQuery and embedDocuments');
} Type guard
const isLangchainEmbeddings = (m: unknown): m is Embeddings =>
!!m && typeof m === 'object' &&
typeof (m as Embeddings).embedQuery === 'function' &&
typeof (m as Embeddings).embedDocuments === 'function';
if (!isLangchainEmbeddings(config.model)) throw new Error('invalid embedder instance'); Prevention
- Custom wrappers must extend @langchain/core's Embeddings base or implement both methods
- Construct the instance in-process; never send it through JSON/IPC where methods are stripped
- Align @langchain/core versions across your app and dependencies to avoid a shifted method surface
When it happens
Trigger: Passing { model: { model: 'text-embedding-3-small' } } (config nested one level too deep); passing a custom wrapper that implements embed() but not embedQuery/embedDocuments; passing an object whose methods were stripped by structuredClone/serialization across worker boundaries.
Common situations: Wrapping an embedder in a custom class that does not extend LangChain's Embeddings base; version drift between @langchain/core in the host app and in dependencies; sending config through IPC/JSON so functions are lost.
Related errors
- Langchain embedder provider requires an initialized Langchai
- Unknown embedder provider: ${providerId}
- apiKey is required for Mem0Memory
- sessionId is required for Mem0Memory
- Invalid memory action: ${memoryAction}
AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15).
Data as JSON: /api/errors/9c0670c2d5484f9a.
Report an issue: GitHub.