Streaming Responses Example

Python Intermediate

Implement streaming responses for a more interactive user experience with incremental text display.

Streaming Responses Example

This example demonstrates how to implement streaming responses with the ZeebeeAI SDK for a more interactive user experience with incremental text display.

Why Use Streaming?

Streaming responses offer several advantages:

  • Improved perceived performance - Users see the first tokens immediately
  • Better user experience - Progressive rendering of long responses feels more natural
  • Interactive capability - Allows for real-time interaction with partial results
  • Reduced timeout issues - Keeps the connection alive during lengthy responses
Python Implementation
from zeebee_ai import ZeebeeClient

# Initialize the client
client = ZeebeeClient(api_key="your_api_key_here")

# Function to process streaming response
def process_stream(generator):
    full_text = ""
    print("AI Response: ", end="", flush=True)
    
    for chunk in generator:
        if "text" in chunk:
            text = chunk["text"]
            full_text += text
            # Print incrementally
            print(text, end="", flush=True)
    
    print("\n\nFull response received.")
    return full_text

# Request with streaming enabled
response_stream = client.chat(
    message="Tell me about quantum computing",
    model="gpt-4o",
    stream=True  # Enable streaming
)

# Process the stream
final_response = process_stream(response_stream)
JavaScript Implementation
// Initialize the SDK
const chat = new ZeebeeChat({
    apiKey: 'your_api_key_here'
});

// Function to send a streaming request
async function streamResponse() {
    // Create a placeholder for the response
    const responseElement = document.getElementById('response');
    responseElement.textContent = '';
    
    try {
        await chat.sendMessage('Tell me about quantum computing', {
            model: 'gpt-4o',
            stream: true,
            onChunk: function(chunk) {
                // Append each chunk as it arrives
                responseElement.textContent += chunk;
                
                // Auto-scroll to show latest content
                responseElement.scrollTop = responseElement.scrollHeight;
            }
        });
        
        console.log('Streaming complete');
    } catch (error) {
        console.error('Streaming error:', error);
    }
}
Error Handling in Streaming
# Python error handling for streaming
try:
    response_stream = client.chat(
        message="Tell me about quantum computing",
        model="gpt-4o",
        stream=True
    )
    
    for chunk in response_stream:
        # Process each chunk
        if "text" in chunk:
            print(chunk["text"], end="", flush=True)
            
except Exception as e:
    print(f"\nError during streaming: {str(e)}")
    # Implement fallback to non-streaming request
    try:
        print("\nAttempting non-streaming fallback...")
        response = client.chat(
            message="Tell me about quantum computing",
            model="gpt-4o",
            stream=False
        )
        print(response["message"])
    except Exception as fallback_error:
        print(f"Fallback also failed: {str(fallback_error)}")
Full Example

Download the complete example application from the button above to see streaming in action with an interactive console interface.

Next Steps

Check the Documentation

Learn more about the ZeebeeAI SDK in our detailed documentation.

View Docs