fix: add allowSystemInMessages to all AI SDK call sites#2141
fix: add allowSystemInMessages to all AI SDK call sites#2141thelinuxkid wants to merge 1 commit into
Conversation
ai@>=5.0.168 throws InvalidPromptError when role:"system" appears in the messages array without allowSystemInMessages:true. Stagehand intentionally passes system messages via the messages array (to support Anthropic cache control via providerOptions), so opt in explicitly at all 8 affected call sites. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
|
This PR is from an external contributor and must be approved by a stagehand team member with write access before CI can run. |
There was a problem hiding this comment.
No issues found across 4 files
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Architecture diagram
sequenceDiagram
participant UserApp as User Application
participant Stagehand as Stagehand Core
participant AISdk as AI SDK (^5.x)
participant Anthropic as Anthropic API
Note over UserApp,Anthropic: LLM Call Flow with allowSystemInMessages: true
UserApp->>Stagehand: act() / extract() / agent()
Note over Stagehand: Three entry points
Stagehand->>Stagehand: Build messages array
Note over Stagehand: System prompt placed in messages[]<br/>with providerOptions for caching
alt Core LLM calls (v3/llm/aisdk.ts)
Stagehand->>AISdk: generateObject(messages, allowSystemInMessages: true)
Stagehand->>AISdk: generateText(messages, allowSystemInMessages: true)
else External client calls (v3/external_clients/aisdk.ts)
Stagehand->>AISdk: generateObject(messages, allowSystemInMessages: true)
Stagehand->>AISdk: generateText(messages, allowSystemInMessages: true)
else Agent handler (v3/handlers/v3AgentHandler.ts)
Stagehand->>AISdk: generateText(messages, allowSystemInMessages: true)
Stagehand->>AISdk: streamText(messages, allowSystemInMessages: true)
else Evals client (evals/lib/AISdkClientWrapped.ts)
Stagehand->>AISdk: generateObject(messages, allowSystemInMessages: true)
Stagehand->>AISdk: generateText(messages, allowSystemInMessages: true)
end
Note over AISdk: allowSystemInMessages: true<br/>prevents InvalidPromptError
AISdk->>Anthropic: API call with system message + cacheControl
Anthropic-->>AISdk: Response with cached prompt
AISdk-->>Stagehand: LLM result
Stagehand-->>UserApp: Action result / extracted data
Note over Stagehand,Anthropic: handleDoneToolCall.ts (excluded)<br/>uses system: param instead of messages[]
Problem
@browserbasehq/stagehand@3.4.0declares"ai": "^5.0.133"as a peer dependency. The Vercel AI SDK introduced a breaking change in PR #14752 (merged April 28, 2026): passing arole: "system"message inside themessagesarray now throws anInvalidPromptErrorby default:The repo lockfile pins
ai@5.0.133(pre-break, October 2025), so developers working directly in this repo don't see the error. However, end users installing@browserbasehq/stagehandresolve^5.0.133→ai@5.0.188(the latest stable 5.x, published May 12 2026, post-break), and hit the error on every LLM call.Why Stagehand passes system messages in
messagesStagehand intentionally passes system messages inside the
messagesarray (rather than using thesystemparameter) in order to attach Anthropic prompt-caching directives viaproviderOptions:This pattern is deliberate and correct — it cannot be replaced by the
systemparameter in the current AI SDK version without losing caching support.Fix
The AI SDK provides
allowSystemInMessages: trueas an explicit opt-in for exactly this use case. This PR adds it to all 8 affected call sites across 4 files:packages/core/lib/v3/llm/aisdk.tsgenerateObject,generateTextpackages/core/lib/v3/external_clients/aisdk.tsgenerateObject,generateTextpackages/core/lib/v3/handlers/v3AgentHandler.tsgenerateText(non-streaming),streamTextpackages/evals/lib/AISdkClientWrapped.tsgenerateObject,generateTextpackages/core/lib/v3/agent/utils/handleDoneToolCall.tsis correctly excluded — it already uses thesystem:parameter and does not put system messages in themessagesarray.Test plan
ai@5.0.188(or latest^5.0.133) and run any Stagehand script that exercisesact,extract, oragent— confirm noInvalidPromptErroris thrownproviderOptionsare preserved)Summary by cubic
Opt in to allowSystemInMessages at all AI SDK call sites to prevent InvalidPromptError with newer
aiversions. Keeps system prompts in the messages array so Anthropic caching continues to work for@browserbasehq/stagehandusers.ai@>=5.0.168and older^5.x.Written for commit 9a4bb32. Summary will update on new commits. Review in cubic