Skip to content

Commit 4f9ff50

Browse files
committed
feat: AG-UI adapter
AG-UI adapter enabling integration with the AG-UI protocol via the Agent.to_ag_ui() method. This includes a full example for all features in the AG-UI dojo. Fixes: ag-ui-protocol/ag-ui/issues/5
1 parent a25eb96 commit 4f9ff50

32 files changed

+2387
-10
lines changed

adapter_ag_ui/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Pydantic Services Inc. 2024 to present
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

adapter_ag_ui/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# AG-UI Adapter
2+
3+
[![CI](https://github.com/pydantic/pydantic-ai/actions/workflows/ci.yml/badge.svg?event=push)](https://github.com/pydantic/pydantic-ai/actions/workflows/ci.yml?query=branch%3Amain)
4+
[![Coverage](https://coverage-badge.samuelcolvin.workers.dev/pydantic/pydantic-ai.svg)](https://coverage-badge.samuelcolvin.workers.dev/redirect/pydantic/pydantic-ai)
5+
[![PyPI](https://img.shields.io/pypi/v/adapter-ag-ui.svg)](https://pypi.python.org/pypi/adapter-ag-ui)
6+
[![python versions](https://img.shields.io/pypi/pyversions/adapter-ag-ui.svg)](https://github.com/pydantic/pydantic-ai)
7+
[![license](https://img.shields.io/github/license/pydantic/pydantic-ai.svg)](https://github.com/pydantic/pydantic-ai/blob/main/LICENSE)
8+
9+
To make it easier to implement use AG-UI with PydanticAI agents we've
10+
implemented an adapter which handles the translation between PydanticAI
11+
and AG-UI.
12+
13+
See [the docs](https://ai.pydantic.dev/ag_ui/) for more information.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Pydantic AI integration for ag-ui protocol.
2+
3+
This package provides seamless integration between pydantic-ai agents and ag-ui
4+
for building interactive AI applications with streaming event-based communication.
5+
"""
6+
7+
from __future__ import annotations
8+
9+
from .adapter import AdapterAGUI
10+
from .consts import SSE_ACCEPT
11+
from .deps import StateDeps
12+
13+
__all__ = [
14+
'AdapterAGUI',
15+
'SSE_ACCEPT',
16+
'StateDeps',
17+
]

adapter_ag_ui/adapter_ag_ui/_enums.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""Enums for AG-UI protocol."""
2+
3+
from __future__ import annotations
4+
5+
from enum import StrEnum
6+
7+
8+
# TODO(steve): Remove this and all uses once https://github.com/ag-ui-protocol/ag-ui/pull/49 is merged.
9+
class Role(StrEnum):
10+
"""Enum for message roles in AG-UI protocol."""
11+
12+
ASSISTANT = 'assistant'
13+
USER = 'user'
14+
DEVELOPER = 'developer'
15+
SYSTEM = 'system'
16+
TOOL = 'tool'
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""Exceptions for the AI Agent UI module."""
2+
3+
from __future__ import annotations
4+
5+
from dataclasses import InitVar, dataclass
6+
7+
from pydantic import ValidationError as PydanticValidationError
8+
9+
10+
@dataclass
11+
class RunError(Exception):
12+
"""Exception raised for errors during agent runs."""
13+
14+
message: str
15+
code: str
16+
17+
def __str__(self) -> str:
18+
return self.message
19+
20+
21+
@dataclass(kw_only=True)
22+
class UnexpectedToolCallError(RunError):
23+
"""Exception raised when an unexpected tool call is encountered."""
24+
25+
tool_name: InitVar[str]
26+
message: str = ''
27+
code: str = 'unexpected_tool_call'
28+
29+
def __post_init__(self, tool_name: str) -> None:
30+
"""Set the message for the unexpected tool call.
31+
32+
Args:
33+
tool_name: The name of the tool that was unexpectedly called.
34+
"""
35+
self.message = f'unexpected tool call name={tool_name}'
36+
37+
38+
@dataclass
39+
class NoMessagesError(RunError):
40+
"""Exception raised when no messages are found in the input."""
41+
42+
message: str = 'no messages found in the input'
43+
code: str = 'no_messages'
44+
45+
46+
@dataclass
47+
class InvalidStateError(RunError, PydanticValidationError):
48+
"""Exception raised when an invalid state is provided."""
49+
50+
message: str = 'invalid state provided'
51+
code: str = 'invalid_state'

0 commit comments

Comments
 (0)