Skip to content

Commit b0fdbc7

Browse files
zoomote[bot]taltas
andauthored
[Fix] Vertex Gemini 3.7 fails after tools return empty output (#1250)
* fix(gemini): preserve empty tool responses * test(gemini): type empty result fixture * test(gemini): cover empty tool result arrays --------- Co-authored-by: @taltas <[email protected]>
1 parent d7795ca commit b0fdbc7

4 files changed

Lines changed: 98 additions & 13 deletions

File tree

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,22 @@ import { Anthropic } from "@anthropic-ai/sdk"
1515
import { type ModelInfo, geminiDefaultModelId, ApiProviderError } from "@roo-code/types"
1616

1717
import { t } from "i18next"
18+
import type { ApiHandlerCreateMessageMetadata } from "../../index"
1819
import { GeminiHandler } from "../gemini"
1920
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
2021

2122
const GEMINI_MODEL_NAME = geminiDefaultModelId
2223

2324
describe("GeminiHandler", () => {
2425
let handler: GeminiHandler
26+
let mockGenerateContentStream: ReturnType<typeof vitest.fn>
2527

2628
beforeEach(() => {
2729
// Reset mocks
2830
mockCaptureException.mockClear()
2931

3032
// Create mock functions
31-
const mockGenerateContentStream = vitest.fn()
33+
mockGenerateContentStream = vitest.fn()
3234
const mockGenerateContent = vitest.fn()
3335
const mockGetGenerativeModel = vitest.fn()
3436

@@ -293,6 +295,43 @@ describe("GeminiHandler", () => {
293295
)
294296
})
295297

298+
it("should keep an empty tool result as the final user turn", async () => {
299+
const messages: Anthropic.Messages.MessageParam[] = [
300+
{ role: "user", content: "Run the tool" },
301+
{
302+
role: "assistant",
303+
content: [{ type: "tool_use", id: "call-1", name: "read_file", input: { path: "empty.txt" } }],
304+
},
305+
{
306+
role: "user",
307+
content: [{ type: "tool_result", tool_use_id: "call-1", content: "" }],
308+
},
309+
]
310+
const metadata = {
311+
taskId: "test-task",
312+
tools: [{ type: "function", function: { name: "read_file", description: "", parameters: {} } }],
313+
} satisfies ApiHandlerCreateMessageMetadata
314+
315+
mockGenerateContentStream.mockResolvedValue(
316+
asyncStreamFrom([{ candidates: [{ content: { parts: [{ text: "Done" }] } }] }]),
317+
)
318+
319+
await collectStream(handler.createMessage(systemPrompt, messages, metadata))
320+
321+
const params = mockGenerateContentStream.mock.calls[0][0]
322+
expect(params.contents.at(-1)).toEqual({
323+
role: "user",
324+
parts: [
325+
{
326+
functionResponse: {
327+
name: "read_file",
328+
response: { name: "read_file", content: "(empty)" },
329+
},
330+
},
331+
],
332+
})
333+
})
334+
296335
it("should handle API errors", async () => {
297336
const mockError = new Error("Gemini API error")
298337
;(handler["client"].models.generateContentStream as any).mockRejectedValue(mockError)

‎src/api/transform/__tests__/gemini-format.spec.ts‎

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,22 +203,64 @@ describe("convertAnthropicMessageToGemini", () => {
203203
])
204204
})
205205

206-
it("should handle empty tool result content", () => {
206+
it("should preserve an empty tool result as a user function response", () => {
207+
const toolIdToName = new Map([["calculator-123", "calculator"]])
207208
const anthropicMessage: Anthropic.Messages.MessageParam = {
208209
role: "user",
209210
content: [
210211
{
211212
type: "tool_result",
212213
tool_use_id: "calculator-123",
213-
content: null as any, // Empty content
214+
content: "",
214215
},
215216
],
216217
}
217218

218-
const result = convertAnthropicMessageToGemini(anthropicMessage)
219+
const result = convertAnthropicMessageToGemini(anthropicMessage, { toolIdToName })
220+
221+
expect(result).toEqual([
222+
{
223+
role: "user",
224+
parts: [
225+
{
226+
functionResponse: {
227+
name: "calculator",
228+
response: { name: "calculator", content: "(empty)" },
229+
},
230+
},
231+
],
232+
},
233+
])
234+
})
235+
236+
it("should preserve an empty tool result array as a user function response", () => {
237+
const toolIdToName = new Map([["calculator-123", "calculator"]])
238+
const anthropicMessage: Anthropic.Messages.MessageParam = {
239+
role: "user",
240+
content: [
241+
{
242+
type: "tool_result",
243+
tool_use_id: "calculator-123",
244+
content: [],
245+
},
246+
],
247+
}
248+
249+
const result = convertAnthropicMessageToGemini(anthropicMessage, { toolIdToName })
219250

220-
// Should skip the empty tool result
221-
expect(result).toEqual([])
251+
expect(result).toEqual([
252+
{
253+
role: "user",
254+
parts: [
255+
{
256+
functionResponse: {
257+
name: "calculator",
258+
response: { name: "calculator", content: "(empty)" },
259+
},
260+
},
261+
],
262+
},
263+
])
222264
})
223265

224266
it("should convert a message with tool result as array with text only", () => {

‎src/api/transform/gemini-format.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,6 @@ export function convertAnthropicContentToGemini(
9393
...(functionCallSignature ? { thoughtSignature: functionCallSignature } : {}),
9494
} as Part
9595
case "tool_result": {
96-
if (!block.content) {
97-
return []
98-
}
99-
10096
// Get tool name from the map (built from tool_use blocks in message history).
10197
// The map must contain the tool name - if it doesn't, this indicates a bug
10298
// where the conversation history is incomplete or tool_use blocks are missing.
@@ -111,7 +107,10 @@ export function convertAnthropicContentToGemini(
111107

112108
if (typeof block.content === "string") {
113109
return {
114-
functionResponse: { name: toolName, response: { name: toolName, content: block.content } },
110+
functionResponse: {
111+
name: toolName,
112+
response: { name: toolName, content: block.content || "(empty)" },
113+
},
115114
}
116115
}
117116

@@ -137,7 +136,12 @@ export function convertAnthropicContentToGemini(
137136

138137
// Return function response followed by any images
139138
return [
140-
{ functionResponse: { name: toolName, response: { name: toolName, content: contentText } } },
139+
{
140+
functionResponse: {
141+
name: toolName,
142+
response: { name: toolName, content: contentText || "(empty)" },
143+
},
144+
},
141145
...imageParts,
142146
]
143147
}

‎src/eslint-suppressions.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@
471471
},
472472
"api/transform/__tests__/gemini-format.spec.ts": {
473473
"@typescript-eslint/no-explicit-any": {
474-
"count": 6
474+
"count": 5
475475
}
476476
},
477477
"api/transform/__tests__/mistral-format.spec.ts": {

0 commit comments

Comments
 (0)