{"record":{"id":"e9ff8d6408927510","repo":"mem0ai/mem0","slug":"provided-langchain-instance-in-the-model-field-e9ff8d","errorCode":null,"errorMessage":"Provided Langchain 'instance' in the 'model' field does not appear to be a valid Langchain language model (missing invoke method).","messagePattern":"Provided Langchain 'instance' in the 'model' field does not appear to be a valid Langchain language model \\(missing invoke method\\)\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"mem0-ts/src/oss/src/llms/langchain.ts","lineNumber":48,"sourceCode":"          `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 },\n    tools?: any[],\n  ): Promise<string | LLMResponse> {\n    const langchainMessages = await convertToLangchainMessages(messages);\n    let runnable: any = this.llmInstance;\n    const invokeOptions: Record<string, any> = {};","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/llms/langchain.ts#L30-L66","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass an actual chat model instance: new ChatOpenAI(...), new ChatAnthropic(...), or any BaseLanguageModel subclass from @langchain/*.","If wrapping a model in a custom adapter, implement invoke(messages) delegating to the wrapped model.","Check you did not accidentally pass OpenAIEmbeddings or a retriever — those are different Langchain runnables without the chat-model contract.","Upgrade @langchain/core to a version where invoke is the standard interface (>=0.1.x)."],"exampleFix":"// before\nimport { OpenAIEmbeddings } from '@langchain/openai';\nconst mem = new Memory({\n  llm: { provider: 'langchain', config: { model: new OpenAIEmbeddings() } }, // no invoke → throws\n});\n\n// after\nimport { ChatOpenAI } from '@langchain/openai';\nconst mem = new Memory({\n  llm: { provider: 'langchain', config: { model: new ChatOpenAI({ model: 'gpt-4o-mini' }) } },\n});","handlingStrategy":"type-guard","validationCode":"if (typeof (cfg.model as any)?.invoke !== 'function') {\n  throw new TypeError('config.model must be a Langchain chat model with .invoke() — embeddings/prompts are not LLMs');\n}","typeGuard":"function isRunnableChatModel(v: unknown): v is { invoke(m: unknown): Promise<unknown> } {\n  return typeof v === 'object' && v !== null && typeof (v as any).invoke === 'function';\n}","tryCatchPattern":"try { new LangchainLLM(config); } catch (e) {\n  if ((e as Error).message.includes('missing invoke method')) throw new ConfigError('Use ChatOpenAI/ChatAnthropic, not embeddings or prompt templates');\n  throw e;\n}","preventionTips":["Never pass OpenAIEmbeddings, PromptTemplate, or retrievers as config.model.","Custom adapters must implement invoke delegating to a real model.","Keep @langchain/core >= 0.1 so invoke is guaranteed on all chat models."],"tags":["langchain","llm","type-guard","config","typescript"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}