Skip to content

Commit 9f13199

Browse files
committed
fix: mitigate Kimi K2.5 premature end_turn on Bedrock Converse API
Bedrock's Converse API intermittently fails to parse Kimi's tool calls, leaking internal tokens (<|tool_call_begin|>, etc.) into text output and returning end_turn instead of tool-calls (vercel/ai#11409). Three mitigations: 1. Add <|tool_call_begin|> as a stop sequence for Kimi on Bedrock to halt generation before a full malformed tool call is emitted 2. Detect and strip leaked tool tokens from text output in the processor 3. Override finish reason to "unknown" when leaked tokens or the stop sequence are detected, so the agent loop retries instead of stopping Fixes anomalyco#13807 https://claude.ai/code/session_01XXhheUVe47XQpxu2MsTdGX
1 parent 993f0a1 commit 9f13199

4 files changed

Lines changed: 164 additions & 1 deletion

File tree

‎packages/opencode/src/provider/transform.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,17 @@ export namespace ProviderTransform {
671671
return schema
672672
}
673673

674+
export function stopSequences(model: Provider.Model): string[] | undefined {
675+
// Kimi on Bedrock: Converse API intermittently leaks internal tool-call tokens
676+
// into text output instead of parsing them as tool calls (vercel/ai#11409).
677+
// Adding these as stop sequences halts generation before the full malformed
678+
// tool call is emitted, giving the agent loop a chance to retry.
679+
if (model.api.npm === "@ai-sdk/amazon-bedrock" && model.api.id.includes("kimi")) {
680+
return ["<|tool_call_begin|>"]
681+
}
682+
return undefined
683+
}
684+
674685
export function error(providerID: string, error: APICallError) {
675686
let message = error.message
676687
if (providerID === "github-copilot" && message.includes("The requested model is not supported")) {

‎packages/opencode/src/session/llm.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export namespace LLM {
167167
temperature: params.temperature,
168168
topP: params.topP,
169169
topK: params.topK,
170+
stopSequences: ProviderTransform.stopSequences(input.model),
170171
providerOptions: ProviderTransform.providerOptions(input.model, params.options),
171172
activeTools: Object.keys(tools).filter((x) => x !== "invalid"),
172173
tools,

‎packages/opencode/src/session/processor.ts‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ export namespace SessionProcessor {
2020
const DOOM_LOOP_THRESHOLD = 3
2121
const log = Log.create({ service: "session.processor" })
2222

23+
// Kimi internal tokens that leak into text when Bedrock's Converse API
24+
// fails to parse tool calls (see: vercel/ai#11409)
25+
const LEAKED_TOOL_TOKEN_RE = /<\|tool_call_begin\|>|<\|tool_sep\|>|<\|tool_call_end\|>|<\|im_end\|>/g
26+
27+
function isKimiOnBedrock(model: Provider.Model): boolean {
28+
return model.api.npm === "@ai-sdk/amazon-bedrock" && model.api.id.includes("kimi")
29+
}
30+
31+
function stripLeakedToolTokens(text: string): { text: string; hadLeakedTokens: boolean } {
32+
// Strip everything from the first leaked token onwards, since it's a malformed tool call
33+
const firstMatch = text.search(LEAKED_TOOL_TOKEN_RE)
34+
if (firstMatch === -1) return { text, hadLeakedTokens: false }
35+
return { text: text.slice(0, firstMatch).trimEnd(), hadLeakedTokens: true }
36+
}
37+
2338
export type Info = Awaited<ReturnType<typeof create>>
2439
export type Result = Awaited<ReturnType<Info["process"]>>
2540

@@ -34,6 +49,8 @@ export namespace SessionProcessor {
3449
let blocked = false
3550
let attempt = 0
3651
let needsCompaction = false
52+
let leakedToolTokens = false
53+
const kimiOnBedrock = isKimiOnBedrock(input.model)
3754

3855
const result = {
3956
get message() {
@@ -239,7 +256,25 @@ export namespace SessionProcessor {
239256
usage: value.usage,
240257
metadata: value.providerMetadata,
241258
})
242-
input.assistantMessage.finish = value.finishReason
259+
// Kimi on Bedrock: the Converse API intermittently fails to parse tool
260+
// calls, leaking internal tokens into text (vercel/ai#11409).
261+
// Override the finish reason to "unknown" so the agent loop retries when:
262+
// 1. Leaked tool tokens were detected in text output, OR
263+
// 2. The stop sequence (<|tool_call_begin|>) fired (finish = "stop")
264+
if (
265+
kimiOnBedrock &&
266+
value.finishReason !== "tool-calls" &&
267+
(leakedToolTokens || value.finishReason === "stop")
268+
) {
269+
log.warn("overriding finish reason due to leaked Kimi tool tokens", {
270+
original: value.finishReason,
271+
sessionID: input.sessionID,
272+
})
273+
input.assistantMessage.finish = "unknown"
274+
leakedToolTokens = false
275+
} else {
276+
input.assistantMessage.finish = value.finishReason
277+
}
243278
input.assistantMessage.cost += usage.cost
244279
input.assistantMessage.tokens = usage.tokens
245280
await Session.updatePart({
@@ -305,6 +340,19 @@ export namespace SessionProcessor {
305340
case "text-end":
306341
if (currentText) {
307342
currentText.text = currentText.text.trimEnd()
343+
344+
// Kimi on Bedrock: strip leaked internal tool-call tokens from text
345+
if (kimiOnBedrock) {
346+
const stripped = stripLeakedToolTokens(currentText.text)
347+
if (stripped.hadLeakedTokens) {
348+
leakedToolTokens = true
349+
currentText.text = stripped.text
350+
log.warn("stripped leaked Kimi tool tokens from text", {
351+
sessionID: input.sessionID,
352+
})
353+
}
354+
}
355+
308356
const textOutput = await Plugin.trigger(
309357
"experimental.text.complete",
310358
{

‎packages/opencode/test/provider/transform.test.ts‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,40 @@ describe("ProviderTransform.variants", () => {
10151015
},
10161016
})
10171017
})
1018+
1019+
test("kimi models return empty variants (always-on reasoning)", () => {
1020+
const model = createMockModel({
1021+
id: "bedrock/kimi-k2.5",
1022+
providerID: "amazon-bedrock",
1023+
api: {
1024+
id: "moonshotai.kimi-k2.5",
1025+
url: "https://bedrock.amazonaws.com",
1026+
npm: "@ai-sdk/amazon-bedrock",
1027+
},
1028+
})
1029+
const result = ProviderTransform.variants(model)
1030+
expect(result).toEqual({})
1031+
})
1032+
1033+
test("anthropic models on bedrock return reasoningConfig variants", () => {
1034+
const model = createMockModel({
1035+
id: "bedrock/claude-4",
1036+
providerID: "amazon-bedrock",
1037+
api: {
1038+
id: "anthropic.claude-4",
1039+
url: "https://bedrock.amazonaws.com",
1040+
npm: "@ai-sdk/amazon-bedrock",
1041+
},
1042+
})
1043+
const result = ProviderTransform.variants(model)
1044+
expect(Object.keys(result)).toEqual(["high", "max"])
1045+
expect(result.high).toEqual({
1046+
reasoningConfig: {
1047+
type: "enabled",
1048+
budgetTokens: 16000,
1049+
},
1050+
})
1051+
})
10181052
})
10191053

10201054
describe("@ai-sdk/google", () => {
@@ -1153,3 +1187,72 @@ describe("ProviderTransform.variants", () => {
11531187
})
11541188
})
11551189
})
1190+
1191+
describe("ProviderTransform.stopSequences", () => {
1192+
const createMockModel = (overrides: Partial<any> = {}): any => ({
1193+
id: "test/test-model",
1194+
providerID: "test",
1195+
api: {
1196+
id: "test-model",
1197+
url: "https://api.test.com",
1198+
npm: "@ai-sdk/openai",
1199+
},
1200+
...overrides,
1201+
})
1202+
1203+
test("returns stop sequences for Kimi on Bedrock", () => {
1204+
const model = createMockModel({
1205+
id: "bedrock/kimi-k2.5",
1206+
providerID: "amazon-bedrock",
1207+
api: {
1208+
id: "moonshotai.kimi-k2.5",
1209+
url: "https://bedrock.amazonaws.com",
1210+
npm: "@ai-sdk/amazon-bedrock",
1211+
},
1212+
})
1213+
const result = ProviderTransform.stopSequences(model)
1214+
expect(result).toEqual(["<|tool_call_begin|>"])
1215+
})
1216+
1217+
test("returns undefined for non-Kimi Bedrock models", () => {
1218+
const model = createMockModel({
1219+
id: "bedrock/claude-4",
1220+
providerID: "amazon-bedrock",
1221+
api: {
1222+
id: "anthropic.claude-4",
1223+
url: "https://bedrock.amazonaws.com",
1224+
npm: "@ai-sdk/amazon-bedrock",
1225+
},
1226+
})
1227+
const result = ProviderTransform.stopSequences(model)
1228+
expect(result).toBeUndefined()
1229+
})
1230+
1231+
test("returns undefined for Kimi on non-Bedrock providers", () => {
1232+
const model = createMockModel({
1233+
id: "opencode/kimi-k2.5",
1234+
providerID: "opencode",
1235+
api: {
1236+
id: "kimi-k2.5",
1237+
url: "https://api.opencode.ai",
1238+
npm: "@ai-sdk/openai-compatible",
1239+
},
1240+
})
1241+
const result = ProviderTransform.stopSequences(model)
1242+
expect(result).toBeUndefined()
1243+
})
1244+
1245+
test("returns undefined for other providers", () => {
1246+
const model = createMockModel({
1247+
id: "openai/gpt-5",
1248+
providerID: "openai",
1249+
api: {
1250+
id: "gpt-5",
1251+
url: "https://api.openai.com",
1252+
npm: "@ai-sdk/openai",
1253+
},
1254+
})
1255+
const result = ProviderTransform.stopSequences(model)
1256+
expect(result).toBeUndefined()
1257+
})
1258+
})

0 commit comments

Comments
 (0)