11""" 
22Example demonstrating how to use the reasoning content feature with models that support it. 
33
4- Some models, like deepseek-reasoner , provide a reasoning_content field in addition to the regular content. 
4+ Some models, like gpt-5 , provide a reasoning_content field in addition to the regular content. 
55This example shows how to access and use this reasoning content from both streaming and non-streaming responses. 
66
77To run this example, you need to: 
881. Set your OPENAI_API_KEY environment variable 
9- 2. Use a model that supports reasoning content (e.g., deepseek-reasoner ) 
9+ 2. Use a model that supports reasoning content (e.g., gpt-5 ) 
1010""" 
1111
1212import  asyncio 
1313import  os 
1414from  typing  import  Any , cast 
1515
1616from  openai .types .responses  import  ResponseOutputRefusal , ResponseOutputText 
17+ from  openai .types .shared .reasoning  import  Reasoning 
1718
1819from  agents  import  ModelSettings 
1920from  agents .models .interface  import  ModelTracing 
2021from  agents .models .openai_provider  import  OpenAIProvider 
2122
22- MODEL_NAME  =  os .getenv ("EXAMPLE_MODEL_NAME" ) or  "deepseek-reasoner " 
23+ MODEL_NAME  =  os .getenv ("EXAMPLE_MODEL_NAME" ) or  "gpt-5 " 
2324
2425
2526async  def  stream_with_reasoning_content ():
@@ -36,10 +37,11 @@ async def stream_with_reasoning_content():
3637    reasoning_content  =  "" 
3738    regular_content  =  "" 
3839
40+     output_text_already_started  =  False 
3941    async  for  event  in  model .stream_response (
4042        system_instructions = "You are a helpful assistant that writes creative content." ,
4143        input = "Write a haiku about recursion in programming" ,
42-         model_settings = ModelSettings (),
44+         model_settings = ModelSettings (reasoning = Reasoning ( effort = "medium" ,  summary = "detailed" ) ),
4345        tools = [],
4446        output_schema = None ,
4547        handoffs = [],
@@ -48,18 +50,16 @@ async def stream_with_reasoning_content():
4850        prompt = None ,
4951    ):
5052        if  event .type  ==  "response.reasoning_summary_text.delta" :
51-             print (
52-                 f"\033 [33m{ event .delta } \033 [0m" , end = "" , flush = True 
53-             )  # Yellow for reasoning content 
53+             # Yellow for reasoning content 
54+             print (f"\033 [33m{ event .delta } \033 [0m" , end = "" , flush = True )
5455            reasoning_content  +=  event .delta 
5556        elif  event .type  ==  "response.output_text.delta" :
56-             print (f"\033 [32m{ event .delta } \033 [0m" , end = "" , flush = True )  # Green for regular content 
57+             if  not  output_text_already_started :
58+                 print ("\n " )
59+                 output_text_already_started  =  True 
60+             # Green for regular content 
61+             print (f"\033 [32m{ event .delta } \033 [0m" , end = "" , flush = True )
5762            regular_content  +=  event .delta 
58- 
59-     print ("\n \n Reasoning Content:" )
60-     print (reasoning_content )
61-     print ("\n Regular Content:" )
62-     print (regular_content )
6363    print ("\n " )
6464
6565
@@ -77,7 +77,7 @@ async def get_response_with_reasoning_content():
7777    response  =  await  model .get_response (
7878        system_instructions = "You are a helpful assistant that explains technical concepts clearly." ,
7979        input = "Explain the concept of recursion in programming" ,
80-         model_settings = ModelSettings (),
80+         model_settings = ModelSettings (reasoning = Reasoning ( effort = "medium" ,  summary = "detailed" ) ),
8181        tools = [],
8282        output_schema = None ,
8383        handoffs = [],
@@ -102,12 +102,10 @@ async def get_response_with_reasoning_content():
102102                    refusal_item  =  cast (Any , content_item )
103103                    regular_content  =  refusal_item .refusal 
104104
105-     print ("\n  Reasoning  Content:" )
105+     print ("\n  \n ### Reasoning  Content:" )
106106    print (reasoning_content  or  "No reasoning content provided" )
107- 
108-     print ("\n Regular Content:" )
107+     print ("\n \n ### Regular Content:" )
109108    print (regular_content  or  "No regular content provided" )
110- 
111109    print ("\n " )
112110
113111
@@ -118,7 +116,7 @@ async def main():
118116    except  Exception  as  e :
119117        print (f"Error: { e }  " )
120118        print ("\n Note: This example requires a model that supports reasoning content." )
121-         print ("You may need to use a specific model like deepseek-reasoner  or similar." )
119+         print ("You may need to use a specific model like gpt-5  or similar." )
122120
123121
124122if  __name__  ==  "__main__" :
0 commit comments