Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions pydantic_ai_slim/pydantic_ai/ui/ag_ui/_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

from __future__ import annotations

from collections.abc import Sequence
from collections.abc import Mapping, Sequence
from functools import cached_property
from typing import (
TYPE_CHECKING,
Any,
cast,
)

from ... import ExternalToolset, ToolDefinition
Expand Down Expand Up @@ -107,7 +108,14 @@ def toolset(self) -> AbstractToolset[AgentDepsT] | None:
@cached_property
def state(self) -> dict[str, Any] | None:
"""Frontend state from the AG-UI run input."""
return self.run_input.state
state = self.run_input.state
if state is None:
return None

if isinstance(state, Mapping) and not state:
return None

return cast('dict[str, Any]', state)

@classmethod
def load_messages(cls, messages: Sequence[Message]) -> list[ModelMessage]:
Expand Down
39 changes: 39 additions & 0 deletions tests/test_ag_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,27 @@ async def simple_stream(messages: list[ModelMessage], agent_info: AgentInfo) ->
yield '(no tool calls)'


async def test_agui_adapter_state_none() -> None:
"""Ensure adapter exposes `None` state when no frontend state provided."""
agent = Agent(
model=FunctionModel(stream_function=simple_stream),
)

run_input = RunAgentInput(
thread_id=uuid_str(),
run_id=uuid_str(),
messages=[],
state=None,
context=[],
tools=[],
forwarded_props=None,
)

adapter = AGUIAdapter(agent=agent, run_input=run_input, accept=None)

assert adapter.state is None


async def test_basic_user_message() -> None:
"""Test basic user message with text response."""
agent = Agent(
Expand Down Expand Up @@ -1193,6 +1214,24 @@ async def test_request_with_state_without_handler() -> None:
pass


async def test_request_with_empty_state_without_handler() -> None:
agent = Agent(model=FunctionModel(stream_function=simple_stream))

run_input = create_input(
UserMessage(
id='msg_1',
content='Hello, how are you?',
),
state={},
)

events = list[dict[str, Any]]()
async for event in run_ag_ui(agent, run_input):
events.append(json.loads(event.removeprefix('data: ')))

assert events == simple_result()


async def test_request_with_state_with_custom_handler() -> None:
@dataclass
class CustomStateDeps:
Expand Down