feat: Add streaming tool output support #1964
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds support for streaming tool output in the OpenAI Agents SDK. Previously, users could only receive tool results after complete execution. Now, tools can yield incremental output during execution, enabling real-time progress feedback for long-running operations.
Key improvements:
ToolOutputStreamEventfor receiving incremental tool outputAsyncIterator[str])Runner.run_streamed()andRunner.run()modesType system fix:
Fixed the type signature of
FunctionTool.on_invoke_toolfrom:to:
This properly represents that the function always returns an awaitable, which then yields either a regular value or an async iterator.
Test plan
New tests (
tests/test_tool_streaming.py):test_basic_streaming_tool- Verifies basic streaming functionalitytest_streaming_tool_with_arguments- Tests parameter handlingtest_mixed_streaming_and_non_streaming_tools- Validates coexistencetest_streaming_tool_accumulation- Confirms proper output accumulationtest_streaming_tool_in_non_streaming_mode- TestsRunner.run()modetest_streaming_tool_agent_association- Checks event metadataExample (
examples/tools/streaming_tool_example.py):Validation:
Issue number
N/A - New feature enhancement
Checks
make lintandmake format(all passed)Usage Example
Documentation
Updated streaming documentation in 4 languages:
docs/streaming.mddocs/zh/streaming.mddocs/ja/streaming.mddocs/ko/streaming.mdAll include complete examples and key points about streaming tools.