Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-tool-choice-none.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/anthropic': patch
---

fix(anthropic): keep tools array when toolChoice is 'none'. Previously, toolChoice 'none' stripped both tools and tool_choice from the request, causing empty responses with tool_use history.
2 changes: 1 addition & 1 deletion packages/anthropic/src/anthropic-messages-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ export type AnthropicTool =
export type AnthropicSpeed = 'fast' | 'standard';

export type AnthropicToolChoice =
| { type: 'auto' | 'any'; disable_parallel_tool_use?: boolean }
| { type: 'auto' | 'any' | 'none'; disable_parallel_tool_use?: boolean }
| { type: 'tool'; name: string; disable_parallel_tool_use?: boolean };

export type AnthropicContainer = {
Expand Down
10 changes: 8 additions & 2 deletions packages/anthropic/src/anthropic-prepare-tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1020,8 +1020,14 @@ describe('prepareTools', () => {
toolChoice: { type: 'none' },
supportsStructuredOutput: true,
});
expect(result.tools).toBeUndefined();
expect(result.toolChoice).toBeUndefined();
expect(result.tools).toEqual([
{
name: 'testFunction',
description: 'Test',
input_schema: {},
},
]);
expect(result.toolChoice).toEqual({ type: 'none' });
});

it('should handle tool choice "tool"', async () => {
Expand Down
8 changes: 6 additions & 2 deletions packages/anthropic/src/anthropic-prepare-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,12 @@ export async function prepareTools({
betas,
};
case 'none':
// Anthropic does not support 'none' tool choice, so we remove the tools:
return { tools: undefined, toolChoice: undefined, toolWarnings, betas };
return {
tools: anthropicTools,
toolChoice: { type: 'none' },
toolWarnings,
betas,
};
case 'tool':
return {
tools: anthropicTools,
Expand Down