-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Vercel AI Elements types #2923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Vercel AI Elements types #2923
Conversation
Docs Preview
|
This would be a wonderful addon, looking forward to see it soon in main. |
@samuelcolvin can you add it to docs such that it would be integrated as chat service in existing Fullstack apps that uses pydantic-ai as Agent stack. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we rename the directory to just vercel_ai
to keep things simple.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for this! I left some comments on the code and I will try to test this and get back.
messages: list[UIMessage] | ||
|
||
model: str | ||
web_search: bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From what I understand you can have arbitrary body and metadata in the request: https://ai-sdk.dev/docs/ai-sdk-ui/chatbot#request-configuration
The examples show model
and web_search
in the body but this is arbitrary.
|
||
__all__ = 'sse_stream', 'VERCEL_AI_ELEMENTS_HEADERS', 'EventStreamer' | ||
# no idea if this is important, but vercel sends it, therefore so am I | ||
VERCEL_AI_ELEMENTS_HEADERS = {'x-vercel-ai-ui-message-stream': 'v1'} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, very important also mentioned here: https://ai-sdk.dev/docs/ai-sdk-ui/stream-protocol#data-stream-protocol
from ..tools import AgentDepsT | ||
from . import response_types as _t | ||
|
||
__all__ = 'sse_stream', 'VERCEL_AI_ELEMENTS_HEADERS', 'EventStreamer' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
technically these events are for the "AI SDK UI"'s data stream protocol and not necessarily Vercel AI elements. https://ai-sdk.dev/docs/ai-sdk-ui/stream-protocol
if not isinstance(event, AgentRunResultEvent): | ||
async for chunk in event_streamer.event_to_chunks(event): | ||
yield chunk.sse() | ||
async for chunk in event_streamer.finish(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are not sending any EndChunk like TextEndChunk or ReasoningEndChunk. Not sure how important that is. I had them in my implementation: https://gist.github.com/183amir/ce45cf52f034b493fac0bb4b1838236a#file-vercel_ai-py-L497
class DataUIPart(CamelBaseModel): | ||
"""Data part with dynamic type based on data name.""" | ||
|
||
type: str # Will be f"data-{NAME}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can have type: str = pydantic.Field(pattern=r"^data-.*$")
This adds support for Vercel AI Elements streams to Pydantic AI.
There's an example frontend in a separate repo github.com/pydantic/pydantic-ai-chat the plan is to either make that into a more complete template, or release a
pydantic-ai-chat
python library which contains a pre-built react frontend.@DouweM we should rename this to use the proper terminology of the "Vercel AI Data Stream Protocol"
Here's a demo of basic usage:
pydantic-ai-chat.mp4