Repository navigation
Commit 6786412
authored
fix(ai-sdk)!: disable output validation in
`createToolFactory` validated (and transformed) procedure outputs, but
its types described the output schema's **input** side. The AI SDK also
treats a tool's output as that input side, so the runtime was the odd
one out:
```ts
const t = createToolFactory()(os.output(z.string().transform((s) => s.length)).handler(() => 'hello'))
// InferToolOutput<typeof t> is `string`, but `await t.execute(...)` returned `5`
```
In AI SDK v7, `asSchema` turns a schema into JSON Schema from its input
side (`jsonSchema.input(...)`, or Zod with `io: 'input'`).
`validateUIMessages` also checks stored tool outputs by running
`outputSchema`'s `validate`. So a stored, already-transformed output
(`5`) failed `safeValidateUIMessages` against `z.string()`. The AI SDK
never validates what `execute` returns while a tool runs.
## Changes
- `createToolFactory` now disables output validation as well as input
validation. The tool outputs what the handler returns (the schema
input), matching `implementToolFactory` and plain AI SDK tools. The
types are unchanged; they were already the input side.
- Docs: the Tool Factory section now notes that tool outputs aren't
validated or transformed. The streaming section says "declared with
`asyncIteratorObject`" instead of "validated with".
## Breaking change
`createToolFactory` tools no longer validate or transform procedure
outputs, including each event of an `asyncIteratorObject` output. An
invalid handler result now reaches the model unchanged instead of
throwing `Output validation failed`.
## Tests
In `tool.test.ts`:
- A transforming output schema now returns the handler's value
(`'hello'`), and that stored output passes `safeValidateUIMessages`.
- Streamed events are no longer validated against the yield schema.
- Removed the two tests that asserted output validation errors.
I checked that the new tests fail against `main`'s `tool.ts`. `pnpm
check`, `pnpm docs:validate` and `pnpm test` pass.createToolFactory (#2465)1 parent ce8dfeb commit 6786412
3 files changed
Lines changed: 42 additions & 31 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
200 | 200 | | |
201 | 201 | | |
202 | 202 | | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
203 | 207 | | |
204 | 208 | | |
205 | | - | |
| 209 | + | |
206 | 210 | | |
207 | 211 | | |
208 | 212 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
274 | 274 | | |
275 | 275 | | |
276 | 276 | | |
277 | | - | |
| 277 | + | |
278 | 278 | | |
279 | 279 | | |
280 | 280 | | |
| |||
539 | 539 | | |
540 | 540 | | |
541 | 541 | | |
542 | | - | |
543 | | - | |
544 | | - | |
545 | | - | |
546 | | - | |
| 542 | + | |
| 543 | + | |
547 | 544 | | |
548 | 545 | | |
549 | 546 | | |
550 | | - | |
551 | | - | |
552 | | - | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
553 | 570 | | |
554 | 571 | | |
555 | 572 | | |
| |||
600 | 617 | | |
601 | 618 | | |
602 | 619 | | |
603 | | - | |
604 | | - | |
605 | | - | |
606 | | - | |
607 | | - | |
608 | | - | |
609 | | - | |
610 | | - | |
611 | | - | |
612 | | - | |
613 | | - | |
614 | | - | |
615 | | - | |
| 620 | + | |
616 | 621 | | |
617 | 622 | | |
618 | 623 | | |
| |||
623 | 628 | | |
624 | 629 | | |
625 | 630 | | |
626 | | - | |
627 | | - | |
628 | | - | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
629 | 637 | | |
630 | 638 | | |
631 | 639 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
332 | 332 | | |
333 | 333 | | |
334 | 334 | | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
| 335 | + | |
| 336 | + | |
339 | 337 | | |
340 | 338 | | |
341 | 339 | | |
342 | 340 | | |
| 341 | + | |
343 | 342 | | |
344 | 343 | | |
345 | 344 | | |
| |||
0 commit comments