Skip to content

Commit b00bde2

Browse files
vercel-ai-sdk[bot]dhofheinznicoalbaneselgrammel
authored
Backport: docs(anthropic): add structured outputs and tool input streaming section (#9604)
This is an automated backport of #9229 to the release-v5.0 branch. Co-authored-by: Daniel Hofheinz <daniel@danielhofheinz.com> Co-authored-by: Nico Albanese <49612682+nicoalbanese@users.noreply.github.com> Co-authored-by: Lars Grammel <lars.grammel@gmail.com>
1 parent 716c41c commit b00bde2

File tree

1 file changed

+63
-5
lines changed

1 file changed

+63
-5
lines changed

content/providers/01-ai-sdk-providers/05-anthropic.mdx

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,69 @@ const { text } = await generateText({
9393
Anthropic language models can also be used in the `streamText`, `generateObject`, and `streamObject` functions
9494
(see [AI SDK Core](/docs/ai-sdk-core)).
9595

96-
<Note>
97-
The Anthropic API returns streaming tool calls all at once after a delay. This
98-
causes the `streamObject` function to generate the object fully after a delay
99-
instead of streaming it incrementally.
100-
</Note>
96+
### Structured Outputs and Tool Input Streaming
97+
98+
By default, the Anthropic API returns streaming tool calls and structured outputs all at once after a delay. To enable incremental streaming of tool inputs (when using `streamText` with tools) and structured outputs (when using `streamObject`), you need to set the `anthropic-beta` header to `fine-grained-tool-streaming-2025-05-14`.
99+
100+
#### For structured outputs with `streamObject`
101+
102+
```ts
103+
import { anthropic } from '@ai-sdk/anthropic';
104+
import { streamObject } from 'ai';
105+
import { z } from 'zod';
106+
107+
const result = streamObject({
108+
model: anthropic('claude-sonnet-4-20250514'),
109+
schema: z.object({
110+
characters: z.array(
111+
z.object({
112+
name: z.string(),
113+
class: z.string(),
114+
description: z.string(),
115+
}),
116+
),
117+
}),
118+
prompt: 'Generate 3 character descriptions for a fantasy role playing game.',
119+
headers: {
120+
'anthropic-beta': 'fine-grained-tool-streaming-2025-05-14',
121+
},
122+
});
123+
124+
for await (const partialObject of result.partialObjectStream) {
125+
console.log(partialObject);
126+
}
127+
```
128+
129+
#### For tool input streaming with `streamText`
130+
131+
```ts
132+
import { anthropic } from '@ai-sdk/anthropic';
133+
import { streamText, tool } from 'ai';
134+
import { z } from 'zod';
135+
136+
const result = streamText({
137+
model: anthropic('claude-sonnet-4-20250514'),
138+
tools: {
139+
writeFile: tool({
140+
description: 'Write content to a file',
141+
inputSchema: z.object({
142+
path: z.string(),
143+
content: z.string(),
144+
}),
145+
execute: async ({ path, content }) => {
146+
// Implementation
147+
return { success: true };
148+
},
149+
}),
150+
},
151+
prompt: 'Write a short story to story.txt',
152+
headers: {
153+
'anthropic-beta': 'fine-grained-tool-streaming-2025-05-14',
154+
},
155+
});
156+
```
157+
158+
Without this header, tool inputs and structured outputs may arrive all at once after a delay instead of streaming incrementally.
101159

102160
The following optional provider options are available for Anthropic models:
103161

0 commit comments

Comments
 (0)