Skip to main content
The 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

The Stream() method accepts the same input types as Send():

String Input

When input is a string, it’s automatically converted to a user message:

InputObject or Map

When input 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

The Stream() method returns two channels:
  1. <-chan *StreamChunk: Channel that receives streaming chunks
  2. <-chan error: Channel that receives errors

StreamChunk Object

Each chunk received from the channel has the following structure:

StreamChoice Object

Each choice in the Choices array contains: Example - Handling Multiple Choices:

StreamDelta Object

The Delta object contains incremental updates:

Convenience Methods

The StreamChunk struct provides convenience methods for easier access: Example - Using Convenience Methods:

Understanding Streaming Behavior

Chunk Structure

  1. First chunk: Contains Role (typically "assistant") and may contain initial Content
  2. Content chunks: Contain incremental Content updates
  3. Final chunk: Contains FinishReason indicating why generation stopped
Example - Collecting Full Response:

Finish Reasons

Empty Chunks

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:

Error Handling

The Stream() method can return errors in two ways:
  1. Initial error: When creating the stream (returned immediately if the request fails)
  2. Stream errors: Individual errors sent through the errChan channel