Skip to content

Commit d1b06ad

Browse files
committed
Close reasoning when the item is done, not part
1 parent 98cd7bc commit d1b06ad

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

src/agentex/lib/core/temporal/plugins/openai_agents/models/temporal_streaming_model.py

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -689,9 +689,34 @@ async def get_response(
689689
output_index = getattr(event, 'output_index', 0)
690690

691691
if item and getattr(item, 'type', None) == 'reasoning':
692-
logger.debug(f"[TemporalStreamingModel] Reasoning item completed")
693-
# Don't close the context here - let it stay open for more reasoning events
694-
# It will be closed when we send the final update or at the end
692+
if reasoning_context and reasoning_summaries:
693+
logger.debug(f"[TemporalStreamingModel] Reasoning itme completed, sending final update")
694+
try:
695+
# Send a full message update with the complete reasoning content
696+
complete_reasoning_content = ReasoningContent(
697+
author="agent",
698+
summary=reasoning_summaries, # Use accumulated summaries
699+
content=reasoning_contents if reasoning_contents else [],
700+
type="reasoning",
701+
style="static",
702+
)
703+
704+
await reasoning_context.stream_update(
705+
update=StreamTaskMessageFull(
706+
parent_task_message=reasoning_context.task_message,
707+
content=complete_reasoning_content,
708+
type="full",
709+
),
710+
)
711+
712+
# Close the reasoning context after sending the final update
713+
# This matches the reference implementation pattern
714+
await reasoning_context.close()
715+
reasoning_context = None
716+
logger.debug(f"[TemporalStreamingModel] Closed reasoning context after final update")
717+
except Exception as e:
718+
logger.warning(f"Failed to send reasoning part done update: {e}")
719+
695720
elif item and getattr(item, 'type', None) == 'function_call':
696721
# Function call completed - add to output
697722
if output_index in function_calls_in_progress:
@@ -718,34 +743,8 @@ async def get_response(
718743
current_reasoning_summary = ""
719744

720745
elif isinstance(event, ResponseReasoningSummaryPartDoneEvent):
721-
# Reasoning part completed - send final update and close if this is the last part
722-
if reasoning_context and reasoning_summaries:
723-
logger.debug(f"[TemporalStreamingModel] Reasoning part completed, sending final update")
724-
try:
725-
# Send a full message update with the complete reasoning content
726-
complete_reasoning_content = ReasoningContent(
727-
author="agent",
728-
summary=reasoning_summaries, # Use accumulated summaries
729-
content=reasoning_contents if reasoning_contents else [],
730-
type="reasoning",
731-
style="static",
732-
)
733-
734-
await reasoning_context.stream_update(
735-
update=StreamTaskMessageFull(
736-
parent_task_message=reasoning_context.task_message,
737-
content=complete_reasoning_content,
738-
type="full",
739-
),
740-
)
741-
742-
# Close the reasoning context after sending the final update
743-
# This matches the reference implementation pattern
744-
await reasoning_context.close()
745-
reasoning_context = None
746-
logger.debug(f"[TemporalStreamingModel] Closed reasoning context after final update")
747-
except Exception as e:
748-
logger.warning(f"Failed to send reasoning part done update: {e}")
746+
# Reasoning part completed - ResponseOutputItemDoneEvent will handle the final update
747+
logger.debug(f"[TemporalStreamingModel] Reasoning part completed")
749748

750749
elif isinstance(event, ResponseCompletedEvent):
751750
# Response completed

0 commit comments

Comments
 (0)