fix(translator): keep Gemini 2.5 Pro thinking and Kiro orphan tool errors

- claude-to-gemini: an enabled numeric budget_tokens keeps its priority over
  output_config.effort "none", and Gemini 2.5 Pro (which cannot switch
  thinking off and answers 400 to a zero budget) keeps its upstream default.
  The existing gemini-2.5-pro effort=none test goes back to its base form.
- openai-to-kiro: a failed tool result whose tool call is missing from the
  history keeps the [tool_error: true] line when the builder turns it into
  text, since the status field is dropped with toolResults.
This commit is contained in:
serenity
2026-11-18 22:53:04 -03:00
committed by diegosouzapw
parent 2f6cfeadc2
commit 5eba15a417
6 changed files with 104 additions and 30 deletions

View File

@@ -1 +1 @@
- **fix(gemini):** a Claude request with `thinking: {type: "disabled"}` or `output_config.effort: "none"` now switches thinking off for Gemini 2.5 models, as the OpenAI path does, instead of letting the model think and spend the output budget. ([#20206](https://github.com/cheaper-inference/OmniRoute/pull/20206))
- **fix(gemini):** a Claude request with `thinking: {type: "disabled"}` or `output_config.effort: "none"` now switches thinking off for Gemini 2.5 Flash models, as the OpenAI path does, instead of letting the model think and spend the output budget. Gemini 2.5 Pro cannot switch thinking off and keeps its default, and an enabled `budget_tokens` still wins over `effort: "none"`. ([#20206](https://github.com/cheaper-inference/OmniRoute/pull/20206))

View File

@@ -568,18 +568,22 @@ export function claudeToGeminiRequest(model, body, stream, credentials = null) {
// gemma-4 models returns - 400: Thinking budget is not supported for this model
} else if (
body.thinking?.type === "disabled" ||
(typeof body.output_config?.effort === "string" &&
// An enabled numeric budget keeps its priority over effort (see the priority note above).
(!(body.thinking?.type === "enabled" && typeof body.thinking.budget_tokens === "number") &&
typeof body.output_config?.effort === "string" &&
body.output_config.effort.toLowerCase() === "none")
) {
// The client asked for no thinking. Gemini 2.5 thinks dynamically when no thinkingConfig is
// sent, so switch it off explicitly, as the OpenAI path does for reasoning_effort "none".
// Models with a thinking-level knob or a zero budget cap (gemini-3 style) have no numeric
// off switch and reject a zero budget: leave them on the upstream default.
// off switch and reject a zero budget: leave them on the upstream default. Gemini 2.5 Pro
// cannot switch thinking off either (budget 128..32768, a zero budget answers 400).
const spec = getModelSpec(model);
if (
!isGeminiThinkingLevelModel(model) &&
!spec?.geminiThinkingLevels?.length &&
spec?.thinkingBudgetCap !== 0
spec?.thinkingBudgetCap !== 0 &&
!/gemini-2\.5-pro/i.test(model)
) {
result.generationConfig.thinkingConfig = buildGeminiThinkingConfig(model, 0, false);
}

View File

@@ -168,6 +168,24 @@ function hasFollowingToolResult(messages, index: number): boolean {
* Convert OpenAI messages to Kiro format
* Rules: system/tool/user -> user role, merge consecutive same roles
*/
type OrphanToolResult = {
toolUseId?: string;
status?: string;
content?: Array<{ text?: string }>;
};
/**
* Text for a tool result whose tool call is missing from the history (truncated conversation).
* The status is lost with the toolResults field, so a failed run keeps the `[tool_error: true]`
* line that claude-to-openai writes for it.
*/
function orphanToolResultText(tr: OrphanToolResult): string {
const id = tr.toolUseId || "";
const text = tr.content?.map((c) => c.text || "").join("\n") || "";
const body = tr.status === "error" ? `[tool_error: true]\n${text}` : text;
return id ? `[Tool Result (${id})]\n${body}` : `[Tool Result]\n${body}`;
}
function convertMessages(messages, tools, model) {
let history = [];
let currentMessage = null;
@@ -694,17 +712,9 @@ function convertMessages(messages, tools, model) {
prev?.assistantResponseMessage?.toolUses && prev.assistantResponseMessage.toolUses.length > 0;
if (!hasPrecedingAssistant) {
const toolResults = item.userInputMessage.userInputMessageContext.toolResults as Array<{
toolUseId?: string;
content?: Array<{ text?: string }>;
}>;
const toolResultTexts = toolResults
.map((tr) => {
const id = tr.toolUseId || "";
const text = tr.content?.map((c) => c.text || "").join("\n") || "";
return id ? `[Tool Result (${id})]\n${text}` : `[Tool Result]\n${text}`;
})
.join("\n\n");
const toolResults = item.userInputMessage.userInputMessageContext
.toolResults as OrphanToolResult[];
const toolResultTexts = toolResults.map(orphanToolResultText).join("\n\n");
const originalContent = item.userInputMessage.content || "";
item.userInputMessage.content = originalContent
@@ -727,14 +737,8 @@ function convertMessages(messages, tools, model) {
if (!hasPrecedingAssistant) {
const toolResults = currentMessage.userInputMessage.userInputMessageContext
.toolResults as Array<{ toolUseId?: string; content?: Array<{ text?: string }> }>;
const toolResultTexts = toolResults
.map((tr) => {
const id = tr.toolUseId || "";
const text = tr.content?.map((c) => c.text || "").join("\n") || "";
return id ? `[Tool Result (${id})]\n${text}` : `[Tool Result]\n${text}`;
})
.join("\n\n");
.toolResults as OrphanToolResult[];
const toolResultTexts = toolResults.map(orphanToolResultText).join("\n\n");
const originalContent = currentMessage.userInputMessage.content || "";
currentMessage.userInputMessage.content = originalContent

View File

@@ -52,3 +52,21 @@ test("guard: an enabled budget is unchanged", () => {
{ thinkingBudget: 2048, includeThoughts: true }
);
});
test("gemini-2.5-pro cannot switch thinking off, so it gets no zero budget", () => {
// Gemini 2.5 Pro takes a budget of 128..32768 and answers 400 to thinkingBudget 0.
for (const extra of [{ thinking: { type: "disabled" } }, { output_config: { effort: "none" } }]) {
assert.equal(thinkingConfig("gemini-2.5-pro", extra), undefined);
assert.equal(thinkingConfig("gemini-2.5-pro-preview-06-05", extra), undefined);
}
});
test("an enabled numeric budget keeps priority over output_config.effort none", () => {
assert.deepEqual(
thinkingConfig("gemini-2.5-flash", {
thinking: { type: "enabled", budget_tokens: 1024 },
output_config: { effort: "none" },
}),
{ thinkingBudget: 1024, includeThoughts: true }
);
});

View File

@@ -10,6 +10,7 @@ import assert from "node:assert/strict";
import { resetDbInstance } from "@/lib/db/core.ts";
import { FORMATS } from "@omniroute/open-sse/translator/formats.ts";
import { translateRequest } from "@omniroute/open-sse/translator/index.ts";
import { buildKiroPayload } from "@omniroute/open-sse/translator/request/openai-to-kiro.ts";
test.after(() => resetDbInstance());
@@ -76,3 +77,55 @@ test("a marker that is not at the start of the result text does not flip the sta
assert.equal(results?.[0].status, "success");
assert.equal(results?.[0].content[0].text, "log line\n[tool_error: true]\nmore");
});
// The orphan handlers sit in the Kiro builder itself; translateRequest drops an unpaired tool
// message before it gets there, so call the builder directly.
function kiroFromOpenAI(messages: unknown[]) {
return buildKiroPayload("claude-sonnet-4.5", { messages }, false, {}) as {
conversationState: {
currentMessage: {
userInputMessage: {
content: string;
userInputMessageContext?: { toolResults?: unknown[] };
};
};
history: Array<{ userInputMessage?: { content: string } }>;
};
};
}
test("an orphan failed tool result (truncated history) keeps the error marker in its text", () => {
// No assistant tool call precedes the marked tool message, so the Kiro builder turns the
// result into text. The text must still say the tool run failed.
const failed = { role: "tool", tool_call_id: "t1", content: "[tool_error: true]\nboom" };
const asCurrent = kiroFromOpenAI([
{ role: "user", content: "hi" },
{ role: "assistant", content: "sure" },
failed,
]).conversationState.currentMessage.userInputMessage;
assert.equal(asCurrent.userInputMessageContext?.toolResults, undefined);
assert.match(asCurrent.content, /\[Tool Result \(t1\)\]\n\[tool_error: true\]\nboom/);
const asHistory = kiroFromOpenAI([
{ role: "user", content: "hi" },
{ role: "assistant", content: "sure" },
failed,
{ role: "assistant", content: "noted" },
{ role: "user", content: "next" },
]).conversationState.history;
assert.ok(
asHistory.some((h) =>
/\[Tool Result \(t1\)\]\n\[tool_error: true\]\nboom/.test(h.userInputMessage?.content ?? "")
)
);
});
test("an orphan successful tool result gets no error marker", () => {
const current = kiroFromOpenAI([
{ role: "user", content: "hi" },
{ role: "assistant", content: "sure" },
{ role: "tool", tool_call_id: "t1", content: "ok" },
]).conversationState.currentMessage.userInputMessage;
assert.match(current.content, /\[Tool Result \(t1\)\]\nok/);
assert.doesNotMatch(current.content, /tool_error/);
});

View File

@@ -489,7 +489,7 @@ test("Claude -> Gemini prefers thinking.budget_tokens over output_config.effort"
});
});
test("Claude -> Gemini switches thinking off for output_config.effort=none", () => {
test("Claude -> Gemini skips thinkingConfig for output_config.effort=none", () => {
const result = claudeToGeminiRequest(
"gemini-2.5-pro",
{
@@ -499,12 +499,7 @@ test("Claude -> Gemini switches thinking off for output_config.effort=none", ()
false
);
// Gemini 2.5 thinks dynamically without a thinkingConfig, so "none" must be explicit
// (the OpenAI path sends the same for reasoning_effort "none").
assert.deepEqual((result.generationConfig as any).thinkingConfig, {
thinkingBudget: 0,
includeThoughts: false,
});
assert.equal((result.generationConfig as any).thinkingConfig, undefined);
});
// Regression for #3842: thinking.budget_tokens must be capped by the model's