Skip to content

vercelAIIntegration doesn't activate on Vercel deployments without force: true #19147

@sergical

Description

@sergical

Problem

The vercelAIIntegration in @sentry/nextjs doesn't activate on Vercel deployments, causing AI SDK spans to have incorrect names:

  • On Vercel: Raw span names like ai.toolCall, ai.streamText (WRONG)
  • Locally: Transformed names like gen_ai.execute_tool, gen_ai.stream_text (CORRECT)

Root Cause

The Node SDK's vercelAIIntegration has conditional activation logic in afterAllSetup:

// packages/node/src/integrations/tracing/vercelai/index.ts:28-37
afterAllSetup(client) {
  const shouldForce = options.force ?? shouldForceIntegration(client);

  if (shouldForce) {
    addVercelAiProcessors(client);  // ← Span transformation enabled
  } else {
    instrumentation?.callWhenPatched(() => addVercelAiProcessors(client));
  }
}

addVercelAiProcessors is only called if:

  1. force: true is passed, OR
  2. Module detection succeeds (CJS mode only), OR
  3. OTEL successfully patches the ai module

Why It Fails on Vercel

The ai package is intentionally NOT externalized in Next.js builds:

// packages/nextjs/src/config/withSentryConfig/constants.ts:5-8
// NOTE: 'ai' (Vercel AI SDK) is intentionally NOT included in this list.
// When externalized, Next.js doesn't properly handle the package's conditional exports,
// specifically the "react-server" export condition.

Because ai is bundled (not externalized):

  • OTEL's require hooks can't intercept module loading
  • instrumentation?.callWhenPatched() callback never fires
  • Module detection via shouldForceIntegration fails (requires CJS mode)
  • Result: addVercelAiProcessors() is never called on Vercel

Comparison: Edge vs Node Implementation

Edge implementation (vercel-edge/src/integrations/tracing/vercelai.ts):

setup(client) {
  addVercelAiProcessors(client);  // ALWAYS called unconditionally
}

Node implementation - conditional, requires force: true to guarantee activation in bundled environments.

Current Workaround

Users must explicitly add force: true in their server config:

// sentry.server.config.ts
import * as Sentry from "@sentry/nextjs";

Sentry.init({
  dsn: "...",
  integrations: [Sentry.vercelAIIntegration({ force: true })],
});

Suggested Fix

Consider one of these approaches:

  1. Auto-detect Vercel environment: Default force: true when VERCEL env var is present
  2. Document the workaround: Add clear documentation that force: true is required for Vercel deployments
  3. Change default behavior: Make the Node implementation behave like Edge (always register processors)

Environment

  • @sentry/nextjs: Latest
  • Deployment: Vercel (serverless)
  • AI SDK: ai package from Vercel

Metadata

Metadata

Assignees

Labels

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions