stream() method is used to make streaming chat completion requests to the Edgee AI Gateway. It returns a generator that yields StreamChunk objects as they arrive from the API.
Arguments
Input Types
String Input
Wheninput is a string, it’s automatically converted to a user message:
InputObject or Dictionary
Wheninput is an InputObject or dictionary, you have full control over the conversation:
For details about
Message type, see the Send Method documentation.
For details about Tool and ToolChoice types, see the Tools documentation.
Example - Streaming with Messages:
Return Value
Thestream() method returns a generator that yields StreamChunk objects. Each chunk contains incremental updates to the response.
StreamChunk Object
Each chunk yielded by the generator has the following structure:StreamChoice Object
Each choice in thechoices array contains:
Example - Handling Multiple Choices:
StreamDelta Object
Thedelta object contains incremental updates:
Convenience Properties
TheStreamChunk class provides convenience properties for easier access:
Example - Using Convenience Properties:
Understanding Streaming Behavior
Chunk Structure
- First chunk: Contains
role(typically"assistant") and may contain initialcontent - Content chunks: Contain incremental
contentupdates - Final chunk: Contains
finish_reasonindicating why generation stopped
Finish Reasons
Empty Chunks
Some chunks may not containcontent. 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
chunk.text before using it:
Alternative: Using send() with stream=True
You can also use thesend() method with stream=True to get streaming responses:
stream() method is a convenience wrapper that calls send() with stream=True.
Error Handling
Thestream() method can raise exceptions: