Skip to content

feat(ai): add safeValidateUIMessages#8680

Merged
gr2m merged 5 commits intovercel:mainfrom
fveiraswww:feat/add-safe-validate-ui-messages
Sep 18, 2025
Merged

feat(ai): add safeValidateUIMessages#8680
gr2m merged 5 commits intovercel:mainfrom
fveiraswww:feat/add-safe-validate-ui-messages

Conversation

@fveiraswww
Copy link
Contributor

@fveiraswww fveiraswww commented Sep 16, 2025

Background

The current validateUIMessages throws on validation failure. For API endpoints and similar contexts it's ergonomically preferable to use a safe-return pattern (like zod's safeParse) to avoid try/catch and get a typed success/failure result.

see context in #8673

Summary

  • Add safeValidateUIMessages
  • Returns a discriminated union: { success: true, data } || { success: false, reason, error }

Manual Verification

  • Ran unit tests in packages/ai

Tasks

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • Formatting issues have been fixed (run pnpm prettier-fix in the project root)
  • I have reviewed this pull request (self-review)

Related Issues

fixes #8673

Copy link
Collaborator

@gr2m gr2m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add documentation as we have for validateUIMessages(): https://ai-sdk.dev/docs/reference/ai-sdk-core/validate-ui-messages

@gr2m
Copy link
Collaborator

gr2m commented Sep 16, 2025

thanks for the pull request 👍🏼 if you could comment on the issue that you are working on, that would be great! For one, it allows us to assign the issue to you, and second people know that somebody is already working on it and won't duplicate your work

@fveiraswww
Copy link
Contributor Author

@gr2m I added the docs following the same style as validateUIMessages and applied the other fixes

@lgrammel I refactored validateUIMessages to wrap safeValidateUIMessages as suggested ✅

@fveiraswww fveiraswww requested a review from gr2m September 18, 2025 11:58
@gr2m gr2m added backport feature New feature or request ai/core labels Sep 18, 2025
@gr2m gr2m enabled auto-merge (squash) September 18, 2025 18:10
@gr2m gr2m merged commit a7f6f81 into vercel:main Sep 18, 2025
15 of 16 checks passed
@gr2m gr2m added backport and removed backport labels Sep 18, 2025
vercel-ai-sdk bot pushed a commit that referenced this pull request Sep 18, 2025
## Background

The current validateUIMessages throws on validation failure. For API
endpoints and similar contexts it's ergonomically preferable to use a
safe-return pattern (like zod's safeParse) to avoid try/catch and get a
typed success/failure result.

see context in #8673 

## Summary

- Add `safeValidateUIMessages`
- Returns a discriminated union: `{ success: true, data }` || `{
success: false, reason, error }`

## Manual Verification

- Ran unit tests in packages/ai

## Tasks

- [x] Tests have been added / updated (for bug fixes / features)
- [x] 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] Formatting issues have been fixed (run `pnpm prettier-fix` in the
project root)
- [x] I have reviewed this pull request (self-review)

## Related Issues

fixes #8673

---------

Co-authored-by: Francisco Veiras <74626997+francisco-svg761@users.noreply.github.com>
Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com>
@vercel-ai-sdk vercel-ai-sdk bot removed the backport label Sep 18, 2025
@vercel-ai-sdk
Copy link
Contributor

vercel-ai-sdk bot commented Sep 18, 2025

✅ Backport PR created: #8742

gr2m added a commit that referenced this pull request Sep 18, 2025
This is an automated backport of #8680 to the v5.0 branch.

Co-authored-by: Francisco Veiras <74626997+fveiraswww@users.noreply.github.com>
Co-authored-by: Francisco Veiras <74626997+francisco-svg761@users.noreply.github.com>
Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai/core feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feature request] Safe variant of validateUIMessages

3 participants