-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Add streaming function call arguments example #1052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
seratch
merged 3 commits into
openai:main
from
devtalker:feature/stream-function-call-args
Aug 11, 2025
+86
−0
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import asyncio | ||
from typing import Any | ||
|
||
from openai.types.responses import ResponseFunctionCallArgumentsDeltaEvent | ||
|
||
from agents import Agent, Runner, function_tool | ||
|
||
|
||
@function_tool | ||
def write_file(filename: str, content: str) -> str: | ||
"""Write content to a file.""" | ||
return f"File {filename} written successfully" | ||
|
||
|
||
@function_tool | ||
def create_config(project_name: str, version: str, dependencies: list[str]) -> str: | ||
"""Create a configuration file for a project.""" | ||
return f"Config for {project_name} v{version} created" | ||
|
||
|
||
async def main(): | ||
""" | ||
Demonstrates real-time streaming of function call arguments. | ||
|
||
Function arguments are streamed incrementally as they are generated, | ||
providing immediate feedback during parameter generation. | ||
""" | ||
agent = Agent( | ||
name="CodeGenerator", | ||
instructions="You are a helpful coding assistant. Use the provided tools to create files and configurations.", | ||
tools=[write_file, create_config], | ||
) | ||
|
||
print("🚀 Function Call Arguments Streaming Demo") | ||
|
||
result = Runner.run_streamed( | ||
agent, | ||
input="Create a Python web project called 'my-app' with FastAPI. Version 1.0.0, dependencies: fastapi, uvicorn" | ||
) | ||
|
||
# Track function calls for detailed output | ||
function_calls: dict[Any, dict[str, Any]] = {} # call_id -> {name, arguments} | ||
current_active_call_id = None | ||
|
||
async for event in result.stream_events(): | ||
if event.type == "raw_response_event": | ||
# Function call started | ||
if event.data.type == "response.output_item.added": | ||
if getattr(event.data.item, "type", None) == "function_call": | ||
function_name = getattr(event.data.item, "name", "unknown") | ||
call_id = getattr(event.data.item, "call_id", "unknown") | ||
|
||
function_calls[call_id] = { | ||
'name': function_name, | ||
'arguments': "" | ||
} | ||
current_active_call_id = call_id | ||
print(f"\n📞 Function call streaming started: {function_name}()") | ||
print("📝 Arguments building...") | ||
|
||
# Real-time argument streaming | ||
elif isinstance(event.data, ResponseFunctionCallArgumentsDeltaEvent): | ||
if current_active_call_id and current_active_call_id in function_calls: | ||
function_calls[current_active_call_id]['arguments'] += event.data.delta | ||
print(f" + {event.data.delta}", end="", flush=True) | ||
|
||
# Function call completed | ||
elif event.data.type == "response.output_item.done": | ||
if hasattr(event.data.item, 'call_id'): | ||
call_id = getattr(event.data.item, "call_id", "unknown") | ||
if call_id in function_calls: | ||
function_info = function_calls[call_id] | ||
print(f"\n✅ Function call streaming completed: {function_info['name']}") | ||
print() | ||
if current_active_call_id == call_id: | ||
current_active_call_id = None | ||
|
||
print("Summary of all function calls:") | ||
for call_id, info in function_calls.items(): | ||
print(f" - #{call_id}: {info['name']}({info['arguments']})") | ||
|
||
print(f"\nResult: {result.final_output}") | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
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.
Uh oh!
There was an error while loading. Please reload this page.