Skip to content

Commit 12cddc9

Browse files
fix: validate Gemini thinkingLevel against model capabilities and handle empty streams (#11303)
* fix: validate Gemini thinkingLevel against model capabilities and handle empty streams getGeminiReasoning() now validates the selected effort against the model's supportsReasoningEffort array before sending it as thinkingLevel. When a stale settings value (e.g. 'medium' from a different model) is not in the supported set, it falls back to the model's default reasoningEffort. GeminiHandler.createMessage() now tracks whether any text content was yielded during streaming and handles NoOutputGeneratedError gracefully instead of surfacing the cryptic 'No output generated' error. * fix: guard thinkingLevel fallback against 'none' effort and add i18n TODO The array validation fallback in getGeminiReasoning() now only triggers when the selected effort IS a valid Gemini thinking level but not in the model's supported set. Values like 'none' (explicit no-reasoning signal) are no longer overridden by the model default. Also adds a TODO for moving the empty-stream message to i18n. * fix: track tool_call_start in hasContent to avoid false empty-stream warning Tool-only responses (no text) are valid content. Without this, agentic tool-call responses would incorrectly trigger the empty response warning message.
1 parent 7db4bfe commit 12cddc9

4 files changed

Lines changed: 248 additions & 6 deletions

File tree

src/api/providers/__tests__/gemini.spec.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// npx vitest run src/api/providers/__tests__/gemini.spec.ts
22

3+
import { NoOutputGeneratedError } from "ai"
4+
35
const mockCaptureException = vitest.fn()
46

57
vitest.mock("@roo-code/telemetry", () => ({
@@ -149,6 +151,84 @@ describe("GeminiHandler", () => {
149151
)
150152
})
151153

154+
it("should yield informative message when stream produces no text content", async () => {
155+
// Stream with only reasoning (no text-delta) simulates thinking-only response
156+
const mockFullStream = (async function* () {
157+
yield { type: "reasoning-delta", id: "1", text: "thinking..." }
158+
})()
159+
160+
mockStreamText.mockReturnValue({
161+
fullStream: mockFullStream,
162+
usage: Promise.resolve({ inputTokens: 10, outputTokens: 0 }),
163+
providerMetadata: Promise.resolve({}),
164+
})
165+
166+
const stream = handler.createMessage(systemPrompt, mockMessages)
167+
const chunks = []
168+
169+
for await (const chunk of stream) {
170+
chunks.push(chunk)
171+
}
172+
173+
// Should have: reasoning chunk, empty-stream informative message, usage
174+
const textChunks = chunks.filter((c) => c.type === "text")
175+
expect(textChunks).toHaveLength(1)
176+
expect(textChunks[0]).toEqual({
177+
type: "text",
178+
text: "Model returned an empty response. This may be caused by an unsupported thinking configuration or content filtering.",
179+
})
180+
})
181+
182+
it("should suppress NoOutputGeneratedError when no text content was yielded", async () => {
183+
// Empty stream - nothing yielded at all
184+
const mockFullStream = (async function* () {
185+
// empty stream
186+
})()
187+
188+
mockStreamText.mockReturnValue({
189+
fullStream: mockFullStream,
190+
usage: Promise.reject(new NoOutputGeneratedError({ message: "No output generated." })),
191+
providerMetadata: Promise.resolve({}),
192+
})
193+
194+
const stream = handler.createMessage(systemPrompt, mockMessages)
195+
const chunks = []
196+
197+
// Should NOT throw - the error is suppressed
198+
for await (const chunk of stream) {
199+
chunks.push(chunk)
200+
}
201+
202+
// Should have the informative empty-stream message only (no usage since it errored)
203+
const textChunks = chunks.filter((c) => c.type === "text")
204+
expect(textChunks).toHaveLength(1)
205+
expect(textChunks[0]).toMatchObject({
206+
type: "text",
207+
text: expect.stringContaining("empty response"),
208+
})
209+
})
210+
211+
it("should re-throw NoOutputGeneratedError when text content was yielded", async () => {
212+
// Stream yields text content but usage still throws NoOutputGeneratedError (unexpected)
213+
const mockFullStream = (async function* () {
214+
yield { type: "text-delta", text: "Hello" }
215+
})()
216+
217+
mockStreamText.mockReturnValue({
218+
fullStream: mockFullStream,
219+
usage: Promise.reject(new NoOutputGeneratedError({ message: "No output generated." })),
220+
providerMetadata: Promise.resolve({}),
221+
})
222+
223+
const stream = handler.createMessage(systemPrompt, mockMessages)
224+
225+
await expect(async () => {
226+
for await (const _chunk of stream) {
227+
// consume stream
228+
}
229+
}).rejects.toThrow()
230+
})
231+
152232
it("should handle API errors", async () => {
153233
const mockError = new Error("Gemini API error")
154234
// eslint-disable-next-line require-yield

src/api/providers/gemini.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Anthropic } from "@anthropic-ai/sdk"
22
import { createGoogleGenerativeAI, type GoogleGenerativeAIProvider } from "@ai-sdk/google"
3-
import { streamText, generateText, ToolSet } from "ai"
3+
import { streamText, generateText, NoOutputGeneratedError, ToolSet } from "ai"
44

55
import {
66
type ModelInfo,
@@ -131,6 +131,9 @@ export class GeminiHandler extends BaseProvider implements SingleCompletionHandl
131131
// Use streamText for streaming responses
132132
const result = streamText(requestOptions)
133133

134+
// Track whether any text content was yielded (not just reasoning/thinking)
135+
let hasContent = false
136+
134137
// Process the full stream to get all events including reasoning
135138
for await (const part of result.fullStream) {
136139
// Capture thoughtSignature from tool-call events (Gemini 3 thought signatures)
@@ -143,10 +146,22 @@ export class GeminiHandler extends BaseProvider implements SingleCompletionHandl
143146
}
144147

145148
for (const chunk of processAiSdkStreamPart(part)) {
149+
if (chunk.type === "text" || chunk.type === "tool_call_start") {
150+
hasContent = true
151+
}
146152
yield chunk
147153
}
148154
}
149155

156+
// If the stream completed without yielding any text content, inform the user
157+
// TODO: Move to i18n key common:errors.gemini.empty_response once translation pipeline is updated
158+
if (!hasContent) {
159+
yield {
160+
type: "text" as const,
161+
text: "Model returned an empty response. This may be caused by an unsupported thinking configuration or content filtering.",
162+
}
163+
}
164+
150165
// Extract grounding sources from providerMetadata if available
151166
const providerMetadata = await result.providerMetadata
152167
const groundingMetadata = providerMetadata?.google as
@@ -167,9 +182,23 @@ export class GeminiHandler extends BaseProvider implements SingleCompletionHandl
167182
}
168183

169184
// Yield usage metrics at the end
170-
const usage = await result.usage
171-
if (usage) {
172-
yield this.processUsageMetrics(usage, info, providerMetadata)
185+
// Wrap in try-catch to handle NoOutputGeneratedError thrown by the AI SDK
186+
// when the stream produces no output (e.g., thinking-only, safety block)
187+
try {
188+
const usage = await result.usage
189+
if (usage) {
190+
yield this.processUsageMetrics(usage, info, providerMetadata)
191+
}
192+
} catch (usageError) {
193+
if (usageError instanceof NoOutputGeneratedError) {
194+
// If we already yielded the empty-stream message, suppress this error
195+
if (hasContent) {
196+
throw usageError
197+
}
198+
// Otherwise the informative message was already yielded above — no-op
199+
} else {
200+
throw usageError
201+
}
173202
}
174203
} catch (error) {
175204
const errorMessage = error instanceof Error ? error.message : String(error)

src/api/transform/__tests__/reasoning.spec.ts

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@ describe("reasoning.ts", () => {
765765
}
766766

767767
const result = getGeminiReasoning(options)
768+
// "none" is not a valid GeminiThinkingLevel, so no fallback — returns undefined
768769
expect(result).toBeUndefined()
769770
})
770771

@@ -838,6 +839,128 @@ describe("reasoning.ts", () => {
838839
const result = getGeminiReasoning(options) as GeminiReasoningParams | undefined
839840
expect(result).toEqual({ thinkingLevel: "medium", includeThoughts: true })
840841
})
842+
843+
it("should fall back to model default when settings effort is not in supportsReasoningEffort array", () => {
844+
// Simulates gemini-3-pro-preview which only supports ["low", "high"]
845+
// but user has reasoningEffort: "medium" from a different model
846+
const geminiModel: ModelInfo = {
847+
...baseModel,
848+
supportsReasoningEffort: ["low", "high"] as ModelInfo["supportsReasoningEffort"],
849+
reasoningEffort: "low",
850+
}
851+
852+
const settings: ProviderSettings = {
853+
apiProvider: "gemini",
854+
reasoningEffort: "medium",
855+
}
856+
857+
const options: GetModelReasoningOptions = {
858+
model: geminiModel,
859+
reasoningBudget: undefined,
860+
reasoningEffort: "medium",
861+
settings,
862+
}
863+
864+
const result = getGeminiReasoning(options) as GeminiReasoningParams | undefined
865+
// "medium" is not in ["low", "high"], so falls back to model.reasoningEffort "low"
866+
expect(result).toEqual({ thinkingLevel: "low", includeThoughts: true })
867+
})
868+
869+
it("should return undefined when unsupported effort and model default is also invalid", () => {
870+
const geminiModel: ModelInfo = {
871+
...baseModel,
872+
supportsReasoningEffort: ["low", "high"] as ModelInfo["supportsReasoningEffort"],
873+
// No reasoningEffort default set
874+
}
875+
876+
const settings: ProviderSettings = {
877+
apiProvider: "gemini",
878+
reasoningEffort: "medium",
879+
}
880+
881+
const options: GetModelReasoningOptions = {
882+
model: geminiModel,
883+
reasoningBudget: undefined,
884+
reasoningEffort: "medium",
885+
settings,
886+
}
887+
888+
const result = getGeminiReasoning(options)
889+
// "medium" is not in ["low", "high"], fallback is undefined → returns undefined
890+
expect(result).toBeUndefined()
891+
})
892+
893+
it("should pass through effort that IS in the supportsReasoningEffort array", () => {
894+
const geminiModel: ModelInfo = {
895+
...baseModel,
896+
supportsReasoningEffort: ["low", "high"] as ModelInfo["supportsReasoningEffort"],
897+
reasoningEffort: "low",
898+
}
899+
900+
const settings: ProviderSettings = {
901+
apiProvider: "gemini",
902+
reasoningEffort: "high",
903+
}
904+
905+
const options: GetModelReasoningOptions = {
906+
model: geminiModel,
907+
reasoningBudget: undefined,
908+
reasoningEffort: "high",
909+
settings,
910+
}
911+
912+
const result = getGeminiReasoning(options) as GeminiReasoningParams | undefined
913+
// "high" IS in ["low", "high"], so it should be used directly
914+
expect(result).toEqual({ thinkingLevel: "high", includeThoughts: true })
915+
})
916+
917+
it("should skip validation when supportsReasoningEffort is boolean (not array)", () => {
918+
const geminiModel: ModelInfo = {
919+
...baseModel,
920+
supportsReasoningEffort: true,
921+
reasoningEffort: "low",
922+
}
923+
924+
const settings: ProviderSettings = {
925+
apiProvider: "gemini",
926+
reasoningEffort: "medium",
927+
}
928+
929+
const options: GetModelReasoningOptions = {
930+
model: geminiModel,
931+
reasoningBudget: undefined,
932+
reasoningEffort: "medium",
933+
settings,
934+
}
935+
936+
const result = getGeminiReasoning(options) as GeminiReasoningParams | undefined
937+
// boolean supportsReasoningEffort should not trigger array validation
938+
expect(result).toEqual({ thinkingLevel: "medium", includeThoughts: true })
939+
})
940+
941+
it("should fall back to model default when settings has 'minimal' but model only supports ['low', 'high']", () => {
942+
const geminiModel: ModelInfo = {
943+
...baseModel,
944+
supportsReasoningEffort: ["low", "high"] as ModelInfo["supportsReasoningEffort"],
945+
reasoningEffort: "low",
946+
}
947+
948+
const settings: ProviderSettings = {
949+
apiProvider: "gemini",
950+
reasoningEffort: "minimal",
951+
}
952+
953+
const options: GetModelReasoningOptions = {
954+
model: geminiModel,
955+
reasoningBudget: undefined,
956+
reasoningEffort: "minimal",
957+
settings,
958+
}
959+
960+
const result = getGeminiReasoning(options) as GeminiReasoningParams | undefined
961+
// "minimal" is not in ["low", "high"], falls back to "low"
962+
expect(result).toEqual({ thinkingLevel: "low", includeThoughts: true })
963+
})
841964
})
842965

843966
describe("Integration scenarios", () => {

src/api/transform/reasoning.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,20 @@ export const getGeminiReasoning = ({
150150
return undefined
151151
}
152152

153+
// Validate that the selected effort is supported by this specific model.
154+
// e.g. gemini-3-pro-preview only supports ["low", "high"] — sending
155+
// "medium" (carried over from a different model's settings) causes errors.
156+
const effortToUse =
157+
Array.isArray(model.supportsReasoningEffort) &&
158+
isGeminiThinkingLevel(selectedEffort) &&
159+
!model.supportsReasoningEffort.includes(selectedEffort)
160+
? model.reasoningEffort
161+
: selectedEffort
162+
153163
// Effort-based models on Google GenAI support minimal/low/medium/high levels.
154-
if (!isGeminiThinkingLevel(selectedEffort)) {
164+
if (!effortToUse || !isGeminiThinkingLevel(effortToUse)) {
155165
return undefined
156166
}
157167

158-
return { thinkingLevel: selectedEffort, includeThoughts: true }
168+
return { thinkingLevel: effortToUse, includeThoughts: true }
159169
}

0 commit comments

Comments
 (0)