posthog-js icon indicating copy to clipboard operation
posthog-js copied to clipboard

@posthog/ai throws "Class extends value undefined is not a constructor or null" when using OpenAI (Anthropic import issue)`

Open ketanip opened this issue 3 months ago • 15 comments

Bug description

Please describe.

Using @posthog/ai with OpenAI results in:

TypeError: Class extends value undefined is not a constructor or null
    at Object.<anonymous> (node_modules/@posthog/ai/src/anthropic/index.ts:46:56)

This happens even though I’m not using Anthropic. The error points to PostHogAnthropic extends AnthropicOriginal, but AnthropicOriginal is undefined. It looks like @posthog/ai imports a default from @anthropic-ai/sdk that doesn’t exist or isn’t being resolved properly.


How to reproduce

  1. Install dependencies:

    npm install openai @posthog/ai posthog-node
    
  2. Add the following code:

    import { OpenAI } from '@posthog/ai'
    import { PostHog } from 'posthog-node'
    
    const phClient = new PostHog(
      "POSTHOG_KEY", 
      { host: "https://us.i.posthog.com" }
    );
    
    const openai = new OpenAI({
      apiKey: "POSTHOG_KEY",
      posthog: phClient,
    });
    
  3. Run the file with node → crash with TypeError: Class extends value undefined is not a constructor or null.


Related sub-libraries

  • [ ] All of them
  • [ ] posthog-js (web)
  • [ ] posthog-js-lite (web lite)
  • [ ] posthog-node
  • [ ] posthog-react-native
  • [ ] @posthog/react
  • [x] @posthog/ai
  • [ ] @posthog/nextjs-config

Additional context

  • I am not using Anthropic, but the package fails during import because PostHogAnthropic extends an undefined AnthropicOriginal.
  • Installing @anthropic-ai/sdk manually did not fix the issue.
  • Seems like the import in @posthog/ai/src/anthropic/index.ts is broken or should be optional when Anthropic isn’t used.

Thank you for your bug report – we love squashing them!

ketanip avatar Sep 30 '25 12:09 ketanip

I am also getting this same error when using the Google Vertex AI provider with this library.

mistakenot avatar Oct 01 '25 08:10 mistakenot

From my analysis this issue is because how they are importing anthroipic sdk in the library, that part of code should not trigger but for some reason it is triggering.

ketanip avatar Oct 01 '25 08:10 ketanip

Odd thing for me is that it works fine when running as a normal nextjs process, I only get this issue when running automated tests with Jest with this config:

import nextJest from 'next/jest.js';

/** @type {import('jest').Config} */
const createJestConfig = nextJest({
  // Provide the path to your Next.js app to load next.config.js and .env files in your test environment
  dir: './',
});

// Add any custom config to be passed to Jest
const config = {
  testEnvironment: 'node',  // Use node environment for server-side tests
  transform: {
    '^.+\\.(js|jsx|ts|tsx)$': ['babel-jest', { presets: ['next/babel'] }],
  },
  moduleNameMapper: {
    // Handle module aliases (if you use them in your project)
    '^@/(.*)$': '<rootDir>/src/$1',
  },
  testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/.next/'],
  detectOpenHandles: true,
};

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
export default createJestConfig(config);

mistakenot avatar Oct 01 '25 08:10 mistakenot

I'm using this with nest.js (backend framework), while startup it throws this error.

ketanip avatar Oct 01 '25 09:10 ketanip

@posthog-bot Any updates ?

ketanip avatar Oct 04 '25 06:10 ketanip

@Radu-Raicea @carlos-marchal-ph your input will be appreciated a lot on this matter.

ketanip avatar Oct 04 '25 07:10 ketanip

I'm using this for the Vercel gateway; the error throws on the Anthropic client. so changing the import from @posthog/ai to @posthog/ai/vercel fixed it for me.

import { withTracing } from "@posthog/ai/vercel";

aramvr avatar Oct 04 '25 15:10 aramvr

Good advice, I got around this by changing this:

import { GoogleGenAI } from '@posthog/ai'
import { PostHog } from 'posthog-node'

to this

import { PostHogGoogleGenAI as GoogleGenAI } from '@posthog/ai/gemini'
import { PostHog } from 'posthog-node'

mistakenot avatar Oct 05 '25 13:10 mistakenot

@PostHog/team-llm-analytics

marandaneto avatar Oct 06 '25 11:10 marandaneto

I got this working by importing it from @posthog/ai/openai also use node16 or later in modeuleResolution for typescript . I used "module": "nodenext" intsconfig.json.

import OpenAI from "@posthog/ai/openai";
import { PostHog } from 'posthog-node';

const phClient = new PostHog(
  "POSTHOG_KEY", 
  { host: "https://us.i.posthog.com" }
);

const openai = new OpenAI({
  apiKey: "OPEN_AI_KEY",
  posthog: phClient,
});

ketanip avatar Oct 06 '25 13:10 ketanip

Still broken for me...

seanghods avatar Oct 19 '25 01:10 seanghods

Use names imports like import OpenAI from "@posthog/ai/openai" as per provider you're working, it's a temporary workout until posthog team fixed it.

ketanip avatar Oct 19 '25 02:10 ketanip

Use names imports like import OpenAI from "@posthog/ai/openai" as per provider you're working, it's a temporary workout until posthog team fixed it.

Thanks, I had some issues with that solution. I ended up creating a patch with @dsinghvi's code in the PR above and it is working for me.

seanghods avatar Oct 19 '25 07:10 seanghods

Still broken for me too on Anthropic here

shadyshoha avatar Nov 25 '25 01:11 shadyshoha

so we fixing this or nah?

Khosraw avatar Dec 12 '25 20:12 Khosraw

Good advice, I got around this by changing this:

import { GoogleGenAI } from '@posthog/ai' import { PostHog } from 'posthog-node' to this

import { PostHogGoogleGenAI as GoogleGenAI } from '@posthog/ai/gemini' import { PostHog } from 'posthog-node'

I've tried this, but it seems PostHog is not logging the thoughtsToken of gemini. Only the output and input. Can't tell if it's a different bug or related to this workaround.

dannyhellman avatar Dec 25 '25 22:12 dannyhellman

I'm having this issue when using the actual Anthropic wrapper.

/app/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@opentel_3d63026c5d7eeb563f4d68eb3f58318e/node_modules/@posthog/ai/dist/index.cjs:2469
class WrappedMessages extends AnthropicOriginal.Messages {
                                                ^
TypeError: Class extends value undefined is not a constructor or null
    at Object.<anonymous> (/app/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@opentel_3d63026c5d7eeb563f4d68eb3f58318e/node_modules/@posthog/ai/dist/index.cjs:2469:49)
    at Module._compile (node:internal/modules/cjs/loader:1761:14)
    at Object..js (node:internal/modules/cjs/loader:1893:10)
    at Module.load (node:internal/modules/cjs/loader:1481:32)
    at Module._load (node:internal/modules/cjs/loader:1300:12)
    at TracingChannel.traceSync (node:diagnostics_channel:328:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:245:24)
    at Module.require (node:internal/modules/cjs/loader:1504:12)
    at require (node:internal/modules/helpers:152:16)
    at Object.<anonymous> (/app/dist/agents/normalizationAgent/normalizationAgent.js:4:14)
Node.js v24.12.0

Versions:

  • @anthropic-ai/sdk: ^0.71.2
  • @posthog/ai: ^7.3.0

yakkomajuri avatar Dec 27 '25 19:12 yakkomajuri