-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add a quick opt-in option to switch to gpt-5 model #1534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,10 @@ async def main(verbose: bool, stream: bool): | |
print(f"Got event of type {event.item.__class__.__name__}") | ||
print(f"Done streaming; final result: {result.final_output}") | ||
else: | ||
res = await Runner.run(agent, "Which language is this repo written in?") | ||
res = await Runner.run( | ||
agent, | ||
"Which language is this repo written in? Your MCP server should know what the repo is.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. gpt-5 models could be confused with this MCP server's rule (= repo information is available in the MCP server URL), so I updated the instructions to be clearer |
||
) | ||
print(res.final_output) | ||
|
||
if verbose: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,26 @@ | ||
""" | ||
Example demonstrating how to use the reasoning content feature with models that support it. | ||
|
||
Some models, like deepseek-reasoner, provide a reasoning_content field in addition to the regular content. | ||
Some models, like gpt-5, provide a reasoning_content field in addition to the regular content. | ||
This example shows how to access and use this reasoning content from both streaming and non-streaming responses. | ||
|
||
To run this example, you need to: | ||
1. Set your OPENAI_API_KEY environment variable | ||
2. Use a model that supports reasoning content (e.g., deepseek-reasoner) | ||
2. Use a model that supports reasoning content (e.g., gpt-5) | ||
""" | ||
|
||
import asyncio | ||
import os | ||
from typing import Any, cast | ||
|
||
from openai.types.responses import ResponseOutputRefusal, ResponseOutputText | ||
from openai.types.shared.reasoning import Reasoning | ||
|
||
from agents import ModelSettings | ||
from agents.models.interface import ModelTracing | ||
from agents.models.openai_provider import OpenAIProvider | ||
|
||
MODEL_NAME = os.getenv("EXAMPLE_MODEL_NAME") or "deepseek-reasoner" | ||
MODEL_NAME = os.getenv("EXAMPLE_MODEL_NAME") or "gpt-5" | ||
|
||
|
||
async def stream_with_reasoning_content(): | ||
|
@@ -36,10 +37,11 @@ async def stream_with_reasoning_content(): | |
reasoning_content = "" | ||
regular_content = "" | ||
|
||
output_text_already_started = False | ||
async for event in model.stream_response( | ||
system_instructions="You are a helpful assistant that writes creative content.", | ||
input="Write a haiku about recursion in programming", | ||
model_settings=ModelSettings(), | ||
model_settings=ModelSettings(reasoning=Reasoning(effort="medium", summary="detailed")), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without these settings, reasoning summary items won't show up with OpenAI models; so I've updated this example. |
||
tools=[], | ||
output_schema=None, | ||
handoffs=[], | ||
|
@@ -48,18 +50,16 @@ async def stream_with_reasoning_content(): | |
prompt=None, | ||
): | ||
if event.type == "response.reasoning_summary_text.delta": | ||
print( | ||
f"\033[33m{event.delta}\033[0m", end="", flush=True | ||
) # Yellow for reasoning content | ||
# Yellow for reasoning content | ||
print(f"\033[33m{event.delta}\033[0m", end="", flush=True) | ||
reasoning_content += event.delta | ||
elif event.type == "response.output_text.delta": | ||
print(f"\033[32m{event.delta}\033[0m", end="", flush=True) # Green for regular content | ||
if not output_text_already_started: | ||
print("\n") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improved the readability of the output |
||
output_text_already_started = True | ||
# Green for regular content | ||
print(f"\033[32m{event.delta}\033[0m", end="", flush=True) | ||
regular_content += event.delta | ||
|
||
print("\n\nReasoning Content:") | ||
print(reasoning_content) | ||
print("\nRegular Content:") | ||
print(regular_content) | ||
print("\n") | ||
|
||
|
||
|
@@ -77,7 +77,7 @@ async def get_response_with_reasoning_content(): | |
response = await model.get_response( | ||
system_instructions="You are a helpful assistant that explains technical concepts clearly.", | ||
input="Explain the concept of recursion in programming", | ||
model_settings=ModelSettings(), | ||
model_settings=ModelSettings(reasoning=Reasoning(effort="medium", summary="detailed")), | ||
tools=[], | ||
output_schema=None, | ||
handoffs=[], | ||
|
@@ -102,12 +102,10 @@ async def get_response_with_reasoning_content(): | |
refusal_item = cast(Any, content_item) | ||
regular_content = refusal_item.refusal | ||
|
||
print("\nReasoning Content:") | ||
print("\n\n### Reasoning Content:") | ||
print(reasoning_content or "No reasoning content provided") | ||
|
||
print("\nRegular Content:") | ||
print("\n\n### Regular Content:") | ||
print(regular_content or "No regular content provided") | ||
|
||
print("\n") | ||
|
||
|
||
|
@@ -118,7 +116,7 @@ async def main(): | |
except Exception as e: | ||
print(f"Error: {e}") | ||
print("\nNote: This example requires a model that supports reasoning content.") | ||
print("You may need to use a specific model like deepseek-reasoner or similar.") | ||
print("You may need to use a specific model like gpt-5 or similar.") | ||
|
||
|
||
if __name__ == "__main__": | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,17 +6,18 @@ | |
|
||
To run this example, you need to: | ||
1. Set your OPENAI_API_KEY environment variable | ||
2. Use a model that supports reasoning content (e.g., deepseek-reasoner) | ||
2. Use a model that supports reasoning content (e.g., gpt-5) | ||
""" | ||
|
||
import asyncio | ||
import os | ||
from typing import Any | ||
|
||
from agents import Agent, Runner, trace | ||
from openai.types.shared.reasoning import Reasoning | ||
|
||
from agents import Agent, ModelSettings, Runner, trace | ||
from agents.items import ReasoningItem | ||
|
||
MODEL_NAME = os.getenv("EXAMPLE_MODEL_NAME") or "deepseek-reasoner" | ||
MODEL_NAME = os.getenv("EXAMPLE_MODEL_NAME") or "gpt-5" | ||
|
||
|
||
async def main(): | ||
|
@@ -27,6 +28,7 @@ async def main(): | |
name="Reasoning Agent", | ||
instructions="You are a helpful assistant that explains your reasoning step by step.", | ||
model=MODEL_NAME, | ||
model_settings=ModelSettings(reasoning=Reasoning(effort="medium", summary="detailed")), | ||
) | ||
|
||
# Example 1: Non-streaming response | ||
|
@@ -35,53 +37,34 @@ async def main(): | |
result = await Runner.run( | ||
agent, "What is the square root of 841? Please explain your reasoning." | ||
) | ||
|
||
# Extract reasoning content from the result items | ||
reasoning_content = None | ||
# RunResult has 'response' attribute which has 'output' attribute | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not work with OpenAI models, so I revised this example |
||
for item in result.response.output: # type: ignore | ||
if isinstance(item, ReasoningItem): | ||
reasoning_content = item.summary[0].text # type: ignore | ||
for item in result.new_items: | ||
if isinstance(item, ReasoningItem) and len(item.raw_item.summary) > 0: | ||
reasoning_content = item.raw_item.summary[0].text | ||
break | ||
|
||
print("\nReasoning Content:") | ||
print("\n### Reasoning Content:") | ||
print(reasoning_content or "No reasoning content provided") | ||
|
||
print("\nFinal Output:") | ||
print("\n### Final Output:") | ||
print(result.final_output) | ||
|
||
# Example 2: Streaming response | ||
with trace("Reasoning Content - Streaming"): | ||
print("\n=== Example 2: Streaming response ===") | ||
print("\nStreaming response:") | ||
|
||
# Buffers to collect reasoning and regular content | ||
reasoning_buffer = "" | ||
content_buffer = "" | ||
|
||
# RunResultStreaming is async iterable | ||
stream = Runner.run_streamed(agent, "What is 15 x 27? Please explain your reasoning.") | ||
|
||
async for event in stream: # type: ignore | ||
if isinstance(event, ReasoningItem): | ||
# This is reasoning content | ||
reasoning_item: Any = event | ||
reasoning_buffer += reasoning_item.summary[0].text | ||
print( | ||
f"\033[33m{reasoning_item.summary[0].text}\033[0m", end="", flush=True | ||
) # Yellow for reasoning | ||
elif hasattr(event, "text"): | ||
# This is regular content | ||
content_buffer += event.text | ||
print( | ||
f"\033[32m{event.text}\033[0m", end="", flush=True | ||
) # Green for regular content | ||
|
||
print("\n\nCollected Reasoning Content:") | ||
print(reasoning_buffer) | ||
|
||
print("\nCollected Final Answer:") | ||
print(content_buffer) | ||
output_text_already_started = False | ||
async for event in stream.stream_events(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
if event.type == "raw_response_event": | ||
if event.data.type == "response.reasoning_summary_text.delta": | ||
print(f"\033[33m{event.data.delta}\033[0m", end="", flush=True) | ||
elif event.data.type == "response.output_text.delta": | ||
if not output_text_already_started: | ||
print("\n") | ||
output_text_already_started = True | ||
print(f"\033[32m{event.data.delta}\033[0m", end="", flush=True) | ||
|
||
print("\n") | ||
|
||
|
||
if __name__ == "__main__": | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this option is not compatible with gpt-5, so explicitly set gpt-4.1 for this example