mem0ai/mem0 · error · Error

The Mem0 model function cannot be called with the new keywor

Error message

The Mem0 model function cannot be called with the new keyword.

What it means

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.

Source

Thrown at integrations/vercel-ai-sdk/src/mem0-provider.ts:126

        baseURL,
        fetch: options.fetch,
        headers: getHeaders(),
        provider: options.provider || "openai",
        name: options.name,
        mem0ApiKey: options.mem0ApiKey,
        apiKey: options.apiKey,
        mem0Config: options.mem0Config,
        modelType: "chat",
      },
      options.config
    );

  const provider = function (
    modelId: Mem0ChatModelId,
    settings: Mem0ChatSettings = {}
  ) {
    if (new.target) {
      throw new Error(
        "The Mem0 model function cannot be called with the new keyword."
      );
    }

    return createGenericModel(modelId, settings);
  };

  provider.specificationVersion = 'v3';
  provider.languageModel = createGenericModel;
  provider.completion = createCompletionModel;
  provider.chat = createChatModel;

  return provider as unknown as Mem0Provider;
}

export const mem0 = createMem0();

View on GitHub (pinned to 001c235229)

Solutions

  1. Drop the `new` keyword: `const model = mem0('gpt-4o')` instead of `new mem0('gpt-4o')`.
  2. Use the namespaced helpers on the factory: provider.chat(modelId) or provider.completion(modelId).
  3. If you need a class-like API, wrap the factory in your own class that calls it internally.

Example fix

// before
const model = new mem0Provider.chat("gpt-4o");

// after
const model = mem0Provider.chat("gpt-4o");
Defensive patterns

Strategy: type-guard

Type guard

// The factory is a function; narrow at compile time
declare const mem0Provider: {
  (modelId: string, settings?: Record<string, unknown>): LanguageModelV3;
  chat: (id: string) => LanguageModelV3;
  completion: (id: string) => LanguageModelV3;
};
// TypeScript blocks `new mem0Provider(...)` only if types are imported correctly — keep them imported

Try / catch

try {
  const model = mem0Provider('gpt-4o');
} catch (e) {
  if ((e as Error).message.includes('new keyword')) {
    // a dependency called it with new; fix the call site, do not swallow
    throw new Error('Call mem0Provider as a function, not with new');
  }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: Migrating code from class-based model wrappers; copy-pasting `new OpenAI(...)` style init from the openai SDK; IDE auto-completing with `new`.

Related errors


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