1010
1111from __future__ import annotations
1212
13- from dataclasses import dataclass , field
1413from typing import Any , AsyncIterator
14+ from dataclasses import field , dataclass
1515
1616import pytest
1717from pydantic_ai .messages import (
18- FunctionToolResultEvent ,
19- PartDeltaEvent ,
20- PartEndEvent ,
21- PartStartEvent ,
22- RetryPromptPart ,
2318 TextPart ,
24- TextPartDelta ,
19+ PartEndEvent ,
2520 ThinkingPart ,
26- ThinkingPartDelta ,
2721 ToolCallPart ,
22+ TextPartDelta ,
23+ PartDeltaEvent ,
24+ PartStartEvent ,
2825 ToolReturnPart ,
26+ RetryPromptPart ,
27+ ThinkingPartDelta ,
28+ FunctionToolResultEvent ,
2929)
3030
31- from agentex .lib .adk ._modules ._pydantic_ai_async import stream_pydantic_ai_events
32- from agentex .types .reasoning_content import ReasoningContent
33- from agentex .types .reasoning_content_delta import ReasoningContentDelta
3431from agentex .types .task_message import TaskMessage
32+ from agentex .types .text_content import TextContent
33+ from agentex .types .reasoning_content import ReasoningContent
3534from agentex .types .task_message_delta import TextDelta
3635from agentex .types .task_message_update import StreamTaskMessageDelta
37- from agentex .types .text_content import TextContent
3836from agentex .types .tool_request_content import ToolRequestContent
3937from agentex .types .tool_response_content import ToolResponseContent
40-
38+ from agentex .types .reasoning_content_delta import ReasoningContentDelta
39+ from agentex .lib .adk ._modules ._pydantic_ai_async import stream_pydantic_ai_events
4140
4241TASK_ID = "task_test"
4342
@@ -70,9 +69,7 @@ async def __aexit__(self, exc_type, exc_val, exc_tb) -> bool:
7069
7170 async def stream_update (self , update : StreamTaskMessageDelta ) -> None :
7271 if self .closed :
73- raise AssertionError (
74- "stream_update called after close — helper closed the wrong context"
75- )
72+ raise AssertionError ("stream_update called after close — helper closed the wrong context" )
7673 self .updates .append (update )
7774
7875 async def close (self ) -> None :
@@ -85,9 +82,7 @@ class FakeStreamingModule:
8582 def __init__ (self ) -> None :
8683 self .contexts : list [FakeContext ] = []
8784
88- def streaming_task_message_context (
89- self , * , task_id : str , initial_content : Any
90- ) -> FakeContext :
85+ def streaming_task_message_context (self , * , task_id : str , initial_content : Any ) -> FakeContext :
9186 tm = TaskMessage (
9287 id = f"m{ len (self .contexts ) + 1 } " ,
9388 task_id = task_id ,
@@ -255,9 +250,7 @@ async def test_empty_thinking_delta_is_skipped(
255250 await stream_pydantic_ai_events (_aiter (events ), TASK_ID )
256251
257252 ctx = streaming .contexts [0 ]
258- assert _reasoning_deltas (ctx ) == [], (
259- "Empty ThinkingPartDelta must not publish a zero-length reasoning delta"
260- )
253+ assert _reasoning_deltas (ctx ) == [], "Empty ThinkingPartDelta must not publish a zero-length reasoning delta"
261254 assert ctx .closed is True
262255
263256
@@ -274,9 +267,7 @@ async def test_tool_call_emits_full_tool_request_message_on_part_end(
274267 ),
275268 PartEndEvent (
276269 index = 1 ,
277- part = ToolCallPart (
278- tool_name = "get_weather" , args = '{"city":"Paris"}' , tool_call_id = "c1"
279- ),
270+ part = ToolCallPart (tool_name = "get_weather" , args = '{"city":"Paris"}' , tool_call_id = "c1" ),
280271 ),
281272 ]
282273 await stream_pydantic_ai_events (_aiter (events ), TASK_ID )
@@ -299,15 +290,11 @@ async def test_tool_call_with_dict_args_passes_through(
299290 events = [
300291 PartStartEvent (
301292 index = 0 ,
302- part = ToolCallPart (
303- tool_name = "search" , args = {"q" : "weather" }, tool_call_id = "c"
304- ),
293+ part = ToolCallPart (tool_name = "search" , args = {"q" : "weather" }, tool_call_id = "c" ),
305294 ),
306295 PartEndEvent (
307296 index = 0 ,
308- part = ToolCallPart (
309- tool_name = "search" , args = {"q" : "weather" }, tool_call_id = "c"
310- ),
297+ part = ToolCallPart (tool_name = "search" , args = {"q" : "weather" }, tool_call_id = "c" ),
311298 ),
312299 ]
313300 await stream_pydantic_ai_events (_aiter (events ), TASK_ID )
@@ -366,9 +353,7 @@ async def test_tool_return_emits_full_tool_response_message(
366353 _ , messages = fake_adk
367354 events = [
368355 FunctionToolResultEvent (
369- part = ToolReturnPart (
370- tool_name = "get_weather" , content = "Sunny, 72F" , tool_call_id = "c1"
371- ),
356+ part = ToolReturnPart (tool_name = "get_weather" , content = "Sunny, 72F" , tool_call_id = "c1" ),
372357 ),
373358 ]
374359 await stream_pydantic_ai_events (_aiter (events ), TASK_ID )
@@ -387,9 +372,7 @@ async def test_tool_return_with_non_string_content_stringifies(
387372 _ , messages = fake_adk
388373 events = [
389374 FunctionToolResultEvent (
390- part = ToolReturnPart (
391- tool_name = "t" , content = {"temp" : 72 , "sky" : "clear" }, tool_call_id = "c"
392- ),
375+ part = ToolReturnPart (tool_name = "t" , content = {"temp" : 72 , "sky" : "clear" }, tool_call_id = "c" ),
393376 ),
394377 ]
395378 await stream_pydantic_ai_events (_aiter (events ), TASK_ID )
@@ -447,9 +430,7 @@ async def test_text_then_tool_then_text_uses_separate_contexts_in_order(
447430 part = ToolCallPart (tool_name = "get_weather" , args = "{}" , tool_call_id = "c1" ),
448431 ),
449432 FunctionToolResultEvent (
450- part = ToolReturnPart (
451- tool_name = "get_weather" , content = "Sunny" , tool_call_id = "c1"
452- ),
433+ part = ToolReturnPart (tool_name = "get_weather" , content = "Sunny" , tool_call_id = "c1" ),
453434 ),
454435 # Second model response: more text.
455436 PartStartEvent (index = 0 , part = TextPart (content = "" )),
@@ -458,9 +439,7 @@ async def test_text_then_tool_then_text_uses_separate_contexts_in_order(
458439 ]
459440 final = await stream_pydantic_ai_events (_aiter (events ), TASK_ID )
460441
461- assert len (streaming .contexts ) == 2 , (
462- "One context per text part — tool calls don't open streaming contexts"
463- )
442+ assert len (streaming .contexts ) == 2 , "One context per text part — tool calls don't open streaming contexts"
464443 assert all (ctx .closed for ctx in streaming .contexts )
465444 assert _text_deltas (streaming .contexts [0 ]) == ["Looking up..." ]
466445 assert _text_deltas (streaming .contexts [1 ]) == ["It's sunny." ]
0 commit comments