Skip to content

Support for Streaming Events from Long-Running Tools #1333

Closed

Description

@noe-starlight

Add support in the OpenAI Agents SDK for tools that:
• Run long-running operations (e.g. background tasks, indexing, data processing)
• Connect to WebSockets or other asynchronous event sources
And allow those tools to stream intermediate updates or status events back to the agent runtime via custom event channels.

Motivation:

In real-world agent applications, tools often perform actions that span longer than a few seconds or need to respond to external events (e.g., via WebSocket, SSE, or polling). The current SDK does not allow for easily streaming intermediate updates from tools. Developers are forced to block until a final result or create workarounds outside the agent runtime.

This limits the user experience and utility for:
• Background processing tools (e.g. book indexing, media analysis)
• Real-time systems (e.g. stock price streams, WebSocket bots)
• Long API workflows (e.g. transcription, document conversion)

Proposed API Behavior:

Allow tools to yield structured event objects (e.g., tool_stream_event, tool_background_task) that the agent runtime can surface to the user (through a developer-defined channel such as SSE or a UI event handler).

Example tool yield behavior:

yield {"type": "tool_background_task", "tool_name": "book_indexer", "message": "Indexing book contents started"}

for i in range(10):
    yield {"type": "tool_stream_event", "tool_name": "book_indexer", "data": f"Indexing page {i+1} of 10"}

Developer stream output handler (e.g. in Flask):

if tool_event.get("type") == "tool_stream_event":
    yield f'data: {json.dumps({"event": "tool_update", "tool_name": tool_event["tool_name"], "detail": tool_event["data"]})}\n\n'
elif tool_event.get("type") == "tool_background_task":
    yield f'data: {json.dumps({"event": "tool_background_task_started", "tool_name": tool_event["tool_name"], "detail": tool_event["message"]})}\n\n'

Expected output

event: tool_background_task_started
tool_name: book_indexer
detail: "Indexing book contents"

event: tool_update
tool_name: book_indexer
detail: "Indexing page 1 of 10"

event: tool_update
tool_name: book_indexer
detail: "Indexing page 2 of 10"

Benefits:
• Enhances interactivity of tools without needing to return blocking responses
• Improves UX in agent-hosted environments with real-time feedback
• Enables developers to build more robust and scalable agents with observable internal progress

Activity

  1. added
    enhancement
    on Aug 1, 2025
  2. seratch commented on Aug 1, 2025

    @seratch
    Member

    Thanks for sharing this. While OpenAI platform released background mode and webhooks for Responses API, this SDK does not yet natively support the functionality. We cannot tell whether and when something would be added to this SDK, but I agree some enhancement would be useful for the use case.

  3. noe-starlight commented on Aug 15, 2025

    @noe-starlight
    Author

    Thanks for sharing this. While OpenAI platform released background mode and webhooks for Responses API, this SDK does not yet natively support the functionality. We cannot tell whether and when something would be added to this SDK, but I agree some enhancement would be useful for the use case.

    HI, while I know that you guys have many things on the roadmap and there's not an actual timebox to add this functionality, I would love to know if there's a workaround for this. thanks in advance.

  4. RussellLuo commented on Oct 16, 2025

    @RussellLuo

    Hi guys, I have managed to write an extension library openai-agents-ext that supports stream events for tools, without intrusive modifications to the OpenAI Agents SDK.

    Here is a basic example:

    import asyncio
    
    from agents import (
        function_tool,
        RawResponsesStreamEvent,
        RunItemStreamEvent,
    )
    from agents.tool_context import ToolContext
    from agents_ext import Agent, EventQueue, StreamToolContext, run_streamed
    from openai.types.responses.response_text_delta_event import ResponseTextDeltaEvent
    
    
    @function_tool
    async def query_weather(ctx: ToolContext[EventQueue], location: str):
        sctx = StreamToolContext(ctx)
    
        try:
            sctx.put_reasoning_item("Starting...")
            return f"Weather in {location} is sunny."
        finally:
            sctx.put_reasoning_item("Completed!")
    
    
    agent = Agent(
        name="weather_agent",
        instructions="You are a helpful weather reporter.",
        tools=[query_weather],
    )
    
    
    async def main():
        result = run_streamed(
            agent, "What's the weather like in Beijing?", context=EventQueue()
        )
        async for event in result.stream_events():
            if isinstance(event, RawResponsesStreamEvent):
                if isinstance(event.data, ResponseTextDeltaEvent):
                    print(event.data.delta, end="", flush=True)
            elif isinstance(event, RunItemStreamEvent):
                if event.item.type == "tool_call_item":
                    print(f"\n['{event.item.raw_item.name}' tool called]", flush=True)
                elif event.item.type == "tool_call_output_item":
                    print(f"\n[tool output: {event.item.output}]", flush=True)
                elif event.item.type == "reasoning_item":
                    print(
                        f"\n['{event.item.raw_item.id}' reasoning: {event.item.raw_item.summary[0].text}]",
                        flush=True,
                    )
    
        # I'll check the weather in Beijing for you.
        # ['query_weather' tool called]
        #
        # ['query_weather' reasoning: Starting...]
        #
        # ['query_weather' reasoning: Completed!]
        #
        # [tool output: Weather in Beijing is sunny.]
        # The weather in Beijing is currently sunny!
    
    
    if __name__ == "__main__":
        asyncio.run(main())

    If there are any features in openai-agents-ext that the current SDK would like to support, I’d be happy to help!

  5. seratch commented on May 20, 2026

    @seratch
    Member

    For long-running tasks, we now encourage using sandbox agents and their future enhancements. Please try the new feature out and let us know if there is any feedback on it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementfeature:core

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions