-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Support Vercel AI Data Stream Protocol #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
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. |
183amir
left a comment
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.
pydantic_ai_slim/pydantic_ai/vercel_ai_elements/request_types.py
Outdated
Show resolved
Hide resolved
pydantic_ai_slim/pydantic_ai/vercel_ai_elements/response_stream.py
Outdated
Show resolved
Hide resolved
pydantic_ai_slim/pydantic_ai/vercel_ai_elements/response_stream.py
Outdated
Show resolved
Hide resolved
pydantic_ai_slim/pydantic_ai/vercel_ai_elements/response_stream.py
Outdated
Show resolved
Hide resolved
# Conflicts: # examples/pydantic_ai_examples/chat_app.py # pydantic_ai_slim/pydantic_ai/agent/abstract.py # pydantic_ai_slim/pydantic_ai/run.py
# Conflicts: # pyproject.toml # tests/models/test_deepseek.py # tests/models/test_huggingface.py # uv.lock
# Conflicts: # docs/ui/ag-ui.md # examples/pydantic_ai_examples/ag_ui/api/agentic_chat.py # examples/pydantic_ai_examples/ag_ui/api/tool_based_generative_ui.py
Implements a classmethod to convert Pydantic AI messages to AG-UI format, providing the reverse operation of the existing load_messages() method. This addresses PR pydantic#3068 feedback to use the new AGUIAdapter pattern introduced in PR pydantic#2923 instead of a standalone function. Changes: - Add AGUIAdapter.dump_messages() classmethod in ui/ag_ui/_adapter.py - Create helper functions: _convert_request_part(), _convert_response_parts(), _get_builtin_tool_call_id() - Handle all message part types: UserPromptPart, SystemPromptPart, ToolReturnPart, RetryPromptPart, TextPart, ToolCallPart, BuiltinToolCallPart, BuiltinToolReturnPart, ThinkingPart - Use part.model_response_str() for tool return content - Use part.args_as_json_str() for tool call arguments - Use part.model_response() for retry prompt content - Combine ToolCallPart and BuiltinToolCallPart handling using BaseToolCallPart - Handle text flow interruption by builtin tools (add \n\n separator) - Skip ThinkingPart in message history conversion - Add comprehensive test coverage with 3 new tests 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
To do:
clai weborAgent.to_web()?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-chatpython 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