|
| 1 | +""" |
| 2 | +Example of using streaming tools with the Agents SDK. |
| 3 | +
|
| 4 | +This example demonstrates how to create a tool that yields incremental output, |
| 5 | +allowing you to stream tool execution results to the user in real-time. |
| 6 | +""" |
| 7 | + |
| 8 | +import asyncio |
| 9 | +from collections.abc import AsyncIterator |
| 10 | + |
| 11 | +from agents import Agent, Runner, ToolOutputStreamEvent, function_tool |
| 12 | + |
| 13 | + |
| 14 | +@function_tool |
| 15 | +async def search_documents(query: str) -> AsyncIterator[str]: |
| 16 | + """Search through documents and stream results as they are found. |
| 17 | +
|
| 18 | + Args: |
| 19 | + query: The search query. |
| 20 | +
|
| 21 | + Yields: |
| 22 | + Incremental search results. |
| 23 | + """ |
| 24 | + # Simulate searching through multiple documents |
| 25 | + documents = [ |
| 26 | + f"Document 1 contains information about {query}...\n", |
| 27 | + f"Document 2 has additional details on {query}...\n", |
| 28 | + f"Document 3 provides analysis of {query}...\n", |
| 29 | + ] |
| 30 | + |
| 31 | + for doc in documents: |
| 32 | + # Simulate processing time |
| 33 | + await asyncio.sleep(0.5) |
| 34 | + # Yield incremental results |
| 35 | + yield doc |
| 36 | + |
| 37 | + |
| 38 | +@function_tool |
| 39 | +async def generate_report(topic: str) -> AsyncIterator[str]: |
| 40 | + """Generate a report on a topic, streaming the output as it's generated. |
| 41 | +
|
| 42 | + Args: |
| 43 | + topic: The topic to generate a report on. |
| 44 | +
|
| 45 | + Yields: |
| 46 | + Incremental report content. |
| 47 | + """ |
| 48 | + sections = [ |
| 49 | + f"# Report on {topic}\n\n", |
| 50 | + f"## Introduction\n\nThis report covers {topic} in detail.\n\n", |
| 51 | + f"## Analysis\n\nOur analysis of {topic} shows several key points...\n\n", |
| 52 | + f"## Conclusion\n\nIn summary, {topic} is an important topic.\n\n", |
| 53 | + ] |
| 54 | + |
| 55 | + for section in sections: |
| 56 | + await asyncio.sleep(0.3) |
| 57 | + yield section |
| 58 | + |
| 59 | + |
| 60 | +async def main(): |
| 61 | + # Create an agent with streaming tools |
| 62 | + agent = Agent( |
| 63 | + name="Research Assistant", |
| 64 | + instructions="You are a helpful research assistant that can search documents and generate reports.", |
| 65 | + tools=[search_documents, generate_report], |
| 66 | + ) |
| 67 | + |
| 68 | + # Run the agent in streaming mode |
| 69 | + result = Runner.run_streamed( |
| 70 | + agent, |
| 71 | + input="Search for information about artificial intelligence and generate a brief report.", |
| 72 | + ) |
| 73 | + |
| 74 | + print("Streaming agent output:\n") |
| 75 | + |
| 76 | + # Stream events and display tool outputs in real-time |
| 77 | + async for event in result.stream_events(): |
| 78 | + # Handle tool streaming events |
| 79 | + if event.type == "tool_output_stream_event": |
| 80 | + assert isinstance(event, ToolOutputStreamEvent) |
| 81 | + print(f"[{event.tool_name}] {event.delta}", end="", flush=True) |
| 82 | + |
| 83 | + # Handle run item events (final outputs) |
| 84 | + elif event.type == "run_item_stream_event": |
| 85 | + if event.name == "tool_output": |
| 86 | + print(f"\n✓ Tool '{event.item.agent.name}' completed\n") |
| 87 | + elif event.name == "message_output_created": |
| 88 | + print(f"\n[Agent Response]: {event.item}\n") |
| 89 | + |
| 90 | + # Get final result |
| 91 | + print("\n" + "=" * 60) |
| 92 | + print("Final output:", result.final_output) |
| 93 | + print("=" * 60) |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + asyncio.run(main()) |
0 commit comments