🤯 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(model-runtime): disable DeepSeek V4 thinking for Anthropic structured output (#22369)
YuTengjingc8aadeacfb73
diff --git a/packages/model-runtime/src/providers/deepseek/__tests__/generateObject.test.ts b/packages/model-runtime/src/providers/deepseek/__tests__/generateObject.test.tsindex ef26b6b05c1..cee35c3afee 100644--- a/packages/model-runtime/src/providers/deepseek/__tests__/generateObject.test.ts+++ b/packages/model-runtime/src/providers/deepseek/__tests__/generateObject.test.ts@@ -2,6 +2,7 @@import type { Mock } from 'vitest';import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import { createDeepSeekAnthropicGenerateObject } from '../generateObject';import { LobeDeepSeekAnthropicAI, openAIParams } from '../index';import { expectNoLoneSurrogateEscapes, loneHighSurrogate, validEmoji } from './testUtils';@@ -50,16 +51,16 @@ describe('LobeDeepSeekAnthropicAI generateObject', () => {vi.clearAllMocks();});- it('should use any tool choice by default for server-side thinking', async () => {- // DeepSeek's Anthropic-compatible endpoint rejects named tool_choice while- // thinking is active, but accepts `any`; V4 models can default to thinking- // enabled server-side.+ it('should disable thinking and force the schema tool by default for V4 models', async () => {+ // Regression: since 2026-11-02 DeepSeek's Anthropic-compatible endpoint+ // rejects `tool_choice: any` while V4's server-side default thinking is+ // active ("Thinking mode does not support this tool_choice").const result = await instance.generateObject(generateObjectPayload);const payload = getLastRequestPayload();- expect(payload.thinking).toBeUndefined();- expect(payload.tool_choice).toEqual({ type: 'any' });+ expect(payload.thinking).toEqual({ type: 'disabled' });+ expect(payload.tool_choice).toEqual({ name: 'task_topic_handoff', type: 'tool' });expect(payload.tools).toEqual([expect.objectContaining({input_schema: expect.objectContaining({@@ -73,6 +74,26 @@ describe('LobeDeepSeekAnthropicAI generateObject', () => {expect(result).toEqual({ summary: 'Task completed', title: 'Done' });});+ it('should disable thinking when only the mapped request model is V4', async () => {+ const create = vi.fn().mockResolvedValue({+ content: [{ input: { title: 'Done' }, name: 'task_topic_handoff', type: 'tool_use' }],+ });++ await createDeepSeekAnthropicGenerateObject(+ { messages: { create } } as any,+ { ...generateObjectPayload, model: 'custom-flash' },+ undefined,+ undefined,+ { requestModel: 'deepseek-v4-flash' },+ );++ const payload = create.mock.calls[0][0];++ expect(payload.model).toBe('deepseek-v4-flash');+ expect(payload.thinking).toEqual({ type: 'disabled' });+ expect(payload.tool_choice).toEqual({ name: 'task_topic_handoff', type: 'tool' });+ });+it('should keep named tool choice when thinking is disabled', async () => {await instance.generateObject({...generateObjectPayload,@@ -85,7 +106,7 @@ describe('LobeDeepSeekAnthropicAI generateObject', () => {expect(payload.tool_choice).toEqual({ name: 'task_topic_handoff', type: 'tool' });});- it('should use any tool choice when thinking is explicitly enabled', async () => {+ it('should use auto tool choice with a prompt instruction when thinking is explicitly enabled', async () => {await instance.generateObject({...generateObjectPayload,thinking: { budget_tokens: 1024, type: 'enabled' },@@ -94,10 +115,11 @@ describe('LobeDeepSeekAnthropicAI generateObject', () => {const payload = getLastRequestPayload();expect(payload.thinking).toBeUndefined();- expect(payload.tool_choice).toEqual({ type: 'any' });+ expect(payload.tool_choice).toEqual({ type: 'auto' });+ expect(payload.system?.[0]?.text).toContain('`task_topic_handoff` tool');});- it('should use any tool choice for thinking-only deepseek-reasoner', async () => {+ it('should use auto tool choice for thinking-only deepseek-reasoner', async () => {await instance.generateObject({...generateObjectPayload,model: 'deepseek-reasoner',@@ -106,7 +128,51 @@ describe('LobeDeepSeekAnthropicAI generateObject', () => {const payload = getLastRequestPayload();expect(payload.thinking).toBeUndefined();- expect(payload.tool_choice).toEqual({ type: 'any' });+ expect(payload.tool_choice).toEqual({ type: 'auto' });+ });++ it('should force the schema tool without a thinking parameter for non-thinking deepseek-chat', async () => {+ await instance.generateObject({+ ...generateObjectPayload,+ model: 'deepseek-chat',+ });++ const payload = getLastRequestPayload();++ expect(payload.thinking).toBeUndefined();+ expect(payload.tool_choice).toEqual({ name: 'task_topic_handoff', type: 'tool' });+ });++ it('should use auto tool choice with a prompt instruction for V4 tools-mode requests', async () => {+ const tools = [+ {+ function: {+ description: 'Pick a title',+ name: 'pick_title',+ parameters: { properties: { title: { type: 'string' } }, type: 'object' as const },+ },+ type: 'function' as const,+ },+ ];++ await instance.generateObject({ ...generateObjectPayload, schema: undefined, tools } as any);+ let payload = getLastRequestPayload();++ // DeepSeek treats `any` as `auto`, so tools mode relies on the instruction.+ expect(payload.thinking).toEqual({ type: 'disabled' });+ expect(payload.tool_choice).toEqual({ type: 'auto' });+ expect(payload.system?.[0]?.text).toContain('calling one of the provided tools');++ await instance.generateObject({+ ...generateObjectPayload,+ schema: undefined,+ thinking: { budget_tokens: 1024, type: 'enabled' },+ tools,+ } as any);+ payload = getLastRequestPayload();++ expect(payload.thinking).toBeUndefined();+ expect(payload.tool_choice).toEqual({ type: 'auto' });});it('should map reasoning_effort to output_config.effort when thinking is enabled', async () => {@@ -119,7 +185,7 @@ describe('LobeDeepSeekAnthropicAI generateObject', () => {const payload = getLastRequestPayload();expect(payload.output_config).toEqual({ effort: 'high' });- expect(payload.tool_choice).toEqual({ type: 'any' });+ expect(payload.tool_choice).toEqual({ type: 'auto' });});it('should remove lone surrogates from Anthropic generateObject request payload strings', async () => {diff --git a/packages/model-runtime/src/providers/deepseek/generateObject.ts b/packages/model-runtime/src/providers/deepseek/generateObject.tsindex 4ffc7f49c66..39458853ff2 100644--- a/packages/model-runtime/src/providers/deepseek/generateObject.ts+++ b/packages/model-runtime/src/providers/deepseek/generateObject.ts@@ -12,9 +12,8 @@ type GenerateObjectHandlePayload = NonNullable<NonNullable<OpenAICompatibleFactoryOptions['generateObject']>['handlePayload']>;-const isGenerateObjectThinkingDisabled = (payload: GenerateObjectPayload) =>- (payload as GenerateObjectPayload & { thinking?: ChatStreamPayload['thinking'] }).thinking- ?.type === 'disabled';+const getGenerateObjectThinkingType = (payload: GenerateObjectPayload) =>+ (payload as GenerateObjectPayload & { thinking?: ChatStreamPayload['thinking'] }).thinking?.type;export const createDeepSeekAnthropicGenerateObject = async (client: Anthropic,@@ -23,12 +22,30 @@ export const createDeepSeekAnthropicGenerateObject = async (pricing?: Pricing,config?: AnthropicGenerateObjectConfig,) => {- // DeepSeek's Anthropic-compatible endpoint rejects named schema tool_choice- // while thinking is active, but accepts `{ type: "any" }`. V4 models may- // default to thinking enabled server-side, so keep `any` unless the caller- // explicitly disabled thinking; with a single schema tool it still forces- // structured output.- const thinkingDisabled = isGenerateObjectThinkingDisabled(payload);+ // DeepSeek's Anthropic-compatible endpoint only accepts `auto` / `none` (or+ // no) tool_choice while thinking is active; `any` and a named tool both 400+ // with "Thinking mode does not support this tool_choice". `any` used to be+ // accepted until 2026-11-02 07:17 UTC, when it started failing every+ // structured-output request on V4 models, which default to thinking enabled+ // server-side. So:+ // - V4: disable thinking explicitly unless the caller enabled it, and force+ // the schema tool (mirrors `buildDeepSeekGenerateObjectPayload`).+ // - Thinking active (caller-enabled V4, thinking-only deepseek-reasoner):+ // use `auto` plus a prompt instruction instead of a forced tool_choice.+ // - Tools mode (several caller tools, e.g. the group-chat supervisor): the+ // endpoint now treats `any` as `auto` even with thinking disabled, so it no+ // longer forces a call. Send `auto` plus the "call one of the tools"+ // instruction regardless of thinking.+ // https://api-docs.deepseek.com/guides/anthropic_api#tool_choice+ // Check the mapped request model too: a router can map a logical id to the+ // upstream V4 id.+ const modelIds = [payload.model, config?.requestModel];+ const isV4 = modelIds.some((id) => isDeepSeekV4Model(id));+ const isThinkingOnly = modelIds.some((id) => !!id && id.includes('deepseek-reasoner'));+ const thinkingType = getGenerateObjectThinkingType(payload);+ const thinkingDisabled = isV4 ? thinkingType !== 'enabled' : thinkingType === 'disabled';+ const thinkingActive = !thinkingDisabled && (isV4 || isThinkingOnly);+const requestParams: AnthropicGenerateObjectConfig['requestParams'] = {...(!thinkingDisabled && payload.reasoning_effort? {@@ -53,8 +70,9 @@ export const createDeepSeekAnthropicGenerateObject = async (return createAnthropicGenerateObject(sanitizedClient, payload, options, pricing, {requestModel: config?.requestModel,+ autoToolChoice: thinkingActive || !!payload.tools,requestParams,- schemaToolChoice: thinkingDisabled ? 'tool' : 'any',+ schemaToolChoice: 'tool',});};