Skip to content

Commit 4cbf406

Browse files
Merge pull request #403 from ymonster/fix/deepseek-empty-reasoning-content
fix: 保留 DeepSeek v4 thinking mode 的空 reasoning_content (#399)
2 parents f72b867 + 1b10ea3 commit 4cbf406

4 files changed

Lines changed: 91 additions & 15 deletions

File tree

packages/@ant/model-provider/src/shared/__tests__/openaiConvertMessages.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,11 @@ describe('DeepSeek thinking mode (enableThinking)', () => {
468468
expect(assistant.reasoning_content).toBe('First thought.\nSecond thought.')
469469
})
470470

471-
test('skips empty thinking blocks', () => {
471+
test('preserves empty thinking blocks as reasoning_content: "" (DeepSeek v4 thinking mode)', () => {
472+
// DeepSeek v4 thinking mode sometimes returns reasoning_content: ""
473+
// when the model answers directly without reasoning. The empty value
474+
// must be echoed back in the next request — otherwise DeepSeek returns
475+
// 400 ("reasoning_content ... must be passed back"). See issue #399.
472476
const result = anthropicMessagesToOpenAI(
473477
[
474478
makeUserMsg('question'),
@@ -481,7 +485,23 @@ describe('DeepSeek thinking mode (enableThinking)', () => {
481485
{ enableThinking: true },
482486
)
483487
const assistant = result.filter(m => m.role === 'assistant')[0] as any
488+
expect(assistant.reasoning_content).toBe('')
489+
expect(assistant.content).toBe('Answer.')
490+
})
491+
492+
test('omits reasoning_content when no thinking block is present', () => {
493+
// No thinking block at all → no reasoning_content field on the
494+
// OpenAI-format assistant message (relevant for non-thinking models).
495+
const result = anthropicMessagesToOpenAI(
496+
[
497+
makeUserMsg('question'),
498+
makeAssistantMsg([{ type: 'text', text: 'Answer.' }]),
499+
],
500+
[] as any,
501+
)
502+
const assistant = result.filter(m => m.role === 'assistant')[0] as any
484503
expect(assistant.reasoning_content).toBeUndefined()
504+
expect(assistant.content).toBe('Answer.')
485505
})
486506

487507
// ── fix: reorder tool and user messages for OpenAI API compatibility (#168) ──

packages/@ant/model-provider/src/shared/__tests__/openaiStreamAdapter.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,54 @@ describe('thinking support (reasoning_content)', () => {
439439
expect(blockStarts[1].content_block.type).toBe('tool_use')
440440
})
441441

442+
test('opens thinking block on empty reasoning_content (DeepSeek v4 direct-answer)', async () => {
443+
// DeepSeek v4 thinking mode sometimes streams reasoning_content: ""
444+
// before answering directly. We must still open a thinking block so the
445+
// resulting assistant message carries an (empty) thinking block — that
446+
// round-trips back as reasoning_content: "" in the next request,
447+
// satisfying DeepSeek's requirement (see issue #399).
448+
const events = await collectEvents([
449+
makeChunk({
450+
choices: [
451+
{
452+
index: 0,
453+
delta: { reasoning_content: '' },
454+
finish_reason: null,
455+
},
456+
],
457+
}),
458+
makeChunk({
459+
choices: [
460+
{
461+
index: 0,
462+
delta: { content: 'Direct answer.' },
463+
finish_reason: null,
464+
},
465+
],
466+
}),
467+
makeChunk({
468+
choices: [{ index: 0, delta: {}, finish_reason: 'stop' }],
469+
}),
470+
])
471+
472+
// A thinking block was opened (and closed before the text block starts)
473+
const blockStarts = events.filter(
474+
e => e.type === 'content_block_start',
475+
) as any[]
476+
expect(blockStarts.length).toBe(2)
477+
expect(blockStarts[0].content_block.type).toBe('thinking')
478+
expect(blockStarts[0].content_block.thinking).toBe('')
479+
expect(blockStarts[1].content_block.type).toBe('text')
480+
481+
// No empty thinking_delta should be emitted — the empty string is
482+
// already conveyed by the thinking block's initial value.
483+
const thinkingDeltas = events.filter(
484+
e =>
485+
e.type === 'content_block_delta' && e.delta.type === 'thinking_delta',
486+
)
487+
expect(thinkingDeltas.length).toBe(0)
488+
})
489+
442490
test('thinking block index is 0, text block index is 1', async () => {
443491
const events = await collectEvents([
444492
makeChunk({

packages/@ant/model-provider/src/shared/openaiConvertMessages.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,14 @@ function convertInternalAssistantMessage(
206206
},
207207
})
208208
} else if (block.type === 'thinking') {
209-
// DeepSeek thinking mode: always preserve reasoning_content.
210-
// DeepSeek requires reasoning_content to be passed back in subsequent requests,
211-
// especially when tool calls are involved (returns 400 if missing).
209+
// DeepSeek thinking mode: always preserve reasoning_content,
210+
// including the empty-string case. DeepSeek v4 may return
211+
// reasoning_content: "" when the model answers directly, and the
212+
// empty value must be echoed back in the next request — otherwise
213+
// DeepSeek returns 400 ("reasoning_content ... must be passed back").
212214
const thinkingText = (block as unknown as Record<string, unknown>)
213215
.thinking
214-
if (typeof thinkingText === 'string' && thinkingText) {
216+
if (typeof thinkingText === 'string') {
215217
reasoningParts.push(thinkingText)
216218
}
217219
}

packages/@ant/model-provider/src/shared/openaiStreamAdapter.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,13 @@ export async function* adaptOpenAIStreamToAnthropic(
106106
// Skip chunks that carry only usage data (no delta content)
107107
if (!delta) continue
108108

109-
// Handle reasoning_content → Anthropic thinking block
109+
// Handle reasoning_content → Anthropic thinking block.
110+
// Empty string is a valid signal: DeepSeek v4 thinking mode sometimes
111+
// returns reasoning_content: "" when the model answers directly. The
112+
// empty thinking block must round-trip back to the API in subsequent
113+
// requests, otherwise DeepSeek rejects with 400.
110114
const reasoningContent = (delta as any).reasoning_content
111-
if (reasoningContent != null && reasoningContent !== '') {
115+
if (reasoningContent != null) {
112116
if (!thinkingBlockOpen) {
113117
currentContentIndex++
114118
thinkingBlockOpen = true
@@ -125,14 +129,16 @@ export async function* adaptOpenAIStreamToAnthropic(
125129
} as BetaRawMessageStreamEvent
126130
}
127131

128-
yield {
129-
type: 'content_block_delta',
130-
index: currentContentIndex,
131-
delta: {
132-
type: 'thinking_delta',
133-
thinking: reasoningContent,
134-
},
135-
} as BetaRawMessageStreamEvent
132+
if (reasoningContent !== '') {
133+
yield {
134+
type: 'content_block_delta',
135+
index: currentContentIndex,
136+
delta: {
137+
type: 'thinking_delta',
138+
thinking: reasoningContent,
139+
},
140+
} as BetaRawMessageStreamEvent
141+
}
136142
}
137143

138144
// Handle text content

0 commit comments

Comments
 (0)