Use this file to discover all available pages before exploring further.
The stream() method is used to make streaming chat completion requests to the Edgee AI Gateway. It returns a Result containing a Stream that yields Result<StreamChunk> objects as they arrive from the API.
When input is a string, it’s automatically converted to a user message:
use tokio_stream::StreamExt;let mut stream = client.stream("gpt-5.2", "Tell me a story").await?;while let Some(result) = stream.next().await { match result { Ok(chunk) => { if let Some(text) = chunk.text() { print!("{}", text); } if let Some(reason) = chunk.finish_reason() { println!("\nFinished: {}", reason); } } Err(e) => eprintln!("Stream error: {}", e), }}// Equivalent to: input: InputObject::new(vec![Message::user("Tell me a story")])
When input is a Vec<Message> or InputObject, you have full control over the conversation:
Property
Type
Description
messages
Vec<Message>
Array of conversation messages
tools
Option<Vec<Tool>>
Array of function tools available to the model
tool_choice
Option<serde_json::Value>
Controls which tool (if any) the model should call. See Tools documentation for details
tags
Option<Vec<String>>
Optional tags to categorize and label the request for analytics and filtering. Can also be sent via the x-edgee-tags header (comma-separated)
compression_model
Option<String>
Compression model for this request: "agentic", "claude", "opencode", "cursor", or "customer". Each model is a bundle of compression strategies. Overrides API key settings when present.
compression_configuration
Option<CompressionConfiguration>
Configuration for the compression model. Currently only available for agentic. Contains optional rate (0.0-1.0, default 0.8) and semantic_preservation_threshold (0-100).
use edgee::Message;use tokio_stream::StreamExt;let messages = vec![ Message::system("You are a helpful assistant."), Message::user("Write a poem about coding"),];let mut stream = client.stream("gpt-5.2", messages).await?;while let Some(result) = stream.next().await { if let Ok(chunk) = result { if let Some(text) = chunk.text() { print!("{}", text); } }}
Reason why the generation stopped. Only present in the final chunk. Possible values: "stop", "length", "tool_calls", "content_filter", or None
Example - Handling Multiple Choices:
use tokio_stream::StreamExt;let mut stream = client.stream("gpt-5.2", "Give me creative ideas").await?;while let Some(result) = stream.next().await { if let Ok(chunk) = result { for choice in &chunk.choices { if let Some(content) = &choice.delta.content { println!("Choice {}: {}", choice.index, content); } } }}
Final chunk: Contains finish_reason indicating why generation stopped
Example - Collecting Full Response:
use tokio_stream::StreamExt;let mut stream = client.stream("gpt-5.2", "Tell me a story").await?;let mut full_text = String::new();while let Some(result) = stream.next().await { match result { Ok(chunk) => { if let Some(text) = chunk.text() { full_text.push_str(text); print!("{}", text); // Also display as it streams } } Err(e) => eprintln!("Stream error: {}", e), }}println!("\n\nFull response ({} characters):", full_text.len());println!("{}", full_text);
Some chunks may not contain content. This is normal and can happen when:
The chunk only contains metadata (role, finish_reason)
The chunk is part of tool call processing
Network buffering creates empty chunks
Always check for chunk.text() before using it:
use tokio_stream::StreamExt;let mut stream = client.stream("gpt-5.2", "Hello").await?;while let Some(result) = stream.next().await { if let Ok(chunk) = result { if let Some(text) = chunk.text() { // ✅ Good: Check before using println!("{}", text); } // ❌ Bad: println!("{:?}", chunk.text()) - may print None }}