1
1
"""
2
2
Example demonstrating how to use the reasoning content feature with models that support it.
3
3
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.
5
5
This example shows how to access and use this reasoning content from both streaming and non-streaming responses.
6
6
7
7
To run this example, you need to:
8
8
1. 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 )
10
10
"""
11
11
12
12
import asyncio
13
13
import os
14
14
from typing import Any , cast
15
15
16
16
from openai .types .responses import ResponseOutputRefusal , ResponseOutputText
17
+ from openai .types .shared .reasoning import Reasoning
17
18
18
19
from agents import ModelSettings
19
20
from agents .models .interface import ModelTracing
20
21
from agents .models .openai_provider import OpenAIProvider
21
22
22
- MODEL_NAME = os .getenv ("EXAMPLE_MODEL_NAME" ) or "deepseek-reasoner "
23
+ MODEL_NAME = os .getenv ("EXAMPLE_MODEL_NAME" ) or "gpt-5 "
23
24
24
25
25
26
async def stream_with_reasoning_content ():
@@ -36,10 +37,11 @@ async def stream_with_reasoning_content():
36
37
reasoning_content = ""
37
38
regular_content = ""
38
39
40
+ output_text_already_started = False
39
41
async for event in model .stream_response (
40
42
system_instructions = "You are a helpful assistant that writes creative content." ,
41
43
input = "Write a haiku about recursion in programming" ,
42
- model_settings = ModelSettings (),
44
+ model_settings = ModelSettings (reasoning = Reasoning ( effort = "medium" , summary = "detailed" ) ),
43
45
tools = [],
44
46
output_schema = None ,
45
47
handoffs = [],
@@ -48,18 +50,16 @@ async def stream_with_reasoning_content():
48
50
prompt = None ,
49
51
):
50
52
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 )
54
55
reasoning_content += event .delta
55
56
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 )
57
62
regular_content += event .delta
58
-
59
- print ("\n \n Reasoning Content:" )
60
- print (reasoning_content )
61
- print ("\n Regular Content:" )
62
- print (regular_content )
63
63
print ("\n " )
64
64
65
65
@@ -77,7 +77,7 @@ async def get_response_with_reasoning_content():
77
77
response = await model .get_response (
78
78
system_instructions = "You are a helpful assistant that explains technical concepts clearly." ,
79
79
input = "Explain the concept of recursion in programming" ,
80
- model_settings = ModelSettings (),
80
+ model_settings = ModelSettings (reasoning = Reasoning ( effort = "medium" , summary = "detailed" ) ),
81
81
tools = [],
82
82
output_schema = None ,
83
83
handoffs = [],
@@ -102,12 +102,10 @@ async def get_response_with_reasoning_content():
102
102
refusal_item = cast (Any , content_item )
103
103
regular_content = refusal_item .refusal
104
104
105
- print ("\n Reasoning Content:" )
105
+ print ("\n \n ### Reasoning Content:" )
106
106
print (reasoning_content or "No reasoning content provided" )
107
-
108
- print ("\n Regular Content:" )
107
+ print ("\n \n ### Regular Content:" )
109
108
print (regular_content or "No regular content provided" )
110
-
111
109
print ("\n " )
112
110
113
111
@@ -118,7 +116,7 @@ async def main():
118
116
except Exception as e :
119
117
print (f"Error: { e } " )
120
118
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." )
122
120
123
121
124
122
if __name__ == "__main__" :
0 commit comments