1+ """
2+ Example demonstrating how to use the reasoning content feature with models that support it.
3+
4+ Some models, like deepseek-reasoner, provide a reasoning_content field in addition to the regular content.
5+ This example shows how to access and use this reasoning content from both streaming and non-streaming responses.
6+
7+ To run this example, you need to:
8+ 1. Set your OPENAI_API_KEY environment variable
9+ 2. Use a model that supports reasoning content (e.g., deepseek-reasoner)
10+ """
11+
12+ import os
13+ import asyncio
14+
15+ from agents .models .openai_provider import OpenAIProvider
16+ from agents import ModelSettings
17+ from agents .items import ReasoningItem
18+
19+ # Replace this with a model that supports reasoning content (e.g., deepseek-reasoner)
20+ # For demonstration purposes, we'll use a placeholder model name
21+ MODEL_NAME = "deepseek-reasoner"
22+
23+ async def stream_with_reasoning_content ():
24+ """
25+ Example of streaming a response from a model that provides reasoning content.
26+ The reasoning content will be emitted as separate events.
27+ """
28+ provider = OpenAIProvider ()
29+ model = provider .get_model (MODEL_NAME )
30+
31+ print ("\n === Streaming Example ===" )
32+ print ("Prompt: Write a haiku about recursion in programming" )
33+
34+ reasoning_content = ""
35+ regular_content = ""
36+
37+ async for event in model .stream_response (
38+ system_instructions = "You are a helpful assistant that writes creative content." ,
39+ input = "Write a haiku about recursion in programming" ,
40+ model_settings = ModelSettings (),
41+ tools = [],
42+ output_schema = None ,
43+ handoffs = [],
44+ tracing = None ,
45+ previous_response_id = None ,
46+ ):
47+ if event .type == "response.reasoning_summary_text.delta" :
48+ print (f"\033 [33m{ event .delta } \033 [0m" , end = "" , flush = True ) # Yellow for reasoning content
49+ reasoning_content += event .delta
50+ elif event .type == "response.output_text.delta" :
51+ print (f"\033 [32m{ event .delta } \033 [0m" , end = "" , flush = True ) # Green for regular content
52+ regular_content += event .delta
53+
54+ print ("\n \n Reasoning Content:" )
55+ print (reasoning_content )
56+ print ("\n Regular Content:" )
57+ print (regular_content )
58+ print ("\n " )
59+
60+ async def get_response_with_reasoning_content ():
61+ """
62+ Example of getting a complete response from a model that provides reasoning content.
63+ The reasoning content will be available as a separate item in the response.
64+ """
65+ provider = OpenAIProvider ()
66+ model = provider .get_model (MODEL_NAME )
67+
68+ print ("\n === Non-streaming Example ===" )
69+ print ("Prompt: Explain the concept of recursion in programming" )
70+
71+ response = await model .get_response (
72+ system_instructions = "You are a helpful assistant that explains technical concepts clearly." ,
73+ input = "Explain the concept of recursion in programming" ,
74+ model_settings = ModelSettings (),
75+ tools = [],
76+ output_schema = None ,
77+ handoffs = [],
78+ tracing = None ,
79+ previous_response_id = None ,
80+ )
81+
82+ # Extract reasoning content and regular content from the response
83+ reasoning_content = None
84+ regular_content = None
85+
86+ for item in response .output :
87+ if hasattr (item , "type" ) and item .type == "reasoning_item" :
88+ reasoning_content = item .content
89+ elif hasattr (item , "type" ) and item .type == "message" :
90+ if item .content and len (item .content ) > 0 :
91+ regular_content = item .content [0 ].text
92+
93+ print ("\n Reasoning Content:" )
94+ print (reasoning_content or "No reasoning content provided" )
95+
96+ print ("\n Regular Content:" )
97+ print (regular_content or "No regular content provided" )
98+
99+ print ("\n " )
100+
101+ async def main ():
102+ try :
103+ await stream_with_reasoning_content ()
104+ await get_response_with_reasoning_content ()
105+ except Exception as e :
106+ print (f"Error: { e } " )
107+ print ("\n Note: This example requires a model that supports reasoning content." )
108+ print ("You may need to use a specific model like deepseek-reasoner or similar." )
109+
110+ if __name__ == "__main__" :
111+ asyncio .run (main ())
0 commit comments