File tree Expand file tree Collapse file tree 2 files changed +27
-2
lines changed
pydantic_ai_slim/pydantic_ai/ui/ag_ui Expand file tree Collapse file tree 2 files changed +27
-2
lines changed Original file line number Diff line number Diff line change 22
33from __future__ import annotations
44
5- from collections .abc import Sequence
5+ from collections .abc import Mapping , Sequence
66from functools import cached_property
77from typing import (
88 TYPE_CHECKING ,
@@ -107,7 +107,14 @@ def toolset(self) -> AbstractToolset[AgentDepsT] | None:
107107 @cached_property
108108 def state (self ) -> dict [str , Any ] | None :
109109 """Frontend state from the AG-UI run input."""
110- return self .run_input .state
110+ state = self .run_input .state
111+ if state is None :
112+ return None
113+
114+ if isinstance (state , Mapping ) and not state :
115+ return None
116+
117+ return state
111118
112119 @classmethod
113120 def load_messages (cls , messages : Sequence [Message ]) -> list [ModelMessage ]:
Original file line number Diff line number Diff line change @@ -1193,6 +1193,24 @@ async def test_request_with_state_without_handler() -> None:
11931193 pass
11941194
11951195
1196+ async def test_request_with_empty_state_without_handler () -> None :
1197+ agent = Agent (model = FunctionModel (stream_function = simple_stream ))
1198+
1199+ run_input = create_input (
1200+ UserMessage (
1201+ id = 'msg_1' ,
1202+ content = 'Hello, how are you?' ,
1203+ ),
1204+ state = {},
1205+ )
1206+
1207+ events = list [dict [str , Any ]]()
1208+ async for event in run_ag_ui (agent , run_input ):
1209+ events .append (json .loads (event .removeprefix ('data: ' )))
1210+
1211+ assert events == simple_result ()
1212+
1213+
11961214async def test_request_with_state_with_custom_handler () -> None :
11971215 @dataclass
11981216 class CustomStateDeps :
You can’t perform that action at this time.
0 commit comments