Stream() method is used to make streaming chat completion requests to the Edgee AI Gateway. It returns two channels: one for StreamChunk objects and one for errors.
Arguments
Input Types
TheStream() method accepts the same input types as Send():
String Input
Wheninput is a string, it’s automatically converted to a user message:
InputObject or Map
Wheninput is an InputObject or map[string]interface{}, 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 two channels:
<-chan *StreamChunk: Channel that receives streaming chunks<-chan error: Channel that receives errors
StreamChunk Object
Each chunk received from the channel has the following structure:StreamChoice Object
Each choice in theChoices array contains:
Example - Handling Multiple Choices:
StreamDelta Object
TheDelta object contains incremental updates:
Convenience Methods
TheStreamChunk struct provides convenience methods for easier access:
Example - Using Convenience Methods:
Understanding Streaming Behavior
Chunk Structure
- First chunk: Contains
Role(typically"assistant") and may contain initialContent - Content chunks: Contain incremental
Contentupdates - Final chunk: Contains
FinishReasonindicating 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:
Error Handling
TheStream() method can return errors in two ways:
- Initial error: When creating the stream (returned immediately if the request fails)
- Stream errors: Individual errors sent through the
errChanchannel