Skip to content

Vercel AI: ai 6 with a v2 provider crashes with 'Maximum call stack size exceeded' #26905

In review

Description

@RulaKhaled

Problem

With ai@6 and an older provider package (@ai-sdk/openai@2, @ai-sdk/google@2, ...), every generateText and streamText call fails with RangeError: Maximum call stack size exceeded when Sentry is loaded. Without Sentry, the same call works.

Affected: ai@6 with a v2 provider, on @sentry/node 10.64.0 and later. ai@6 with v3 providers (@ai-sdk/openai@3, ...) works, and so do ai 4, 5 and 7.

Cause

ai 6 wraps older models in a Proxy before calling them. Our instrumentation then assigns its own doGenerate/doStream onto that proxy. The proxy passes the assignment through to the model underneath, so our wrapper and ai's wrapper end up calling each other until the stack overflows.

Our tests didn't catch it because they only use MockLanguageModelV3, which ai doesn't wrap.

Reproduction

@sentry/[email protected], [email protected], @ai-sdk/[email protected], Node 20.

instrument.mjs:

import * as Sentry from '@sentry/node';

Sentry.init({
  dsn: 'https://[email protected]/1337',
  tracesSampleRate: 1,
  transport: () => ({ send: async () => ({}), flush: async () => true }),
});

app.mjs:

import * as Sentry from '@sentry/node';
import { generateText } from 'ai';
import { createOpenAI } from '@ai-sdk/openai';

const fetch = async () =>
  new Response(
    JSON.stringify({
      id: 'c1',
      object: 'chat.completion',
      created: 0,
      model: 'gpt-4o',
      choices: [{ index: 0, message: { role: 'assistant', content: 'hi' }, finish_reason: 'stop' }],
      usage: { prompt_tokens: 1, completion_tokens: 2, total_tokens: 3 },
    }),
    { headers: { 'content-type': 'application/json' } },
  );
const openai = createOpenAI({ apiKey: 'x', fetch });

try {
  const r = await Sentry.startSpan({ name: 'main' }, () => generateText({ model: openai.chat('gpt-4o'), prompt: 'x' }));
  console.log('OK:', r.text);
} catch (e) {
  console.log('FAILED:', e.constructor.name, e.message);
}
$ node --import ./instrument.mjs app.mjs
FAILED: RangeError Maximum call stack size exceeded

$ node app.mjs
OK: hi

A hand-written { specificationVersion: 'v2', doGenerate, ... } model fails the same way.

Fix direction

Don't assign onto the resolved model. For example, return a wrapping object or proxy from the resolveLanguageModel hook, or patch the unwrapped target. A regression test should use a spec-v2 model on ai 6.

Activity

  1. linear-code commented on Nov 25, 2026

    @linear-code
  2. self-assigned this
    on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions