You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
AI SDK 6 stabilizes structured output support for agents, enabling you to generate structured data alongside multi-step tool calling.
257
+
258
+
Previously, you could only generate structured outputs with `generateObject` and `streamObject`, which didn't support tool calling. Now `ToolLoopAgent` (and `generateText` / `streamText`) can combine both capabilities using the `output` parameter:
259
+
260
+
```typescript
261
+
import { Output, ToolLoopAgent, tool } from'ai';
262
+
import { openai } from'@ai-sdk/openai';
263
+
import { z } from'zod';
264
+
265
+
const agent =newToolLoopAgent({
266
+
model: openai('gpt-4o'),
267
+
tools: {
268
+
weather: tool({
269
+
description: 'Get the weather in a location',
270
+
inputSchema: z.object({
271
+
city: z.string(),
272
+
}),
273
+
execute: async ({ city }) => {
274
+
return { temperature: 72, condition: 'sunny' };
275
+
},
276
+
}),
277
+
},
278
+
output: Output.object({
279
+
schema: z.object({
280
+
summary: z.string(),
281
+
temperature: z.number(),
282
+
recommendation: z.string(),
283
+
}),
284
+
}),
285
+
});
286
+
287
+
const { output } =awaitagent.generate({
288
+
prompt: 'What is the weather in San Francisco and what should I wear?',
289
+
});
290
+
// The agent calls the weather tool AND returns structured output
291
+
console.log(output);
292
+
// {
293
+
// summary: "It's sunny in San Francisco",
294
+
// temperature: 72,
295
+
// recommendation: "Wear light clothing and sunglasses"
296
+
// }
297
+
```
298
+
299
+
### Output Types
300
+
301
+
The `Output` object provides multiple strategies for structured generation:
302
+
303
+
-**`Output.object()`**: Generate structured objects with Zod schemas
304
+
-**`Output.array()`**: Generate arrays of structured objects
305
+
-**`Output.choice()`**: Select from a specific set of options
306
+
-**`Output.text()`**: Generate plain text (default behavior)
307
+
308
+
### Streaming Structured Output
309
+
310
+
Use `agent.stream()` to stream structured output as it's being generated:
311
+
312
+
```typescript
313
+
import { ToolLoopAgent, Output } from'ai';
314
+
import { openai } from'@ai-sdk/openai';
315
+
import { z } from'zod';
316
+
317
+
const profileAgent =newToolLoopAgent({
318
+
model: openai('gpt-4o'),
319
+
instructions: 'Generate realistic person profiles.',
Structured outputs are also supported in `generateText` and `streamText` functions, allowing you to use this feature outside of agents when needed.
344
+
345
+
<Note>
346
+
When using structured output with `generateText` or `streamText`, you must
347
+
configure multiple steps with `stopWhen` because generating the structured
348
+
output is itself a step. For example: `stopWhen: stepCountIs(2)` to allow tool
349
+
calling and output generation.
350
+
</Note>
351
+
352
+
## Reranking Support
353
+
354
+
AI SDK 6 introduces native support for reranking, a technique that improves search relevance by reordering documents based on their relationship to a query.
355
+
356
+
Unlike embedding-based similarity search, reranking models are specifically trained to understand query-document relationships, producing more accurate relevance scores:
357
+
358
+
```typescript
359
+
import { rerank } from'ai';
360
+
import { cohere } from'@ai-sdk/cohere';
361
+
362
+
const documents = [
363
+
'sunny day at the beach',
364
+
'rainy afternoon in the city',
365
+
'snowy night in the mountains',
366
+
];
367
+
368
+
const { ranking } =awaitrerank({
369
+
model: cohere.reranking('rerank-v3.5'),
370
+
documents,
371
+
query: 'talk about rain',
372
+
topN: 2,
373
+
});
374
+
375
+
console.log(ranking);
376
+
// [
377
+
// { originalIndex: 1, score: 0.9, document: 'rainy afternoon in the city' },
378
+
// { originalIndex: 0, score: 0.3, document: 'sunny day at the beach' }
379
+
// ]
380
+
```
381
+
382
+
### Structured Document Reranking
383
+
384
+
Reranking also supports structured documents, making it ideal for searching through databases, emails, or other structured content:
385
+
386
+
```typescript
387
+
import { rerank } from'ai';
388
+
import { cohere } from'@ai-sdk/cohere';
389
+
390
+
const documents = [
391
+
{
392
+
from: 'Paul Doe',
393
+
subject: 'Follow-up',
394
+
text: 'We are happy to give you a discount of 20% on your next order.',
395
+
},
396
+
{
397
+
from: 'John McGill',
398
+
subject: 'Missing Info',
399
+
text: 'Sorry, but here is the pricing information from Oracle: $5000/month',
0 commit comments