Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(model-runtime): echo reasoning back for GLM and Kimi thinking models (#19008)

Arvin Xu49d1a94110f5
diff --git a/packages/model-runtime/src/core/contextBuilders/openai.test.ts b/packages/model-runtime/src/core/contextBuilders/openai.test.ts
index b583cbef084..76bbf619606 100644
--- a/packages/model-runtime/src/core/contextBuilders/openai.test.ts
+++ b/packages/model-runtime/src/core/contextBuilders/openai.test.ts
@@ -428,6 +428,47 @@ describe('convertOpenAIMessages', () => {
expect((result[0] as any).reasoning).toBeUndefined();
});
+ describe('reasoning passback for thinking families', () => {
+ // Upstream rejects a thinking-mode turn that carries tool_calls without the
+ // historical reasoning: "If thinking mode and tool_calls, `reasoning_content`
+ // must be passed back to the API."
+ const assistantTurn = {
+ content: 'Calling a tool',
+ reasoning: { content: 'step-by-step thinking' },
+ role: 'assistant',
+ tool_calls: [
+ { function: { arguments: '{}', name: 'search' }, id: 'call-1', type: 'function' },
+ ],
+ } as any;
+
+ it.each([['deepseek-v4-pro'], ['glm-5.3'], ['kimi-k3']])(
+ 'echoes structured reasoning back for %s',
+ async (model) => {
+ const result = await convertOpenAIMessages([assistantTurn], { model });
+
+ expect((result[0] as any).reasoning_content).toBe('step-by-step thinking');
+ },
+ );
+
+ it('leaves unrelated models untouched', async () => {
+ const result = await convertOpenAIMessages([assistantTurn], { model: 'gpt-5.6-sol' });
+
+ expect((result[0] as any).reasoning_content).toBeUndefined();
+ });
+
+ it('keeps the empty placeholder scoped to DeepSeek thinking models', async () => {
+ const withoutReasoning = { ...assistantTurn, reasoning: undefined };
+
+ const deepseek = await convertOpenAIMessages([withoutReasoning], {
+ model: 'deepseek-v4-pro',
+ });
+ const glm = await convertOpenAIMessages([withoutReasoning], { model: 'glm-5.3' });
+
+ expect((deepseek[0] as any).reasoning_content).toBe('');
+ expect((glm[0] as any).reasoning_content).toBeUndefined();
+ });
+ });
+
it('should preserve reasoning_content field from messages (for DeepSeek compatibility)', async () => {
const messages = [
{
diff --git a/packages/model-runtime/src/core/contextBuilders/openai.ts b/packages/model-runtime/src/core/contextBuilders/openai.ts
index b1790933de5..5d4162a669a 100644
--- a/packages/model-runtime/src/core/contextBuilders/openai.ts
+++ b/packages/model-runtime/src/core/contextBuilders/openai.ts
@@ -32,8 +32,22 @@ type ConvertMessageContentOptions = {
thoughtSignatureScope?: SignatureScope;
};
-const isDeepSeekModel = (model: string | undefined) =>
- typeof model === 'string' && model.toLowerCase().includes('deepseek');
+/**
+ * Model families whose thinking mode requires the historical `reasoning_content`
+ * to be echoed back when the assistant turn also carries `tool_calls`. Upstream
+ * rejects the request outright otherwise:
+ *
+ * > If thinking mode and tool_calls, `reasoning_content` must be passed back to the API.
+ *
+ * Matched on the model id rather than the provider because these models are
+ * mostly reached through OpenAI-compatible aggregators and user-configured
+ * proxies, where the provider key says nothing about the underlying family.
+ */
+const REASONING_PASSBACK_MODEL_KEYWORDS = ['deepseek', 'glm', 'kimi'] as const;
+
+const requiresReasoningPassback = (model: string | undefined) =>
+ typeof model === 'string' &&
+ REASONING_PASSBACK_MODEL_KEYWORDS.some((keyword) => model.toLowerCase().includes(keyword));
type OpenAICompatibleContentPart =
ExtendedChatCompletionContentPart | OpenAI.ChatCompletionContentPart | UserMessageContentPart;
@@ -253,15 +267,17 @@ export const convertOpenAIMessages = async (
// MiniMax uses reasoning_details for historical thinking, so forward it unchanged
if (msg.reasoning_details !== undefined) result.reasoning_details = msg.reasoning_details;
- // For DeepSeek-family models routed via any OpenAI-compatible runtime
- // (including custom user providers that bypass the dedicated DeepSeek
- // handlePayload), derive reasoning_content from the structured reasoning
- // field on assistant messages and force a placeholder when the model is
- // thinking-mode eligible.
- if (msg.role === 'assistant' && isDeepSeekModel(options?.model)) {
+ // For passback-requiring families routed via any OpenAI-compatible runtime
+ // (including custom user providers that bypass a dedicated handlePayload),
+ // derive reasoning_content from the structured reasoning field on assistant
+ // messages.
+ if (msg.role === 'assistant' && requiresReasoningPassback(options?.model)) {
if (result.reasoning_content === undefined && typeof msg.reasoning?.content === 'string') {
result.reasoning_content = msg.reasoning.content;
}
+ // The empty placeholder stays DeepSeek-scoped. Echoing reasoning we
+ // actually have is what upstream asks for; fabricating an empty thinking
+ // block for a family we have not validated is a different, riskier change.
if (
result.reasoning_content === undefined &&
isDeepSeekThinkingEligibleModel(options?.model)