Skip to content

[v6.0] fix(ai): return validated elements from array output parseCompleteOutput#16791

Merged
gr2m merged 1 commit into
release-v6.0from
backport-v6.0/fix-15734
Jul 6, 2026
Merged

[v6.0] fix(ai): return validated elements from array output parseCompleteOutput#16791
gr2m merged 1 commit into
release-v6.0from
backport-v6.0/fix-15734

Conversation

@gr2m

@gr2m gr2m commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

Backport of #15734 to release-v6.0 (clean cherry-pick). Array output previously returned the unvalidated elements, silently dropping schema transforms/coercions/defaults.

Part of the v6.0 backport tracking issue #16767.

…put (#15734)

## Background

When using `generateText` with `output: Output.array(...)`, the
`parseCompleteOutput` method validated each element with
`safeValidateTypes` but discarded `validationResult.value`, returning
the raw parsed JSON (`outerValue.elements`) instead. This silently
dropped any schema transforms (`.transform()`), coercions (`.coerce`),
and defaults (`.default()`), causing the TypeScript return type
`Array<ELEMENT>` to be incorrect at runtime.

## Summary

- Collect `validationResult.value` during the validation loop and return
the validated array instead of `outerValue.elements`
- Remove the unsafe `as Array<ELEMENT>` type assertion
- Add regression tests including one with a `.transform()` schema that
proves returned values come from validation, not raw JSON

## Manual Verification

Ran the `output.test.ts` test suite — all 62 tests pass, including the
new transform test which would fail on the old code (it would return `{
content: "hello" }` instead of `{ content: "hello", extra: true }`).

## Checklist

- [x] All commits are signed (PRs with unsigned commits cannot be
merged)
- [x] Tests have been added / updated (for bug fixes / features)
- [ ] Documentation has been added / updated (for bug fixes / features)
- [x] A _patch_ changeset for relevant packages has been added (for bug
fixes / features - run `pnpm changeset` in the project root)
- [x] I have reviewed this pull request (self-review)

## Related Issues

Fixes #15708

(cherry picked from commit d66ae02)
@gr2m gr2m added the backport Admins only: add this label to a pull request in order to backport it to the prior version label Jul 6, 2026
@gr2m gr2m merged commit 45a9cbf into release-v6.0 Jul 6, 2026
52 checks passed
@gr2m gr2m deleted the backport-v6.0/fix-15734 branch July 6, 2026 20:06
github-actions Bot added a commit that referenced this pull request Jul 6, 2026
@github-actions github-actions Bot removed the backport Admins only: add this label to a pull request in order to backport it to the prior version label Jul 6, 2026
@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

⚠️ Backport to release-v5.0 created but has conflicts: #16819

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants