This directory contains a series of examples demonstrating the streaming capabilities of the A2A (Agent-to-Agent) library. Each example builds on concepts from previous examples to showcase different aspects of streaming functionality.
Before running these examples, make sure you have the required dependencies installed:
# Core requirements
pip install python-a2a
# Additional requirements for UI examples (05_streaming_ui_integration.py)
pip install colorama tqdm flask
# Additional requirements for distributed examples (06_distributed_streaming.py)
pip install aiohttp tqdm-
basic_streaming.py - Minimal streaming implementation
- Simplest possible streaming server (only core essentials)
- Minimal streaming client with proper error handling
- Proper dictionary chunk handling
- Ideal starting point for understanding streaming
-
01_basic_streaming.py - Comprehensive introduction to streaming basics
- Simple streaming server implementation
- Basic client for consuming streams
- Comparison between streamed and non-streamed responses
- Simulated thinking delays and natural language chunking
-
02_advanced_streaming.py - Advanced streaming techniques
- Metrics tracking for streaming performance
- Different chunking strategies (sentence, word, paragraph)
- Performance visualization and analytics
- Stream buffering and handling
-
03_streaming_llm_integration.py - Integrating with LLM providers
- Bridging LLM provider streaming APIs with A2A
- Handling different provider formats (OpenAI, Anthropic, Bedrock)
- Transformer pipelines for content processing
- Error handling and fallback mechanisms
-
04_task_based_streaming.py - Structured task-based streaming
- Task state transitions (WAITING, COMPLETED, etc.)
- Progress tracking with step-by-step updates
- Artifact generation during streaming
- Partial and final results as structured artifacts
-
05_streaming_ui_integration.py - Integrating streaming with UIs
- CLI-based streaming visualization
- Web interface using Server-Sent Events (SSE)
- Interactive streaming controls (pause, resume, cancel)
- Real-time progress visualization
-
06_distributed_streaming.py - Distributed streaming architecture
- Multiple streaming servers with load balancing
- Stream aggregation from multiple sources
- Fault tolerance and failover strategies
- Performance monitoring and metrics
Each example is designed to be run independently. Simply use Python to execute the desired example:
python 01_basic_streaming.pyFor the more advanced examples, make sure to install the dependencies first:
# For UI integration example
python 05_streaming_ui_integration.py
# For distributed streaming example
python 06_distributed_streaming.py-
Missing Module Errors: Make sure to install all required dependencies as listed in the Dependencies section.
-
"Message.init() missing required argument 'role'": This error occurs because the Message constructor requires a role parameter. If you're creating your own code based on these examples, always include
role=MessageRole.USERorrole=MessageRole.AGENTwhen creating Message objects. -
Type Errors with Dictionary Chunks: When handling streamed chunks, remember they can sometimes be dictionaries rather than simple strings. Handle this properly by checking types and extracting content when needed.
-
Port Conflicts: If a server won't start due to a port being in use, modify the port number in the code or wait a few moments for the previous server process to fully terminate.
The examples in this directory demonstrate these key streaming concepts:
- Stream Creation: How to implement streaming on the server side using async generators
- Stream Consumption: How to consume streams on the client side
- Chunking Strategies: Different approaches to breaking content into streamable chunks
- Error Handling: Robust error handling for streaming scenarios
- Performance Metrics: Tracking and analyzing streaming performance
- UI Integration: Techniques for rendering streaming content in user interfaces
- LLM Integration: Connecting A2A streaming with LLM providers
- Distributed Architecture: Advanced patterns for scalable streaming systems
If you encounter issues with streaming, the core_streaming_test.py script provides a simple test harness to verify that core streaming functionality is working correctly.
For more information on A2A streaming, refer to:
- A2A Example Documentation
- The implementation in
python_a2a/server/http.pyandpython_a2a/client/streaming.py - Official Documentation for the full API reference