fix(translator): map Claude stop_sequences and stop to Gemini stopSequences (#12785)

This commit is contained in:
MSiva
2026-09-18 19:59:15 +05:30
committed by GitHub
parent 0551893390
commit 0e7925309d
2 changed files with 36 additions and 4 deletions

View File

@@ -1,9 +1,6 @@
import { register } from "../registry.ts";
import { FORMATS } from "../formats.ts";
import {
DEFAULT_SAFETY_SETTINGS,
cleanJSONSchemaForAntigravity,
} from "../helpers/geminiHelper.ts";
import { DEFAULT_SAFETY_SETTINGS, cleanJSONSchemaForAntigravity } from "../helpers/geminiHelper.ts";
import { buildGeminiTools, sanitizeGeminiToolName } from "../helpers/geminiToolsSanitizer.ts";
import {
buildGeminiThoughtSignatureKey,
@@ -83,6 +80,10 @@ export function claudeToGeminiRequest(model, body, stream, credentials = null) {
result.generationConfig.maxOutputTokens = maxOutputTokens;
}
}
if (body.stop_sequences !== undefined || body.stop !== undefined) {
const rawStop = body.stop_sequences ?? body.stop;
result.generationConfig.stopSequences = Array.isArray(rawStop) ? rawStop : [rawStop];
}
// ── System instruction ─────────────────────────────────────────
if (body.system) {

View File

@@ -486,3 +486,34 @@ test("Claude -> Gemini non-numeric budget_tokens falls through to effort path",
includeThoughts: true,
});
});
test("Claude -> Gemini maps stop_sequences and stop to generationConfig.stopSequences", () => {
const result1 = claudeToGeminiRequest(
"gemini-2.5-pro",
{
messages: [{ role: "user", content: [{ type: "text", text: "hi" }] }],
stop_sequences: ["STOP", "\nHuman:"],
},
false
);
assert.deepEqual(result1.generationConfig.stopSequences, ["STOP", "\nHuman:"]);
const result2 = claudeToGeminiRequest(
"gemini-2.5-pro",
{
messages: [{ role: "user", content: [{ type: "text", text: "hi" }] }],
stop: "SINGLE_STOP",
},
false
);
assert.deepEqual(result2.generationConfig.stopSequences, ["SINGLE_STOP"]);
const result3 = claudeToGeminiRequest(
"gemini-2.5-pro",
{
messages: [{ role: "user", content: [{ type: "text", text: "hi" }] }],
},
false
);
assert.strictEqual(result3.generationConfig.stopSequences, undefined);
});