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): ask Fable 5.1 to call the tool when tool_choice falls back to auto (#19024)

YuTengjingf279402dbcd0
diff --git a/packages/model-runtime/src/core/anthropicCompatibleFactory/generateObject.test.ts b/packages/model-runtime/src/core/anthropicCompatibleFactory/generateObject.test.ts
index 139dd480b4c..3f8a3589dc8 100644
--- a/packages/model-runtime/src/core/anthropicCompatibleFactory/generateObject.test.ts
+++ b/packages/model-runtime/src/core/anthropicCompatibleFactory/generateObject.test.ts
@@ -84,6 +84,34 @@ describe('Anthropic generateObject', () => {
strict: true,
}),
]);
+ // auto tool_choice cannot force the call, so the prompt must ask for it
+ expect(requestParams.system).toEqual([
+ {
+ text: 'You must respond by calling the `person_extractor` tool. Do not reply with plain text.',
+ type: 'text',
+ },
+ ]);
+ });
+
+ it('should append the tool-use instruction after an existing system prompt on Fable 5.1', async () => {
+ const { requestParams } = await buildAnthropicGenerateObjectRequest({
+ messages: [
+ { content: 'You are an extractor.', role: 'system' as const },
+ { content: 'Generate a person object', role: 'user' as const },
+ ],
+ model: 'claude-fable-5-1',
+ schema: {
+ name: 'person_extractor',
+ schema: { properties: { name: { type: 'string' } }, type: 'object' as const },
+ },
+ } as any);
+
+ expect(requestParams.system).toEqual([
+ {
+ text: 'You are an extractor.\n\nYou must respond by calling the `person_extractor` tool. Do not reply with plain text.',
+ type: 'text',
+ },
+ ]);
});
it('should key the Fable 5.1 tool_choice guard on config.requestModel', async () => {
@@ -116,6 +144,8 @@ describe('Anthropic generateObject', () => {
name: 'person_extractor',
type: 'tool',
});
+ // forced tool_choice still works here, so no prompt instruction is injected
+ expect(requestParams.system).toBeUndefined();
});
it('should use auto tool_choice for tools mode on Fable 5.1', async () => {
@@ -139,6 +169,12 @@ describe('Anthropic generateObject', () => {
} as any);
expect(requestParams.tool_choice).toEqual({ type: 'auto' });
+ expect(requestParams.system).toEqual([
+ {
+ text: 'You must respond by calling one of the provided tools. Do not reply with plain text.',
+ type: 'text',
+ },
+ ]);
});
it('should still use auto tool_choice when the request model is an opaque mapped id', async () => {
diff --git a/packages/model-runtime/src/core/anthropicCompatibleFactory/generateObject.ts b/packages/model-runtime/src/core/anthropicCompatibleFactory/generateObject.ts
index 17f7b904847..4c70c4f7841 100644
--- a/packages/model-runtime/src/core/anthropicCompatibleFactory/generateObject.ts
+++ b/packages/model-runtime/src/core/anthropicCompatibleFactory/generateObject.ts
@@ -59,15 +59,6 @@ export const buildAnthropicGenerateObjectRequest = async (
log('converted %d messages to Anthropic format', anthropicMessages.length);
- const systemPrompts = systemPromptText
- ? [
- {
- text: systemPromptText,
- type: 'text' as const,
- },
- ]
- : undefined;
-
let finalTools;
let tool_choice: Anthropic.ToolChoiceAuto | Anthropic.ToolChoiceAny | Anthropic.ToolChoiceTool;
let schemaToolName: string | undefined;
@@ -77,9 +68,19 @@ export const buildAnthropicGenerateObjectRequest = async (
const forcedToolChoiceRejected = [model, config?.requestModel].some(
(id): id is string => !!id && rejectsForcedToolChoice(id),
);
+ // With `tool_choice: auto` nothing forces the model to call a tool, so the
+ // official migration guide asks callers to state the requirement in the prompt.
+ // Without it the model may answer in plain text and the parser silently returns
+ // `undefined` / `[]` to callers that expect structured output.
+ // https://platform.claude.com/docs/en/models/fable-5-1/whats-new-fable-5-1#forced-tool-use-is-not-supported
+ let toolUseInstruction: string | undefined;
if (tools) {
finalTools = buildAnthropicTools(tools);
tool_choice = forcedToolChoiceRejected ? { type: 'auto' } : { type: 'any' };
+ if (forcedToolChoiceRejected) {
+ toolUseInstruction =
+ 'You must respond by calling one of the provided tools. Do not reply with plain text.';
+ }
} else if (schema) {
// Convert OpenAI-style schema to Anthropic tool format
const tool: Anthropic.ToolUnion = {
@@ -106,10 +107,23 @@ export const buildAnthropicGenerateObjectRequest = async (
: config?.schemaToolChoice === 'any'
? { type: 'any' }
: { name: tool.name, type: 'tool' };
+ if (forcedToolChoiceRejected) {
+ toolUseInstruction = `You must respond by calling the \`${tool.name}\` tool. Do not reply with plain text.`;
+ }
} else {
throw new Error('tools or schema is required');
}
+ const systemPrompts =
+ systemPromptText || toolUseInstruction
+ ? [
+ {
+ text: [systemPromptText, toolUseInstruction].filter(Boolean).join('\n\n'),
+ type: 'text' as const,
+ },
+ ]
+ : undefined;
+
return {
requestParams: {
max_tokens: config?.maxTokens ?? 64_000,