Skip to content

feat(ai): add validateUIMessages function#8065

Merged
lgrammel merged 24 commits intomainfrom
lg/2iK9Xbix
Aug 15, 2025
Merged

feat(ai): add validateUIMessages function#8065
lgrammel merged 24 commits intomainfrom
lg/2iK9Xbix

Conversation

@lgrammel
Copy link
Copy Markdown
Collaborator

@lgrammel lgrammel commented Aug 15, 2025

Background

Validating UI messages is often necessary, e.g. when they are loaded from the DB or received through REST requests.

Summary

Add validateUIMessages function.

Example

const messages = await validateUIMessages<UseChatToolsMessage>({
  messages: body.messages,
  tools,
});

Manual Verification

  • test ui tool calling example with validation

Tasks

  • implementation TextUIPart
  • implementation ReasoningUIPart
  • implementation SourceUrlUIPart
  • implementation SourceDocumentUIPart
  • implementation FileUIPart
  • metadata
  • implementation StepStartUIPart
  • implementation DataUIPart<DATA_TYPES>
  • implementation DynamicToolUIPart
  • implementation ToolUIPart
  • test provider metadata
  • test data ui part missing schema
  • documentation (reference)
  • documentation (guide)
  • example updates
  • changeset

Related Issues

Fixes #6763

@imranbarbhuiya
Copy link
Copy Markdown
Contributor

I tried this here imranbarbhuiya@caccd03#diff-359eed88eef8047dde41894dee7f556365015932d5dfcdb994e5c13db9c2c1fb but there were some deep typing issues in test so I stopped, but it has most of the schemas, u can probably use some from there

});

const dataUIPartSchema = z.object({
type: z.string().startsWith('data-'),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

since u r using zod v4, u can use

Suggested change
type: z.string().startsWith('data-'),
type: z.templateLiteral([z.literal('data-'), z.string()]),

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

and make it less readable when it is not needed? why would i do that?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

it gives proper type like the interface currently has data-${string} instead of just string, other then than yeah current one is better readable

Copy link
Copy Markdown
Collaborator Author

@lgrammel lgrammel Aug 15, 2025

Choose a reason for hiding this comment

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

here we wont push everything into zod because the dynamic union types are too complex, so it doesnt matter, hence the simpler version

@lgrammel lgrammel changed the title ui message validation feat(ai): add validateUIMessages function Aug 15, 2025
@lgrammel lgrammel marked this pull request as ready for review August 15, 2025 13:57
@lgrammel lgrammel merged commit ca4f68f into main Aug 15, 2025
9 checks passed
@lgrammel lgrammel deleted the lg/2iK9Xbix branch August 15, 2025 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for validating UIMessage[]

3 participants