Skip to content

Commit e619d5e

Browse files
authored
Send AG-UI thinking start and end events (#2855)
1 parent f9cd68d commit e619d5e

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

pydantic_ai_slim/pydantic_ai/ag_ui.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@
6868
TextMessageContentEvent,
6969
TextMessageEndEvent,
7070
TextMessageStartEvent,
71-
# TODO: Enable once https://github.com/ag-ui-protocol/ag-ui/issues/289 is resolved.
72-
# ThinkingEndEvent,
73-
# ThinkingStartEvent,
71+
ThinkingEndEvent,
72+
ThinkingStartEvent,
7473
ThinkingTextMessageContentEvent,
7574
ThinkingTextMessageEndEvent,
7675
ThinkingTextMessageStartEvent,
@@ -396,10 +395,9 @@ async def _agent_stream(run: AgentRun[AgentDepsT, Any]) -> AsyncIterator[BaseEve
396395
yield stream_ctx.part_end
397396
stream_ctx.part_end = None
398397
if stream_ctx.thinking:
399-
# TODO: Enable once https://github.com/ag-ui-protocol/ag-ui/issues/289 is resolved.
400-
# yield ThinkingEndEvent(
401-
# type=EventType.THINKING_END,
402-
# )
398+
yield ThinkingEndEvent(
399+
type=EventType.THINKING_END,
400+
)
403401
stream_ctx.thinking = False
404402
elif isinstance(node, CallToolsNode):
405403
async with node.stream(run.ctx) as handle_stream:
@@ -431,10 +429,9 @@ async def _handle_model_request_event( # noqa: C901
431429
part = agent_event.part
432430
if isinstance(part, ThinkingPart): # pragma: no branch
433431
if not stream_ctx.thinking:
434-
# TODO: Enable once https://github.com/ag-ui-protocol/ag-ui/issues/289 is resolved.
435-
# yield ThinkingStartEvent(
436-
# type=EventType.THINKING_START,
437-
# )
432+
yield ThinkingStartEvent(
433+
type=EventType.THINKING_START,
434+
)
438435
stream_ctx.thinking = True
439436

440437
if part.content:
@@ -450,10 +447,9 @@ async def _handle_model_request_event( # noqa: C901
450447
)
451448
else:
452449
if stream_ctx.thinking:
453-
# TODO: Enable once https://github.com/ag-ui-protocol/ag-ui/issues/289 is resolved.
454-
# yield ThinkingEndEvent(
455-
# type=EventType.THINKING_END,
456-
# )
450+
yield ThinkingEndEvent(
451+
type=EventType.THINKING_END,
452+
)
457453
stream_ctx.thinking = False
458454

459455
if isinstance(part, TextPart):

tests/test_ag_ui.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,13 +892,16 @@ async def stream_function(
892892
'threadId': (thread_id := IsSameStr()),
893893
'runId': (run_id := IsSameStr()),
894894
},
895+
{'type': 'THINKING_START'},
896+
{'type': 'THINKING_END'},
895897
{'type': 'TEXT_MESSAGE_START', 'messageId': (message_id := IsSameStr()), 'role': 'assistant'},
896898
{
897899
'type': 'TEXT_MESSAGE_CONTENT',
898900
'messageId': message_id,
899901
'delta': "Let's do some thinking",
900902
},
901903
{'type': 'TEXT_MESSAGE_END', 'messageId': message_id},
904+
{'type': 'THINKING_START'},
902905
{'type': 'THINKING_TEXT_MESSAGE_START'},
903906
{'type': 'THINKING_TEXT_MESSAGE_CONTENT', 'delta': 'Thinking '},
904907
{'type': 'THINKING_TEXT_MESSAGE_CONTENT', 'delta': 'about the weather'},
@@ -909,6 +912,7 @@ async def stream_function(
909912
{'type': 'THINKING_TEXT_MESSAGE_START'},
910913
{'type': 'THINKING_TEXT_MESSAGE_CONTENT', 'delta': 'Thinking about the universe'},
911914
{'type': 'THINKING_TEXT_MESSAGE_END'},
915+
{'type': 'THINKING_END'},
912916
{
913917
'type': 'RUN_FINISHED',
914918
'threadId': thread_id,

0 commit comments

Comments
 (0)