{"record":{"id":"df696ea50f708eed","repo":"mem0ai/mem0","slug":"langchain-provider-requires-an-initialized-langcha","errorCode":null,"errorMessage":"Langchain provider requires an initialized Langchain instance passed via the 'model' field in the LLM config.","messagePattern":"Langchain provider requires an initialized Langchain instance passed via the 'model' field in the LLM config\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"mem0-ts/src/oss/src/llms/langchain.ts","lineNumber":43,"sourceCode":"      case \"assistant\":\n      case \"ai\":\n        return new AIMessage(content);\n      default:\n        console.warn(\n          `Unsupported message role '${msg.role}' for Langchain. Treating as 'human'.`,\n        );\n        return new HumanMessage(content);\n    }\n  });\n};\n\nexport class LangchainLLM implements LLM {\n  private llmInstance: BaseLanguageModel;\n  private modelName: string;\n\n  constructor(config: LLMConfig) {\n    if (!config.model || typeof config.model !== \"object\") {\n      throw new Error(\n        \"Langchain provider requires an initialized Langchain instance passed via the 'model' field in the LLM config.\",\n      );\n    }\n    if (typeof (config.model as any).invoke !== \"function\") {\n      throw new Error(\n        \"Provided Langchain 'instance' in the 'model' field does not appear to be a valid Langchain language model (missing invoke method).\",\n      );\n    }\n    this.llmInstance = config.model as BaseLanguageModel;\n    this.modelName =\n      (this.llmInstance as any).modelId ||\n      (this.llmInstance as any).model ||\n      \"langchain-model\";\n  }\n\n  async generateResponse(\n    messages: Message[],\n    response_format?: { type: string },","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/llms/langchain.ts#L25-L61","documentation":"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.","triggerScenarios":"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().","commonSituations":"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.","solutions":["Construct a real Langchain model and pass it as model: import { ChatOpenAI } from '@langchain/openai'; new ChatOpenAI({ model: 'gpt-4o', apiKey: ... }).","Ensure @langchain/core and the relevant integration package (@langchain/openai, @langchain/anthropic, ...) are installed as peers in the host project.","Do not pass a string, and do not re-use a config written for other mem0 providers — only Langchain takes an instance.","Build the instance once and reuse it; constructing per-request wastes connections."],"exampleFix":"// before\nconst mem = new Memory({\n  llm: { provider: 'langchain', config: { model: 'gpt-4o' } }, // string → throws\n});\n\n// after\nimport { ChatOpenAI } from '@langchain/openai';\nconst chatModel = new ChatOpenAI({ model: 'gpt-4o', apiKey: process.env.OPENAI_API_KEY });\nconst mem = new Memory({\n  llm: { provider: 'langchain', config: { model: chatModel } },\n});","handlingStrategy":"type-guard","validationCode":"function isLangchainModelInstance(v: unknown): boolean {\n  return Boolean(v) && typeof v === 'object' && typeof (v as any).invoke === 'function';\n}\n// before constructing Memory:\nif (!isLangchainModelInstance(cfg.model)) throw new Error('provider langchain requires a model instance');","typeGuard":"function isLangchainModel(v: unknown): v is import('@langchain/core/language_models/base').BaseLanguageModel {\n  return typeof v === 'object' && v !== null && typeof (v as { invoke?: unknown }).invoke === 'function';\n}","tryCatchPattern":"try {\n  new LangchainLLM(config);\n} catch (err) {\n  if ((err as Error).message.includes('Langchain provider requires')) {\n    throw new ConfigError('Pass a constructed Langchain model (e.g. new ChatOpenAI(...)) via config.model');\n  }\n  throw err;\n}","preventionTips":["Only the langchain provider takes an instance; keep separate config builders per provider.","Construct the model once at module scope and inject it.","Add a unit test asserting isLangchainModel(config.model) before boot."],"tags":["langchain","llm","config","typescript","validation"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}