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): handle Claude Sonnet 5.5 always-on thinking (#20183)

YuTengjingc3315ed5cbbf
diff --git a/packages/model-runtime/src/core/anthropicCompatibleFactory/index.test.ts b/packages/model-runtime/src/core/anthropicCompatibleFactory/index.test.ts
index 3b4a3eecf54..dd1cee318ff 100644
--- a/packages/model-runtime/src/core/anthropicCompatibleFactory/index.test.ts
+++ b/packages/model-runtime/src/core/anthropicCompatibleFactory/index.test.ts
@@ -6,6 +6,7 @@ import { afterEach, describe, expect, it, vi } from 'vitest';
import type { ModelRuntimeDiagnostics } from '../../types/providerDiagnostics';
import { ContextExceededPreFlightError } from '../../utils/resolveSafeMaxTokens';
import {
+ buildDefaultAnthropicPayload,
createAnthropicCompatibleParams,
createAnthropicCompatibleRuntime,
createDefaultAnthropicClient,
@@ -727,6 +728,41 @@ describe('createAnthropicCompatibleRuntime', () => {
);
});
+ it('should omit disabled thinking when the mapped model always thinks', async () => {
+ // handlePayload resolves thinking by the logical id: Sonnet 5 accepts `disabled`, but a
+ // channel redirect to Sonnet 5.5 rejects it with a 400, so chat() must re-check the sent model.
+ const messagesCreate = vi.fn().mockResolvedValue({ content: [] });
+ const Runtime = createAnthropicCompatibleRuntime({
+ chatCompletion: {
+ handlePayload: buildDefaultAnthropicPayload,
+ },
+ customClient: {
+ createClient: () =>
+ ({
+ baseURL: 'https://aihubmix.com',
+ messages: { create: messagesCreate },
+ }) as unknown as Anthropic,
+ },
+ provider: 'test-provider',
+ });
+ const runtime = new Runtime({
+ apiKey: 'test-key',
+ modelIdMapping: { 'claude-sonnet-5': 'claude-sonnet-5-5' },
+ });
+
+ await runtime.chat({
+ messages: [{ content: 'hi', role: 'user' }],
+ model: 'claude-sonnet-5',
+ responseMode: 'json',
+ stream: false,
+ thinking: { type: 'disabled' },
+ } as any);
+
+ const request = messagesCreate.mock.calls[0][0];
+ expect(request.model).toBe('claude-sonnet-5-5');
+ expect(request).not.toHaveProperty('thinking');
+ });
+
it('should keep logical model for generateObject and pass mapped id as request config', async () => {
const generateObject = vi.fn().mockResolvedValue({ ok: true });
const Runtime = createAnthropicCompatibleRuntime({
diff --git a/packages/model-runtime/src/core/anthropicCompatibleFactory/index.ts b/packages/model-runtime/src/core/anthropicCompatibleFactory/index.ts
index 6d131aad10f..b8cffc992bf 100644
--- a/packages/model-runtime/src/core/anthropicCompatibleFactory/index.ts
+++ b/packages/model-runtime/src/core/anthropicCompatibleFactory/index.ts
@@ -7,7 +7,10 @@ import type { Pricing } from 'model-bank';
import { ErrorClassifier } from '../../errors';
import { stripUnsupportedClaudeAssistantPrefill } from '../../providers/anthropic/claudePrefill';
-import { rejectsDisabledThinkingAtEffort } from '../../providers/anthropic/modelId';
+import {
+ isAlwaysThinkingClaudeModel,
+ rejectsDisabledThinkingAtEffort,
+} from '../../providers/anthropic/modelId';
import type {
ChatCompletionErrorPayload,
ChatMethodOptions,
@@ -586,6 +589,17 @@ export const createAnthropicCompatibleRuntime = <T extends Record<string, any> =
);
}
+ // Same for disabled thinking: handlePayload resolved it by the logical id, but the
+ // mapped request model may be an always-thinking Claude that rejects `disabled` with a
+ // 400. Omitting the config is the documented fallback (see resolveClaudeThinkingConfig).
+ if (
+ requestPayload.model &&
+ (requestPayload as { thinking?: { type?: string } }).thinking?.type === 'disabled' &&
+ isAlwaysThinkingClaudeModel(requestPayload.model)
+ ) {
+ delete (requestPayload as { thinking?: unknown }).thinking;
+ }
+
const shouldDebugChatCompletion = debugParams?.chatCompletion?.() ?? false;
if (shouldDebugChatCompletion) {
debugPayload(requestPayload);
diff --git a/packages/model-runtime/src/core/anthropicCompatibleFactory/resolveThinkingConfig.test.ts b/packages/model-runtime/src/core/anthropicCompatibleFactory/resolveThinkingConfig.test.ts
index cd27133eea5..c00124097cc 100644
--- a/packages/model-runtime/src/core/anthropicCompatibleFactory/resolveThinkingConfig.test.ts
+++ b/packages/model-runtime/src/core/anthropicCompatibleFactory/resolveThinkingConfig.test.ts
@@ -27,6 +27,18 @@ describe('resolveClaudeThinkingConfig', () => {
// its default `omitted` display, which is what turning the switch off asks for.
expect(resolve('claude-fable-5', { type: 'disabled' })).toBeUndefined();
});
+
+ it('should omit disabled thinking when the mapped request model always thinks', () => {
+ // A channel may redirect Sonnet 5 (which accepts `disabled`) to Sonnet 5.5 upstream.
+ expect(
+ resolveClaudeThinkingConfig({
+ maxTokens: 4096,
+ model: 'claude-sonnet-5',
+ requestModel: 'global.anthropic.claude-sonnet-5-5',
+ thinking: { type: 'disabled' },
+ }),
+ ).toBeUndefined();
+ });
});
describe('models that default thinking off but omit the display', () => {
diff --git a/packages/model-runtime/src/core/anthropicCompatibleFactory/resolveThinkingConfig.ts b/packages/model-runtime/src/core/anthropicCompatibleFactory/resolveThinkingConfig.ts
index 92746608d10..55debd8d764 100644
--- a/packages/model-runtime/src/core/anthropicCompatibleFactory/resolveThinkingConfig.ts
+++ b/packages/model-runtime/src/core/anthropicCompatibleFactory/resolveThinkingConfig.ts
@@ -32,10 +32,16 @@ export interface ResolvedClaudeThinkingConfig {
export const resolveClaudeThinkingConfig = ({
maxTokens,
model,
+ requestModel,
thinking,
}: {
maxTokens: number;
model: string;
+ /**
+ * Provider model id actually sent upstream after channel `modelIdMapping`. A channel that
+ * redirects e.g. Sonnet 5 to Sonnet 5.5 sends an always-thinking model that rejects `disabled`.
+ */
+ requestModel?: string;
thinking?: ChatStreamPayload['thinking'];
}): ResolvedClaudeThinkingConfig | undefined => {
// An explicit `display` from the caller always wins; the model default only fills the gap.
@@ -59,10 +65,13 @@ export const resolveClaudeThinkingConfig = ({
}
case 'disabled': {
- // Fable 5 / Mythos 5 / Opus 5.5 reject `disabled` with a 400. Omitting the config is the
- // documented fallback: they think regardless, and their `omitted` display default already
- // keeps the reasoning text out of the response, which is what turning the switch off asks for.
- if (isAlwaysThinkingClaudeModel(model)) return undefined;
+ // Fable 5 / Mythos 5 / Opus 5.5 / Sonnet 5.5 reject `disabled` with a 400. Omitting the
+ // config is the documented fallback: they think regardless, and their `omitted` display
+ // default already keeps the reasoning text out of the response, which is what turning the
+ // switch off asks for.
+ if ([model, requestModel].some((id) => !!id && isAlwaysThinkingClaudeModel(id))) {
+ return undefined;
+ }
// `display` is invalid alongside `disabled` β€” there is nothing to display β€” so any caller
// value is dropped here rather than forwarded.
diff --git a/packages/model-runtime/src/providers/anthropic/modelId.test.ts b/packages/model-runtime/src/providers/anthropic/modelId.test.ts
index 022db943b8c..b10eca5f96c 100644
--- a/packages/model-runtime/src/providers/anthropic/modelId.test.ts
+++ b/packages/model-runtime/src/providers/anthropic/modelId.test.ts
@@ -160,11 +160,15 @@ describe('isAlwaysThinkingClaudeModel', () => {
expect(isAlwaysThinkingClaudeModel('global.anthropic.claude-fable-5')).toBe(true);
});
- it('should return true for Opus 5.5 and later', () => {
+ it('should return true for Opus 5.5 / Sonnet 5.5 and later', () => {
expect(isAlwaysThinkingClaudeModel('claude-opus-5-5')).toBe(true);
expect(isAlwaysThinkingClaudeModel('global.anthropic.claude-opus-5-5')).toBe(true);
expect(isAlwaysThinkingClaudeModel('anthropic/claude-opus-5.5')).toBe(true);
expect(isAlwaysThinkingClaudeModel('claude-opus-6')).toBe(true);
+ expect(isAlwaysThinkingClaudeModel('claude-sonnet-5-5')).toBe(true);
+ expect(isAlwaysThinkingClaudeModel('anthropic.claude-sonnet-5-5')).toBe(true);
+ expect(isAlwaysThinkingClaudeModel('anthropic/claude-sonnet-5.5')).toBe(true);
+ expect(isAlwaysThinkingClaudeModel('claude-sonnet-6')).toBe(true);
});
it('should return false for models that accept disabled thinking', () => {
@@ -222,11 +226,15 @@ describe('rejectsForcedToolChoice', () => {
expect(rejectsForcedToolChoice('anthropic/claude-fable-5-1')).toBe(true);
});
- it('should reject forced tool_choice on Opus 5.5 and later', () => {
+ it('should reject forced tool_choice on Opus 5.5 / Sonnet 5.5 and later', () => {
expect(rejectsForcedToolChoice('claude-opus-5-5')).toBe(true);
expect(rejectsForcedToolChoice('global.anthropic.claude-opus-5-5')).toBe(true);
expect(rejectsForcedToolChoice('anthropic/claude-opus-5.5')).toBe(true);
expect(rejectsForcedToolChoice('claude-opus-6')).toBe(true);
+ expect(rejectsForcedToolChoice('claude-sonnet-5-5')).toBe(true);
+ expect(rejectsForcedToolChoice('anthropic.claude-sonnet-5-5')).toBe(true);
+ expect(rejectsForcedToolChoice('anthropic/claude-sonnet-5.5')).toBe(true);
+ expect(rejectsForcedToolChoice('claude-sonnet-6')).toBe(true);
});
it('should keep forced tool_choice valid on Fable 5 / Mythos 5 and other families', () => {
diff --git a/packages/model-runtime/src/providers/anthropic/modelId.ts b/packages/model-runtime/src/providers/anthropic/modelId.ts
index 8c3299fc6c5..e3afa3f6444 100644
--- a/packages/model-runtime/src/providers/anthropic/modelId.ts
+++ b/packages/model-runtime/src/providers/anthropic/modelId.ts
@@ -141,12 +141,13 @@ export const isAdaptiveThinkingDefaultOnModel = (model: string): boolean => {
};
/**
- * Claude Opus 5.5 and later carry the Fable 5.1 contract: thinking is always on and forced
- * tool use is rejected. Opus 5 still accepts both.
+ * Claude Opus 5.5 / Sonnet 5.5 and later carry the Fable 5.1 contract: thinking is always on
+ * and forced tool use is rejected. Opus 5 / Sonnet 5 still accept both.
* @see https://platform.claude.com/docs/en/models/opus-5-5/whats-new-opus-5-5#breaking-changes
+ * @see https://claude.dev/blog/building-with-claude-sonnet-5-5/#migrating-from-sonnet-5
*/
-const isOpus55OrLater = (parsed: ParsedClaudeModelId): boolean =>
- parsed.family === 'opus' &&
+const isClaude55OrLater = (parsed: ParsedClaudeModelId): boolean =>
+ isClaudeFamily(parsed, ['opus', 'sonnet']) &&
(parsed.majorVersion > 5 || (parsed.majorVersion === 5 && hasMinorVersionAtLeast(parsed, 5)));
/**
@@ -161,7 +162,7 @@ export const isAlwaysThinkingClaudeModel = (model: string): boolean => {
// Claude Fable 5 and Claude Mythos 5 (and Claude Mythos Preview) always think.
if (isClaudeFamily(parsed, ['fable', 'mythos']) && parsed.majorVersion >= 5) return true;
- return isOpus55OrLater(parsed);
+ return isClaude55OrLater(parsed);
};
/**
@@ -229,16 +230,16 @@ export const supportsClaudeEffortLevel = (
};
/**
- * Claude Fable 5.1 / Mythos 5.1 / Opus 5.5 reject forced tool use. `tool_choice` of type
- * `any` or `tool` returns a 400; keep `auto` (or `none`) and use `strict: true` for schema
- * enforcement instead. Fable 5 / Mythos 5 / Opus 5 still accept forced choice.
+ * Claude Fable 5.1 / Mythos 5.1 / Opus 5.5 / Sonnet 5.5 reject forced tool use. `tool_choice` of
+ * type `any` or `tool` returns a 400; keep `auto` (or `none`) and use `strict: true` for schema
+ * enforcement instead. Fable 5 / Mythos 5 / Opus 5 / Sonnet 5 still accept forced choice.
* @see https://platform.claude.com/docs/en/models/fable-5-1/whats-new-fable-5-1#forced-tool-use-is-not-supported
* @see https://platform.claude.com/docs/en/models/opus-5-5/whats-new-opus-5-5#forced-tool-use-is-not-supported
*/
export const rejectsForcedToolChoice = (model: string): boolean => {
const parsed = parseClaudeModelId(model);
if (!parsed) return false;
- if (isOpus55OrLater(parsed)) return true;
+ if (isClaude55OrLater(parsed)) return true;
if (!isClaudeFamily(parsed, ['fable', 'mythos'])) return false;
if (parsed.majorVersion > 5) return true;
diff --git a/packages/model-runtime/src/providers/bedrock/index.test.ts b/packages/model-runtime/src/providers/bedrock/index.test.ts
index ca1fbdaa264..1e7012bcccb 100644
--- a/packages/model-runtime/src/providers/bedrock/index.test.ts
+++ b/packages/model-runtime/src/providers/bedrock/index.test.ts
@@ -312,6 +312,36 @@ describe('LobeBedrockAI', () => {
]);
});
+ it('should omit disabled thinking when the mapped Bedrock id always thinks', async () => {
+ // Sonnet 5 accepts `disabled`; a channel redirect to Sonnet 5.5 rejects it with a 400.
+ const mappedInstance = new LobeBedrockAI({
+ accessKeyId: 'test-access-key-id',
+ accessKeySecret: 'test-access-key-secret',
+ modelIdMapping: { 'claude-sonnet-5': 'global.anthropic.claude-sonnet-5-5' },
+ region: 'us-west-2',
+ });
+ const mockStream = new ReadableStream({
+ start(controller) {
+ controller.enqueue('Hello, world!');
+ controller.close();
+ },
+ });
+ vi.spyOn(mappedInstance['client'], 'send').mockResolvedValue(
+ Promise.resolve(mockStream) as any,
+ );
+
+ await mappedInstance.chat({
+ messages: [{ content: 'Hello', role: 'user' }],
+ model: 'claude-sonnet-5',
+ thinking: { type: 'disabled' },
+ } as any);
+
+ const commandInput = (InvokeModelWithResponseStreamCommand as unknown as Mock).mock
+ .calls[0][0];
+ expect(commandInput.modelId).toBe('global.anthropic.claude-sonnet-5-5');
+ expect(JSON.parse(commandInput.body)).not.toHaveProperty('thinking');
+ });
+
it('should drop assistant prefill when a logical id maps to a Claude 5 Bedrock id', async () => {
// The channel modelIdMapping resolves the actually-sent Bedrock model
// id; the prefill guard must follow it, not the logical id.
diff --git a/packages/model-runtime/src/providers/bedrock/index.ts b/packages/model-runtime/src/providers/bedrock/index.ts
index da6ebd1ac9c..dd3328c7bf4 100644
--- a/packages/model-runtime/src/providers/bedrock/index.ts
+++ b/packages/model-runtime/src/providers/bedrock/index.ts
@@ -338,6 +338,7 @@ export class LobeBedrockAI implements LobeRuntimeAI {
const resolvedThinking = resolveClaudeThinkingConfig({
maxTokens: resolvedMaxTokens,
model,
+ requestModel: this.resolveModelId(model),
thinking,
});