ShadAI Framework
  1. guides
ShadAI Framework
  • Index
  • README
  • Pricing
    • Pricing Plan
  • advanced
    • Best Practices
    • Tool Orchestration
    • Performance Optimization
    • Custom Tools
  • examples
    • Advanced Patterns
    • Custom Agent Examples
    • Market Research Examples
    • Multi-Document Analysis
    • Basic Query Examples
  • api-reference
    • Exceptions Reference
    • Engine Tool API
    • Shadai Client API Reference
    • Query Tool API
    • Summarize Tool API
    • Agent Tool API
    • Web Search Tool API
  • use-cases
    • Knowledge Synthesis
    • Research Assistant
    • Custom Workflows
    • Document Q&A
  • core-concepts
    • Architecture
    • Intelligent Agent
    • RAG System
    • Tools Overview
  • guides
    • Memory & Context
    • Streaming Responses
    • File Ingestion
    • Error Handling
    • Session Management
  • getting-started
    • Authentication
    • Your First Query
    • Quick Start
    • Installation
  1. guides

Streaming Responses

Shadai streams all responses in real-time, delivering tokens as they're generated for the best user experience.

Why Streaming?#

Without Streaming:
User asks question → Wait 30 seconds → Get complete answer
With Streaming:
User asks question → Immediate first token → Tokens flow → Natural experience
Benefits:
⚡ Instant feedback - Users see responses immediately
🎯 Better UX - No long waits
🔄 Interruptible - Can stop if not useful
📊 Progress indication - Users know something is happening

Basic Streaming#

All Shadai tools support streaming:
Key points:
async for - Iterate over chunks
chunk - Individual text piece (could be word, sentence, or token)
end="" - No newline between chunks
flush=True - Display immediately

Streaming All Tools#

Query#

Summarize#

Web Search#

Engine#

Agent#

Collecting Full Response#

Method 1: Accumulate Chunks#

Method 2: List Join#

Practical Patterns#

Pattern 1: Terminal Output#

Pattern 2: File Writing#

Pattern 3: Web Server (FastAPI)#

Pattern 4: WebSocket#

Pattern 5: Progress Indication#

Error Handling#

Basic Error Handling#

Mid-Stream Errors#

Advanced Techniques#

Chunk Processing#

Rate Limiting#

Buffered Streaming#

Multi-Stream Aggregation#

Performance Optimization#

Fast Display#

Memory Efficiency#

UI Integration#

React (Frontend)#

Streamlit#

Best Practices#

✅ Do This#

❌ Don't Do This#

Troubleshooting#

No Output Appearing#

Delayed Output#

Next Steps#

Error Handling - Robust error management
Web Server Integration - Production APIs
Performance Optimization - Scale efficiently

Pro Tip: Always use flush=True for real-time display in terminals!
Modified at 2025-10-17 17:47:30
Previous
Memory & Context
Next
File Ingestion
Built with