{"record":{"id":"0ca8c611141ca6a7","repo":"mem0ai/mem0","slug":"the-mem0-model-function-cannot-be-called-with-the","errorCode":null,"errorMessage":"The Mem0 model function cannot be called with the new keyword.","messagePattern":"The Mem0 model function cannot be called with the new keyword\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"integrations/vercel-ai-sdk/src/mem0-provider.ts","lineNumber":126,"sourceCode":"        baseURL,\n        fetch: options.fetch,\n        headers: getHeaders(),\n        provider: options.provider || \"openai\",\n        name: options.name,\n        mem0ApiKey: options.mem0ApiKey,\n        apiKey: options.apiKey,\n        mem0Config: options.mem0Config,\n        modelType: \"chat\",\n      },\n      options.config\n    );\n\n  const provider = function (\n    modelId: Mem0ChatModelId,\n    settings: Mem0ChatSettings = {}\n  ) {\n    if (new.target) {\n      throw new Error(\n        \"The Mem0 model function cannot be called with the new keyword.\"\n      );\n    }\n\n    return createGenericModel(modelId, settings);\n  };\n\n  provider.specificationVersion = 'v3';\n  provider.languageModel = createGenericModel;\n  provider.completion = createCompletionModel;\n  provider.chat = createChatModel;\n\n  return provider as unknown as Mem0Provider;\n}\n\nexport const mem0 = createMem0();\n","sourceCodeStart":108,"sourceCodeEnd":143,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/integrations/vercel-ai-sdk/src/mem0-provider.ts#L108-L143","documentation":"The Mem0 provider factory (mem0-provider.ts) is deliberately a plain function, not a class constructor. The `new.target` check inside it throws this error when JavaScript's `new` operator is used, because the factory pattern (matching Vercel AI SDK conventions like openai() or anthropic()) relies on closure state and shared static helpers, not instances.","triggerScenarios":"Calling `new provider('gpt-4o')` or `new createChatModel(...)` instead of `provider('gpt-4o')`. Typical when developers assume *Provider() functions are classes because of PascalCase-adjacent naming, or when TypeScript's --target/newTarget settings surface the check.","commonSituations":"Migrating code from class-based model wrappers; copy-pasting `new OpenAI(...)` style init from the openai SDK; IDE auto-completing with `new`.","solutions":["Drop the `new` keyword: `const model = mem0('gpt-4o')` instead of `new mem0('gpt-4o')`.","Use the namespaced helpers on the factory: provider.chat(modelId) or provider.completion(modelId).","If you need a class-like API, wrap the factory in your own class that calls it internally."],"exampleFix":"// before\nconst model = new mem0Provider.chat(\"gpt-4o\");\n\n// after\nconst model = mem0Provider.chat(\"gpt-4o\");","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// The factory is a function; narrow at compile time\ndeclare const mem0Provider: {\n  (modelId: string, settings?: Record<string, unknown>): LanguageModelV3;\n  chat: (id: string) => LanguageModelV3;\n  completion: (id: string) => LanguageModelV3;\n};\n// TypeScript blocks `new mem0Provider(...)` only if types are imported correctly — keep them imported","tryCatchPattern":"try {\n  const model = mem0Provider('gpt-4o');\n} catch (e) {\n  if ((e as Error).message.includes('new keyword')) {\n    // a dependency called it with new; fix the call site, do not swallow\n    throw new Error('Call mem0Provider as a function, not with new');\n  }\n  throw e;\n}","preventionTips":["Treat provider factories like openai()/anthropic(): call without new.","Import the SDK's TypeScript types so misuse is caught at compile time.","Lint for `new` on lowercase identifiers (no-new / naming conventions rules)."],"tags":["typescript","api-misuse","factory-pattern"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}