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 language model (missing invoke method).

What it means

Thrown synchronously by the LangchainLLM constructor when config.model IS an object but does not expose an invoke method. All Langchain language models (BaseLanguageModel subclasses like ChatOpenAI, ChatAnthropic) implement invoke(), so the constructor duck-types on that method to reject objects that are not runnable LLMs.

Source

Thrown at mem0-ts/src/oss/src/llms/langchain.ts:48

          `Unsupported message role '${msg.role}' for Langchain. Treating as 'human'.`,
        );
        return new HumanMessage(content);
    }
  });
};

export class LangchainLLM implements LLM {
  private llmInstance: BaseLanguageModel;
  private modelName: string;

  constructor(config: LLMConfig) {
    if (!config.model || typeof config.model !== "object") {
      throw new Error(
        "Langchain provider requires an initialized Langchain instance passed via the 'model' field in the LLM config.",
      );
    }
    if (typeof (config.model as any).invoke !== "function") {
      throw new Error(
        "Provided Langchain 'instance' in the 'model' field does not appear to be a valid Langchain language model (missing invoke method).",
      );
    }
    this.llmInstance = config.model as BaseLanguageModel;
    this.modelName =
      (this.llmInstance as any).modelId ||
      (this.llmInstance as any).model ||
      "langchain-model";
  }

  async generateResponse(
    messages: Message[],
    response_format?: { type: string },
    tools?: any[],
  ): Promise<string | LLMResponse> {
    const langchainMessages = await convertToLangchainMessages(messages);
    let runnable: any = this.llmInstance;
    const invokeOptions: Record<string, any> = {};

View on GitHub (pinned to 001c235229)

Solutions

  1. Pass an actual chat model instance: new ChatOpenAI(...), new ChatAnthropic(...), or any BaseLanguageModel subclass from @langchain/*.
  2. If wrapping a model in a custom adapter, implement invoke(messages) delegating to the wrapped model.
  3. Check you did not accidentally pass OpenAIEmbeddings or a retriever — those are different Langchain runnables without the chat-model contract.
  4. Upgrade @langchain/core to a version where invoke is the standard interface (>=0.1.x).

Example fix

// before
import { OpenAIEmbeddings } from '@langchain/openai';
const mem = new Memory({
  llm: { provider: 'langchain', config: { model: new OpenAIEmbeddings() } }, // no invoke → throws
});

// after
import { ChatOpenAI } from '@langchain/openai';
const mem = new Memory({
  llm: { provider: 'langchain', config: { model: new ChatOpenAI({ model: 'gpt-4o-mini' }) } },
});
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof (cfg.model as any)?.invoke !== 'function') {
  throw new TypeError('config.model must be a Langchain chat model with .invoke() — embeddings/prompts are not LLMs');
}

Type guard

function isRunnableChatModel(v: unknown): v is { invoke(m: unknown): Promise<unknown> } {
  return typeof v === 'object' && v !== null && typeof (v as any).invoke === 'function';
}

Try / catch

try { new LangchainLLM(config); } catch (e) {
  if ((e as Error).message.includes('missing invoke method')) throw new ConfigError('Use ChatOpenAI/ChatAnthropic, not embeddings or prompt templates');
  throw e;
}

Prevention

When it happens

Trigger: Passing an object in the model field that lacks .invoke — for example a Langchain PromptTemplate, an embeddings instance (OpenAIEmbeddings), a vector store, a plain { model: 'gpt-4o' } options object, or a custom class without an invoke method.

Common situations: Confusing which Langchain object to hand to mem0 (embeddings vs chat model); passing the output of a builder function that returns config rather than the model; wrapping a model in a custom adapter without delegating invoke; version drift where a very old @langchain/core exposed generate instead of invoke.

Related errors


AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15). Data as JSON: /api/errors/e9ff8d6408927510. Report an issue: GitHub.