mem0ai/mem0 · error · Error

Langchain provider requires an initialized Langchain instanc

Error message

Langchain provider requires an initialized Langchain instance passed via the 'model' field in the LLM config.

What it means

Thrown synchronously by the LangchainLLM constructor when config.model is missing or is not an object. Unlike other providers where 'model' is a string model name, the Langchain provider expects an already-constructed Langchain language model instance (e.g. new ChatOpenAI(...)) passed through the model field, so a plain string like 'gpt-4o' is rejected.

Source

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

      case "assistant":
      case "ai":
        return new AIMessage(content);
      default:
        console.warn(
          `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 },

View on GitHub (pinned to 001c235229)

Solutions

  1. Construct a real Langchain model and pass it as model: import { ChatOpenAI } from '@langchain/openai'; new ChatOpenAI({ model: 'gpt-4o', apiKey: ... }).
  2. Ensure @langchain/core and the relevant integration package (@langchain/openai, @langchain/anthropic, ...) are installed as peers in the host project.
  3. Do not pass a string, and do not re-use a config written for other mem0 providers — only Langchain takes an instance.
  4. Build the instance once and reuse it; constructing per-request wastes connections.

Example fix

// before
const mem = new Memory({
  llm: { provider: 'langchain', config: { model: 'gpt-4o' } }, // string → throws
});

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

Strategy: type-guard

Validate before calling

function isLangchainModelInstance(v: unknown): boolean {
  return Boolean(v) && typeof v === 'object' && typeof (v as any).invoke === 'function';
}
// before constructing Memory:
if (!isLangchainModelInstance(cfg.model)) throw new Error('provider langchain requires a model instance');

Type guard

function isLangchainModel(v: unknown): v is import('@langchain/core/language_models/base').BaseLanguageModel {
  return typeof v === 'object' && v !== null && typeof (v as { invoke?: unknown }).invoke === 'function';
}

Try / catch

try {
  new LangchainLLM(config);
} catch (err) {
  if ((err as Error).message.includes('Langchain provider requires')) {
    throw new ConfigError('Pass a constructed Langchain model (e.g. new ChatOpenAI(...)) via config.model');
  }
  throw err;
}

Prevention

When it happens

Trigger: Configuring provider 'langchain' with model omitted, model set to a string ('gpt-4o'), model set to null/undefined, or model set to a non-object primitive. The very next check (error 107) covers the case where an object is passed but lacks .invoke().

Common situations: Copy-pasting an OpenAI-provider config into the Langchain provider; assuming model accepts a model identifier string; passing a serialized/plain JSON description of a model instead of a live instance; passing a Langchain vector-store or embedding object where the LLM was expected.

Related errors


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