Skip to content

fix(server-utils): avoid stack overflow when patching ai 6 spec-v2 models - #26909

Closed
PCTEJA wants to merge 1 commit into
getsentry:developfrom
PCTEJA:fix/vercel-ai-v2-proxy-stack-overflow-26905
Closed

PCTEJA wants to merge 1 commit into
getsentry:developfrom
PCTEJA:fix/vercel-ai-v2-proxy-stack-overflow-26905

Conversation

@PCTEJA

@PCTEJA PCTEJA commented Nov 26, 2026

Copy link
Copy Markdown

What

Fixes the Maximum call stack size exceeded crash on every generateText call with ai 6 and a spec-v2 provider: the model-call patch now calls the model's stored doGenerate/doStream instead of the forwarding closure ai 6's asLanguageModelV3 proxy returns, so patching through the proxy no longer recurses. The model-call span is still emitted, including for models reached only via prepareStep or a string model id.

Why

ai 6 wraps v2 models in a Proxy whose get trap returns a fresh closure forwarding to the target on every read, with no set trap — capturing that closure and assigning a wrapper landed the wrapper on the target while the closure still forwarded to it. The stored method is the real one (v2 providers define these as prototype methods), and the SDK still invokes the closure, which forwards to the wrapper.

Closes: #26905

…dels

ai 6 wraps v2 models in a Proxy whose get trap returns a fresh closure forwarding to the target on every read, with no set trap. Capturing that closure and assigning a wrapper landed the wrapper on the target while the closure still forwarded to it, recursing until the stack overflowed. The wrapper now calls the model's stored method instead, and the model passed to the operation — the proxy's target — is patched at the operation start, so the model-call span is still emitted.
@github-actions github-actions Bot added the external PR from an external contributor label Nov 26, 2026
@RulaKhaled

Copy link
Copy Markdown
Collaborator

Thanks for picking this up and for the clear write-up of the cause, @PCTEJA. We went with a different fix in #26941: the resolveLanguageModel hook returns a Proxy around the model instead of assigning onto it. That avoids writing to objects we don't own, so it doesn't need the descriptor walk or the extra guards, and it keeps the v4-only operation-start patch as it was. Closing this in favor of #26941. Appreciate the contribution!

@RulaKhaled RulaKhaled closed this Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external PR from an external contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants