1313
1414# Regular Task
1515async def stream_regular_task () -> None :
16- regular_task = await client .tasks .create (
16+ task = await client .tasks .create_task (
1717 task = """
1818 Find top 10 Hacker News articles and return the title and url.
1919 """ ,
2020 llm = "gemini-2.5-flash" ,
2121 )
2222
23- print (f"Regular Task ID: { regular_task .id } " )
23+ print (f"Regular Task ID: { task .id } " )
2424
25- async for res in client .tasks .stream (regular_task .id ):
26- print (f"Regular Task Status: { res .status } " )
27-
28- if len (res .steps ) > 0 :
29- last_step = res .steps [- 1 ]
30- print (f"Regular Task Step: { last_step .url } ({ last_step .next_goal } )" )
31- for action in last_step .actions :
32- print (f" - Regular Task Action: { action } " )
25+ async for step in task .stream ():
26+ print (f"Regular Task Status: { step .number } " )
3327
3428 print ("Regular Task Done" )
3529
@@ -43,26 +37,24 @@ class HackerNewsPost(BaseModel):
4337 class SearchResult (BaseModel ):
4438 posts : List [HackerNewsPost ]
4539
46- structured_task = await client .tasks .create (
40+ task = await client .tasks .create_task (
4741 task = """
4842 Find top 10 Hacker News articles and return the title and url.
4943 """ ,
5044 llm = "gpt-4.1" ,
51- structured_output_json = SearchResult ,
45+ schema = SearchResult ,
5246 )
5347
54- print (f"Structured Task ID: { structured_task .id } " )
48+ print (f"Structured Task ID: { task .id } " )
49+
50+ async for step in task .stream ():
51+ print (f"Structured Task Step { step .number } : { step .url } ({ step .next_goal } )" )
5552
56- async for res in client .tasks .stream (structured_task .id , structured_output_json = SearchResult ):
57- print (f"Structured Task Status: { res .status } " )
53+ result = await task .complete ()
5854
59- if res .status == "finished" :
60- if res .parsed_output is None :
61- print ("Structured Task No output" )
62- else :
63- for post in res .parsed_output .posts :
64- print (f" - Structured Task Post: { post .title } - { post .url } " )
65- break
55+ if result .parsed_output is not None :
56+ for post in result .parsed_output .posts :
57+ print (f" - { post .title } - { post .url } " )
6658
6759 print ("Structured Task Done" )
6860
0 commit comments