Skip to content

anthropicAIIntegration() breaks Anthropic's streaming API when running TypeScript via ts-node #17977

@adoxner

Description

@adoxner

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/node - express

SDK Version

10.20.0

Framework Version

Node v20.19.0

Link to Sentry event

No response

Reproduction Example/SDK Setup

Environment

  • @sentry/node: 10.20.0
  • @sentry/profiling-node: 10.19.0
  • @anthropic-ai/sdk: 0.65.0
  • Node.js: v20.19.0
  • TypeScript: 5.9.3
  • ts-node: 10.9.2

Critical Finding

The bug ONLY occurs when running TypeScript via ts-node. It does NOT occur with compiled JavaScript.

✅ Works: Compiled JS

npx tsc file.ts && node file.js  # Works fine

❌ Fails: ts-node

npx ts-node file.ts  # Bug triggers!

Reproduction

File: sentry-bug-with-tsnode.ts

const Sentry = require('@sentry/node')

Sentry.init({
  dsn: 'https://[email protected]/0',
  integrations: [
    Sentry.anthropicAIIntegration({ recordInputs: true, recordOutputs: true })
  ],
  tracesSampleRate: 1.0,
  profilesSampleRate: 1.0
})

import Anthropic from '@anthropic-ai/sdk'

async function test () {
  const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY })

  const streamResult = anthropic.messages.stream({
    max_tokens: 100,
    model: 'claude-sonnet-4-5',
    stream: true,
    messages: [{ role: 'user', content: 'Count to 3' }]
  })

  console.log('Type:', streamResult?.constructor?.name) // Shows "Promise" ❌
  console.log('Has .on?:', typeof streamResult?.on)      // Shows "undefined" ❌

  const awaited = await streamResult
  console.log('After await, has .on?:', typeof awaited?.on)  // Still "undefined" ❌

  // This will fail:
  const stream = await streamResult.on('streamEvent', handler) // TypeError!
}

Steps to Reproduce

Run the above example code:

npx ts-node sentry-bug-with-tsnode.ts

Expected Result

Returns type of MessageStream.

Output from example code:

Type: MessageStream
Has .on?: function

Actual Result

Returns promise resolving to an empty object.

Output from example code:

[DEBUG] Stream constructor: Promise  ← Should be "MessageStream"
[DEBUG] Has .on method?: undefined   ← Should be "function"

⚠️  BUG DETECTED: Sentry returned Promise instead of MessageStream
Awaiting Promise...

[AFTER AWAIT] Constructor: (no constructor)
[AFTER AWAIT] Has .on?: undefined
[AFTER AWAIT] Has .finalMessage?: undefined
[AFTER AWAIT] Keys:

❌ BUG CONFIRMED!
After awaiting, result has no .on() method

Additional Context

This has been broken since SDK version 10.7.0, as reported in #17734. A fix was created in #17754 which fixed the streaming in most cases but not with ts-node. The instrumentation appears to replace the MessageStream with a Promise that resolves to an empty object when modules are transpiled at runtime.

Because this defect breaks the streaming functionality of the anthropic sdk in ts-node, the only workarounds are to keep the SDK version 10.6.0 or remove the anthropicAIIntegration option.

Tip: React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it.

Metadata

Metadata

Assignees

Projects

Status

Waiting for: Product Owner

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions