-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathstructured-outputs-streaming.ts
More file actions
executable file
·49 lines (41 loc) · 1.35 KB
/
structured-outputs-streaming.ts
File metadata and controls
executable file
·49 lines (41 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env -S npm run tsn -T
import { zodOutputFormat } from '@anthropic-ai/sdk/helpers/zod';
import Anthropic from '@anthropic-ai/sdk';
import { z } from 'zod';
const WeatherResponse = z.object({
city: z.string(),
temperature: z.number(),
conditions: z.array(z.string()),
forecast: z.array(
z.object({
day: z.string(),
high: z.number(),
low: z.number(),
condition: z.string(),
}),
),
});
async function main() {
const client = new Anthropic();
const stream = client.messages.stream({
model: 'claude-sonnet-4-5',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Provide a weather report for San Francisco.' }],
output_config: {
format: zodOutputFormat(WeatherResponse),
},
});
for await (const event of stream) {
if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') {
process.stdout.write(event.delta.text);
}
}
// Get the final parsed result
const finalMessage = await stream.finalMessage();
console.log('\n\n=== Final Parsed Results ===');
console.log('City:', finalMessage.parsed_output?.city);
console.log('Temperature:', finalMessage.parsed_output?.temperature);
console.log('Conditions:', finalMessage.parsed_output?.conditions);
console.log('Forecast:', finalMessage.parsed_output?.forecast);
}
main().catch(console.error);