From b892e7a3d875013aef97577a2d64b26d53e583b6 Mon Sep 17 00:00:00 2001 From: Sascha Buehrle <47737812+saschabuehrle@users.noreply.github.com> Date: Tue, 10 Mar 2026 02:39:55 +0100 Subject: [PATCH] fix: gracefully disable thinking for non-reasoning models (fixes #350) (#351) When a user switches to a model that doesn't support thinking (e.g., Gemini 1.5 Flash) while a thinking level is still set from a previous model, the app crashes with an uncaught exception from requireSupportedEffort(). Add model.reasoning guards in mapOptionsForApi() for all provider paths (Anthropic, Google, Google Gemini CLI, Google Vertex, OpenAI) so that thinking is gracefully disabled instead of throwing. The Bedrock path already had this guard. --- packages/ai/src/stream.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/ai/src/stream.ts b/packages/ai/src/stream.ts index 789fd937f..826104e6e 100644 --- a/packages/ai/src/stream.ts +++ b/packages/ai/src/stream.ts @@ -391,7 +391,7 @@ function resolveOpenAiReasoningEffort( options?: SimpleStreamOptions, ): Effort | undefined { const reasoning = options?.reasoning; - if (!reasoning) return undefined; + if (!reasoning || !model.reasoning) return undefined; return requireSupportedEffort(model, reasoning); } @@ -424,9 +424,9 @@ function mapOptionsForApi( switch (model.api) { case "anthropic-messages": { - // Explicitly disable thinking when reasoning is not specified + // Explicitly disable thinking when reasoning is not specified or model doesn't support it const reasoning = options?.reasoning; - if (!reasoning) { + if (!reasoning || !model.reasoning) { return castApi<"anthropic-messages">({ ...base, thinkingEnabled: false, @@ -548,10 +548,10 @@ function mapOptionsForApi( }); case "google-generative-ai": { - // Explicitly disable thinking when reasoning is not specified + // Explicitly disable thinking when reasoning is not specified or model doesn't support it // This is needed because Gemini has "dynamic thinking" enabled by default const reasoning = options?.reasoning; - if (!reasoning) { + if (!reasoning || !model.reasoning) { return castApi<"google-generative-ai">({ ...base, thinking: { enabled: false }, @@ -587,7 +587,7 @@ function mapOptionsForApi( case "google-gemini-cli": { const reasoning = options?.reasoning; - if (!reasoning) { + if (!reasoning || !model.reasoning) { return castApi<"google-gemini-cli">({ ...base, thinking: { enabled: false }, @@ -637,9 +637,9 @@ function mapOptionsForApi( } case "google-vertex": { - // Explicitly disable thinking when reasoning is not specified + // Explicitly disable thinking when reasoning is not specified or model doesn't support it const reasoning = options?.reasoning; - if (!reasoning) { + if (!reasoning || !model.reasoning) { return castApi<"google-vertex">({ ...base, thinking: { enabled: false },