Skip to content

@sentry/node v9.40+ breaks OpenAI imports when tracesSampler is configured #17221

@mfazekas

Description

@mfazekas

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/node

SDK Version

9.43.0

Framework Version

Node 22.16.0

Link to Sentry event

N/A - This is a build/import error

Reproduction Example/SDK Setup

https://github.com/mfazekas/sentry-openai-repro

Steps to Reproduce

  1. Clone the reproduction repository: https://github.com/mfazekas/sentry-openai-repro
  2. Run npm install
  3. Run npm test

The minimal reproduction consists of two files:

sentry.ts:

import * as Sentry from '@sentry/node'

Sentry.init({
  dsn: 'https://example@sentry.io/123456',
  environment: 'test',
  sampleRate: 1.0,
  tracesSampler: () => {
    return 1.0
  },
})

export default Sentry

index.ts:

// Import Sentry first
import './sentry'

// Then import OpenAI
import OpenAI from 'openai'

// This will fail with: TypeError: import_openai.default is not a constructor
const client = new OpenAI({ apiKey: 'dummy-key' })
console.log('✓ OpenAI client created successfully!')

Expected Result

The OpenAI client should be created successfully without errors.

Actual Result

TypeError: import_openai.default is not a constructor
    at OpenAI (/Users/boga/Work/Zabit/zabit-mono/.local/sentry-openai-repro/index.ts:8:16)
    at Object.<anonymous> (/Users/boga/Work/Zabit/zabit-mono/.local/sentry-openai-repro/index.ts:9:52)
    at Module._compile (node:internal/modules/cjs/loader:1730:14)
    at Object.transformer (/Users/boga/Work/Zabit/zabit-mono/.local/sentry-openai-repro/node_modules/tsx/dist/register-DCnOAxY2.cjs:2:1186)
    at Module.load (node:internal/modules/cjs/loader:1465:32)
    at Function._load (node:internal/modules/cjs/loader:1282:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:235:24)
    at cjsLoader (node:internal/modules/esm/translators:266:5)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:200:7)

Additional Context

Version-specific findings:

  • ✅ Works: @sentry/node v8.38.0
  • ✅ Works: @sentry/node v9.0.0
  • ✅ Works: @sentry/node v9.20.0
  • ✅ Works: @sentry/node v9.39.0
  • ❌ Fails: @sentry/node v9.40.0
  • ❌ Fails: @sentry/node v9.43.0

The issue was introduced in v9.40.0.

Key findings:

  1. The issue ONLY occurs when tracesSampler is configured
  2. The issue ONLY occurs when Sentry is initialized in a separate file that is imported
  3. Setting registerEsmLoaderHooks: false does NOT fix the issue
  4. The issue does NOT occur when both Sentry and OpenAI are imported in the same file

Workarounds:

  1. Remove tracesSampler configuration
  2. Use tracesSampleRate instead of tracesSampler
  3. Import OpenAI before initializing Sentry
  4. Downgrade to @sentry/node v9.39.0 or earlier
  5. Initialize Sentry and import OpenAI in the same file

This appears to be related to how Sentry instruments modules when tracesSampler is enabled. The issue is similar to #12414 but manifests differently (v8 shows "API.Completions is not a constructor" while v9.40+ shows "import_openai.default is not a constructor").

Environment

  • Node.js: v22.16.0
  • @sentry/node: 9.43.0
  • openai: ^4.93.0
  • typescript: ~5.8.3
  • tsx: 4.19.3

Metadata

Metadata

Assignees

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions