Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- OpenRouter streamed chunks carry reasoning as `delta.reasoning` and `reasoning_details` (#663)
- Recording an OpenRouter stream keeps its `delta.reasoning` text in the fixture (#664)
- Gemini matches tool-result fixtures sent as snake_case `function_call`/`function_response` (#662)

## [1.49.0] - 2026-11-30

Expand Down
100 changes: 100 additions & 0 deletions src/__tests__/gemini-snake-case-parts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { describe, it, expect, afterEach } from "vitest";
import * as http from "node:http";
import type { Fixture } from "../types.js";
import { createServer, type ServerInstance } from "../server.js";

// Gemini's REST API accepts proto field names in snake_case as well as
// camelCase, and some clients (LiteLLM's Gemini / Vertex transformation, which
// Google ADK's LiteLlm uses) send `function_call` / `function_response`.
// See #658.

function post(url: string, body: unknown): Promise<{ status: number; body: string }> {
return new Promise((resolve, reject) => {
const data = JSON.stringify(body);
const parsed = new URL(url);
const req = http.request(
{
hostname: parsed.hostname,
port: parsed.port,
path: parsed.pathname + parsed.search,
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(data),
},
},
(res) => {
const chunks: Buffer[] = [];
res.on("data", (c: Buffer) => chunks.push(c));
res.on("end", () => {
resolve({ status: res.statusCode ?? 0, body: Buffer.concat(chunks).toString() });
});
},
);
req.on("error", reject);
req.write(data);
req.end();
});
}

const fixtures: Fixture[] = [
{
match: {
userMessage: "t1:",
hasToolResult: true,
toolResultContains: "partial=14",
model: "gem",
},
response: { content: "ANSWER" },
},
];

function contents(snake: boolean): unknown[] {
const call = { name: "crashing_tool", args: { value: 7 } };
const result = {
name: "crashing_tool",
response: { content: [{ type: "text", text: "crash after partial=14" }] },
};
return [
{ role: "user", parts: [{ text: "t1: call the tool" }] },
{ role: "model", parts: [snake ? { function_call: call } : { functionCall: call }] },
{
role: "user",
parts: [snake ? { function_response: result } : { functionResponse: result }],
},
];
}

let instance: ServerInstance | null = null;

afterEach(async () => {
if (instance) {
await new Promise<void>((resolve) => instance!.server.close(() => resolve()));
instance = null;
}
});

describe("Gemini snake_case function_call / function_response parts (#658)", () => {
for (const [label, snake] of [
["snake_case", true],
["camelCase (control)", false],
] as const) {
it(`streamGenerateContent matches a tool-result fixture with ${label} parts`, async () => {
instance = await createServer(fixtures);
const res = await post(`${instance.url}/v1beta/models/gem:streamGenerateContent?alt=sse`, {
contents: contents(snake),
});
expect(res.status).toBe(200);
expect(res.body).toContain("ANSWER");
});

it(`generateContent matches a tool-result fixture with ${label} parts`, async () => {
instance = await createServer(fixtures);
const res = await post(`${instance.url}/v1beta/models/gem:generateContent`, {
contents: contents(snake),
});
expect(res.status).toBe(200);
expect(JSON.parse(res.body).candidates[0].content.parts[0].text).toBe("ANSWER");
});
}
});
20 changes: 18 additions & 2 deletions src/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ interface GeminiPart {
functionCall?: { name: string; args: Record<string, unknown>; id?: string };
functionResponse?: { name: string; response: unknown; id?: string };
inlineData?: { mimeType: string; data: string };
// Snake_case spellings the Gemini API also accepts (#658).
function_call?: GeminiPart["functionCall"];
function_response?: GeminiPart["functionResponse"];
}

// Map the snake_case function_call / function_response keys onto their
// camelCase names. Parts that already use camelCase are returned unchanged.
function normalizeGeminiPart(p: GeminiPart): GeminiPart {
if (p === null || typeof p !== "object") return p;
if (p.functionCall === undefined && p.function_call !== undefined) {
p = { ...p, functionCall: p.function_call };
}
if (p.functionResponse === undefined && p.function_response !== undefined) {
p = { ...p, functionResponse: p.function_response };
}
return p;
}

interface GeminiContent {
Expand Down Expand Up @@ -178,7 +194,7 @@ export function geminiToCompletionRequest(

if (role === "user") {
// Check for functionResponse parts
const parts = content.parts ?? [];
const parts = (content.parts ?? []).map(normalizeGeminiPart);
const funcResponses = parts.filter(
(p) => p !== null && p !== undefined && p.functionResponse,
);
Expand Down Expand Up @@ -224,7 +240,7 @@ export function geminiToCompletionRequest(
}
} else if (role === "model") {
// Check for functionCall parts
const parts = content.parts ?? [];
const parts = (content.parts ?? []).map(normalizeGeminiPart);
const funcCalls = parts.filter((p) => p !== null && p !== undefined && p.functionCall);
const textParts = parts.filter(
(p) => p !== null && p !== undefined && p.text !== undefined && !p.thought,
Expand Down
Loading