Python: Add OpenAI reasoning events support to Responses client (#698)

* feat: add OpenAI reasoning events support with comprehensive test coverage

- Implement reasoning event handling in OpenAI Responses client
  * Add support for ResponseReasoningTextDeltaEvent
  * Add support for ResponseReasoningTextDoneEvent
  * Add support for ResponseReasoningSummaryTextDeltaEvent
  * Add support for ResponseReasoningSummaryTextDoneEvent
  * Map all reasoning events to TextReasoningContent objects
  * Preserve metadata across reasoning events

- Add comprehensive test coverage (5 focused test functions)
  * test_streaming_reasoning_text_delta_event
  * test_streaming_reasoning_text_done_event
  * test_streaming_reasoning_summary_text_delta_event
  * test_streaming_reasoning_summary_text_done_event
  * test_streaming_reasoning_events_preserve_metadata

- Add sample demonstrating reasoning functionality
  * Shows how to enable reasoning in chat options
  * Demonstrates accessing reasoning content from responses

- Code quality improvements
  * Follow existing code patterns and style guidelines
  * Organize imports properly
  * Maintain backwards compatibility
  * All tests pass and quality checks succeed

* fix: resolve type errors and cleanup unused imports after rebase

- Add proper hasattr checks for optional attributes in union types
- Remove unused OpenAI event type imports
- Fix line length formatting issues
- Ensure type safety when accessing content attributes
This commit is contained in:
Christian Glessner
2025-09-12 19:07:49 +07:00
committed by GitHub
Unverified
parent d54b0f0849
commit f79fbfa92e
3 changed files with 177 additions and 16 deletions
@@ -10,12 +10,12 @@ async def reasoning_example() -> None:
"""Example of reasoning response (get results as they are generated)."""
print("=== Reasoning Example ===")
agent = OpenAIResponsesClient(ai_model_id="o4-mini").create_agent(
agent = OpenAIResponsesClient(ai_model_id="gpt-5").create_agent(
name="MathHelper",
instructions="You are a personal math tutor. When asked a math question, "
"write and run code using the python tool to answer the question.",
tools=HostedCodeInterpreterTool(),
reasoning={"effort": "medium"},
reasoning={"effort": "high", "summary": "detailed"},
)
query = "I need to solve the equation 3x + 11 = 14. Can you help me?"
@@ -27,9 +27,9 @@ async def reasoning_example() -> None:
for content in chunk.contents:
if isinstance(content, TextReasoningContent):
print(f"\033[97m{content.text}\033[0m", end="", flush=True)
if isinstance(content, TextContent):
elif isinstance(content, TextContent):
print(content.text, end="", flush=True)
if isinstance(content, UsageContent):
elif isinstance(content, UsageContent):
usage = content
print("\n")
if usage: