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/spread-anthropic-provider-options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/anthropic': patch
---

Spread message-level `providerOptions.anthropic` (excluding cache control keys) onto assistant messages, enabling custom fields like `reasoning_content` to pass through to the HTTP body.
Original file line number Diff line number Diff line change
Expand Up @@ -1961,6 +1961,57 @@ describe('assistant messages', () => {
`);
});
});

it('should spread message-level providerOptions.anthropic onto assistant message', async () => {
const result = await convertToAnthropicMessagesPrompt({
prompt: [
{
role: 'assistant',
content: [{ type: 'text', text: 'Hello' }],
providerOptions: {
anthropic: {
reasoning_content: 'I thought about it...',
},
},
},
],
sendReasoning: true,
warnings: [],
toolNameMapping: defaultToolNameMapping,
});

const msg = result.prompt.messages[0] as any;
expect(msg.reasoning_content).toBe('I thought about it...');
expect(msg.content).toEqual([
{ type: 'text', text: 'Hello', cache_control: undefined },
]);
});

it('should exclude cacheControl and cache_control from spread', async () => {
const result = await convertToAnthropicMessagesPrompt({
prompt: [
{
role: 'assistant',
content: [{ type: 'text', text: 'Hello' }],
providerOptions: {
anthropic: {
cacheControl: { type: 'ephemeral' },
cache_control: { type: 'ephemeral' },
reasoning_content: 'thinking...',
},
},
},
],
sendReasoning: true,
warnings: [],
toolNameMapping: defaultToolNameMapping,
});

const msg = result.prompt.messages[0] as any;
expect(msg.reasoning_content).toBe('thinking...');
expect(msg.cacheControl).toBeUndefined();
expect(msg.cache_control).toBeUndefined();
});
});

describe('cache control', () => {
Expand Down
20 changes: 19 additions & 1 deletion packages/anthropic/src/convert-to-anthropic-messages-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,25 @@ export async function convertToAnthropicMessagesPrompt({
}
}

messages.push({ role: 'assistant', content: anthropicContent });
// Collect message-level providerOptions.anthropic (excluding cache
// control keys) and spread onto the message, mirroring how the
// openai-compatible provider handles providerOptions.openaiCompatible.
const extra: Record<string, unknown> = {};
for (const message of block.messages) {
const opts = message.providerOptions?.anthropic;
if (opts != null && typeof opts === 'object') {
for (const [k, v] of Object.entries(opts)) {
if (k === 'cacheControl' || k === 'cache_control') continue;
extra[k] = v;
}
}
}

messages.push({
role: 'assistant',
content: anthropicContent,
...extra,
} as AnthropicAssistantMessage);

break;
}
Expand Down