mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
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:
committed by
GitHub
Unverified
parent
d54b0f0849
commit
f79fbfa92e
@@ -762,10 +762,10 @@ class OpenAIBaseResponsesClient(OpenAIBase, BaseChatClient):
|
||||
TextContent(text=message_content.refusal, raw_representation=message_content)
|
||||
)
|
||||
case "reasoning": # ResponseOutputReasoning
|
||||
if item.content:
|
||||
if hasattr(item, "content") and item.content:
|
||||
for index, reasoning_content in enumerate(item.content):
|
||||
additional_properties = None
|
||||
if item.summary and index < len(item.summary):
|
||||
if hasattr(item, "summary") and item.summary and index < len(item.summary):
|
||||
additional_properties = {"summary": item.summary[index]}
|
||||
contents.append(
|
||||
TextReasoningContent(
|
||||
@@ -775,7 +775,7 @@ class OpenAIBaseResponsesClient(OpenAIBase, BaseChatClient):
|
||||
)
|
||||
)
|
||||
case "code_interpreter_call": # ResponseOutputCodeInterpreterCall
|
||||
if item.outputs:
|
||||
if hasattr(item, "outputs") and item.outputs:
|
||||
for code_output in item.outputs:
|
||||
if code_output.type == "logs":
|
||||
contents.append(TextContent(text=code_output.logs, raw_representation=item))
|
||||
@@ -788,16 +788,16 @@ class OpenAIBaseResponsesClient(OpenAIBase, BaseChatClient):
|
||||
media_type="image",
|
||||
)
|
||||
)
|
||||
elif item.code:
|
||||
elif hasattr(item, "code") and item.code:
|
||||
# fallback if no output was returned is the code:
|
||||
contents.append(TextContent(text=item.code, raw_representation=item))
|
||||
case "function_call": # ResponseOutputFunctionCall
|
||||
contents.append(
|
||||
FunctionCallContent(
|
||||
call_id=item.call_id if item.call_id else "",
|
||||
name=item.name,
|
||||
arguments=item.arguments,
|
||||
additional_properties={"fc_id": item.id},
|
||||
call_id=item.call_id if hasattr(item, "call_id") and item.call_id else "",
|
||||
name=item.name if hasattr(item, "name") else "",
|
||||
arguments=item.arguments if hasattr(item, "arguments") else "",
|
||||
additional_properties={"fc_id": item.id} if hasattr(item, "id") else {},
|
||||
raw_representation=item,
|
||||
)
|
||||
)
|
||||
@@ -922,6 +922,18 @@ class OpenAIBaseResponsesClient(OpenAIBase, BaseChatClient):
|
||||
case "response.output_text.delta":
|
||||
contents.append(TextContent(text=event.delta, raw_representation=event))
|
||||
metadata.update(self._get_metadata_from_response(event))
|
||||
case "response.reasoning_text.delta":
|
||||
contents.append(TextReasoningContent(text=event.delta, raw_representation=event))
|
||||
metadata.update(self._get_metadata_from_response(event))
|
||||
case "response.reasoning_text.done":
|
||||
contents.append(TextReasoningContent(text=event.text, raw_representation=event))
|
||||
metadata.update(self._get_metadata_from_response(event))
|
||||
case "response.reasoning_summary_text.delta":
|
||||
contents.append(TextReasoningContent(text=event.delta, raw_representation=event))
|
||||
metadata.update(self._get_metadata_from_response(event))
|
||||
case "response.reasoning_summary_text.done":
|
||||
contents.append(TextReasoningContent(text=event.text, raw_representation=event))
|
||||
metadata.update(self._get_metadata_from_response(event))
|
||||
case "response.completed":
|
||||
conversation_id = event.response.id if chat_options.store is True else None
|
||||
model = event.response.model
|
||||
@@ -962,7 +974,7 @@ class OpenAIBaseResponsesClient(OpenAIBase, BaseChatClient):
|
||||
)
|
||||
)
|
||||
case "code_interpreter_call": # ResponseOutputCodeInterpreterCall
|
||||
if event_item.outputs:
|
||||
if hasattr(event_item, "outputs") and event_item.outputs:
|
||||
for code_output in event_item.outputs:
|
||||
if code_output.type == "logs":
|
||||
contents.append(TextContent(text=code_output.logs, raw_representation=event_item))
|
||||
@@ -975,14 +987,18 @@ class OpenAIBaseResponsesClient(OpenAIBase, BaseChatClient):
|
||||
media_type="image",
|
||||
)
|
||||
)
|
||||
elif event_item.code:
|
||||
elif hasattr(event_item, "code") and event_item.code:
|
||||
# fallback if no output was returned is the code:
|
||||
contents.append(TextContent(text=event_item.code, raw_representation=event_item))
|
||||
case "reasoning": # ResponseOutputReasoning
|
||||
if event_item.content:
|
||||
if hasattr(event_item, "content") and event_item.content:
|
||||
for index, reasoning_content in enumerate(event_item.content):
|
||||
additional_properties = None
|
||||
if event_item.summary and index < len(event_item.summary):
|
||||
if (
|
||||
hasattr(event_item, "summary")
|
||||
and event_item.summary
|
||||
and index < len(event_item.summary)
|
||||
):
|
||||
additional_properties = {"summary": event_item.summary[index]}
|
||||
contents.append(
|
||||
TextReasoningContent(
|
||||
|
||||
Reference in New Issue
Block a user