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(agent-runtime): keep DeepSeek V4 thinking answers out of the reasoning channel (#16294)

Arvin Xu0379cdf70eab
diff --git a/apps/server/src/modules/AgentRuntime/RuntimeExecutors.ts b/apps/server/src/modules/AgentRuntime/RuntimeExecutors.ts
index f94003eadc7..0f43d335386 100644
--- a/apps/server/src/modules/AgentRuntime/RuntimeExecutors.ts
+++ b/apps/server/src/modules/AgentRuntime/RuntimeExecutors.ts
@@ -42,6 +42,8 @@ import {
applyModelExtendParams,
type ChatStreamPayload,
consumeStreamUntilDone,
+ isDeepSeekThinkingEligibleModel,
+ isDeepSeekV4FamilyModel,
isKimiAlwaysPreserveThinkingModel,
type ModelExtendParams,
} from '@lobechat/model-runtime';
@@ -941,9 +943,31 @@ export const createRuntimeExecutors = (
const modelSupportsPreserveThinkingFromCard =
Array.isArray(modelExtendParams) && modelExtendParams.includes('preserveThinking');
// Kimi K2.7+ Code has preserved thinking always active and cannot opt out.
- const modelForcesPreserveThinking =
+ const kimiForcesPreserveThinking =
(provider === 'moonshot' || provider === BRANDING_PROVIDER) &&
isKimiAlwaysPreserveThinkingModel(model);
+ // DeepSeek V4 / reasoner thinking models MUST replay the real assistant
+ // reasoning in history β€” this is mandatory, not opt-in. Their
+ // Anthropic-compatible API rejects an assistant tool-call turn whose
+ // thinking block is missing (HTTP 400), so stripping reasoning leaves the
+ // payload builder no choice but to emit a whitespace-only placeholder
+ // thinking block. Under large agentic context that degenerate history makes
+ // the model emit its final answer *inside* the thinking block with empty
+ // visible text (controlled replay: ~30% answer-in-thinking with the
+ // placeholder vs ~2.5% when the genuine reasoning is replayed). The only
+ // opt-out is a V4 model whose thinking the user explicitly disabled via
+ // `deepseekV4ReasoningEffort: 'none'`. That flag is V4-specific and may
+ // linger on an agent after switching models, so it must NOT suppress
+ // replay for `deepseek-reasoner`, which is thinking-only and always
+ // forces reasoning history in the payload builder β€” suppressing it there
+ // would reintroduce the 400/answer-hidden behavior.
+ const deepseekV4ThinkingDisabled =
+ isDeepSeekV4FamilyModel(model) &&
+ agentConfig.chatConfig?.deepseekV4ReasoningEffort === 'none';
+ const deepseekForcesPreserveThinking =
+ isDeepSeekThinkingEligibleModel(model) && !deepseekV4ThinkingDisabled;
+ const modelForcesPreserveThinking =
+ kimiForcesPreserveThinking || deepseekForcesPreserveThinking;
const providerSupportsPreserveThinkingFallback =
provider === 'qwen' || provider === 'zhipu' || provider === 'moonshot';
const modelSupportsPreserveThinking =
@@ -1571,6 +1595,10 @@ export const createRuntimeExecutors = (
const reasoningImageUploads: Promise<void>[] = [];
let hasContentImages = false;
let hasReasoningImages = false;
+ // Set when a terminal turn's answer was salvaged from the reasoning
+ // channel (see the answer-in-thinking guard below) β€” surfaced in
+ // message metadata for observability.
+ let answerSalvagedFromReasoning = false;
textBuffer = '';
reasoningBuffer = '';
@@ -1841,6 +1869,36 @@ export const createRuntimeExecutors = (
throw new ModelEmptyError();
}
+ // Answer-in-thinking salvage: some thinking-mode models β€” notably
+ // DeepSeek V4 over the Anthropic-compatible API β€” occasionally emit
+ // the final user-facing answer inside the reasoning channel and stop
+ // naturally with an empty text block. The reasoning is then rendered
+ // as a collapsed "thinking" panel, so the user sees a blank reply.
+ // When a turn ends naturally with no tool calls and no visible
+ // content but non-empty text reasoning, promote the reasoning to be
+ // the answer. This is a backstop; the primary fix is replaying the
+ // real assistant reasoning in history (see modelForcesPreserveThinking
+ // above) which sharply reduces how often the model does this.
+ const isTerminalNaturalStop =
+ currentStepFinishReason === 'end_turn' || currentStepFinishReason === 'stop';
+ if (
+ isTerminalNaturalStop &&
+ toolsCalling.length === 0 &&
+ tool_calls.length === 0 &&
+ content.trim().length === 0 &&
+ thinkingContent.trim().length > 0 &&
+ !hasReasoningImages
+ ) {
+ log(
+ '[%s] answer-in-thinking salvage: promoting %d chars of reasoning to content',
+ operationLogId,
+ thinkingContent.length,
+ );
+ content = thinkingContent;
+ thinkingContent = '';
+ answerSalvagedFromReasoning = true;
+ }
+
log(
`[${operationLogId}] finish model-runtime calling | content: %d chars | reasoning: %d chars | tools: %d | usage: %s`,
content.length,
@@ -1935,6 +1993,9 @@ export const createRuntimeExecutors = (
if (hasContentImages) {
metadata.isMultimodal = true;
}
+ if (answerSalvagedFromReasoning) {
+ metadata.answerSalvagedFromReasoning = true;
+ }
// Sanitize tool_call `arguments` before persisting to DB so malformed
// JSON (e.g. Qwen emitting `{, ...}`) can't poison future context
diff --git a/apps/server/src/modules/AgentRuntime/__tests__/RuntimeExecutors.test.ts b/apps/server/src/modules/AgentRuntime/__tests__/RuntimeExecutors.test.ts
index a1e83f9518a..a77c9ce1cc6 100644
--- a/apps/server/src/modules/AgentRuntime/__tests__/RuntimeExecutors.test.ts
+++ b/apps/server/src/modules/AgentRuntime/__tests__/RuntimeExecutors.test.ts
@@ -74,6 +74,12 @@ vi.mock('@lobechat/model-runtime', () => ({
// retry classifier path.
ERROR_CODE_SPECS: {},
getErrorCodeSpec: () => undefined,
+ isDeepSeekThinkingEligibleModel: (model: string) =>
+ typeof model === 'string' &&
+ (model.toLowerCase().includes('deepseek-reasoner') ||
+ model.toLowerCase().includes('deepseek-v4')),
+ isDeepSeekV4FamilyModel: (model: string) =>
+ typeof model === 'string' && model.toLowerCase().includes('deepseek-v4'),
isKimiAlwaysPreserveThinkingModel: (model: string) =>
/^kimi-k2\.(?:[7-9]|\d{2,})-code(?:$|-)/.test(model),
refineErrorCode: () => undefined,
diff --git a/packages/model-runtime/src/index.ts b/packages/model-runtime/src/index.ts
index 6ab1ba1fc61..f536478b365 100644
--- a/packages/model-runtime/src/index.ts
+++ b/packages/model-runtime/src/index.ts
@@ -87,4 +87,5 @@ export {
type ModelExtendParams,
resolveDefaultThinkingLevelForModel,
} from './utils/modelExtendParams';
+export { isDeepSeekThinkingEligibleModel, isDeepSeekV4FamilyModel } from './utils/modelParse';
export { parseDataUri } from './utils/uriParser';