fix(ai): strip thinking field from history messages on ollama-cloud

Ollama cloud rejects requests (HTTP 400) when assistant history messages
contain the `thinking` field. The field is valid as output from the
model (streamed thinking blocks), but the cloud API does not accept it
as an input in conversation history.

Root-cause trace: a kimi-k2.6 response containing both `thinking` and
`tool_calls` was stored in history. The following request echoed it
back, triggering a 400. Requests containing only `tool_calls` (no
`thinking`) succeeded, isolating the `thinking` field as the cause.

Strip the field from converted assistant messages when the provider is
`ollama-cloud`. Local Ollama instances (provider `ollama`) are unaffected
and continue to receive `thinking` in history for models that support it.
This commit is contained in:
VoidChecksum
2026-05-10 23:52:31 +02:00
committed by can1357
parent 6d16e93b86
commit 5586cddd5d
+12 -1
View File
@@ -199,7 +199,18 @@ function convertMessages(model: Model<"ollama-chat">, context: Context): OllamaM
});
}
messages.push(...context.messages);
return transformMessages(messages, model).map(convertMessage);
const isCloud = model.provider === "ollama-cloud";
return transformMessages(messages, model).map(msg => {
const converted = convertMessage(msg);
// Ollama cloud rejects requests when assistant history messages contain the `thinking`
// field — it's valid in model responses but not accepted as a history input. Strip it
// to prevent HTTP 400 errors. Local Ollama instances are unaffected.
if (isCloud && converted.role === "assistant" && converted.thinking) {
const { thinking: _t, ...rest } = converted;
return rest;
}
return converted;
});
}
function convertTools(tools: Tool[] | undefined): OllamaFunctionTool[] | undefined {