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
| Parameter | Type | Description |
|---|---|---|
model | string | The model identifier to use (e.g., "openai/gpt-4o") |
input | any | The input for the completion. Can be a string, InputObject, *InputObject, or map[string]interface{} |
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:
| Property | Type | Description |
|---|---|---|
Messages | []Message | Array of conversation messages |
Tools | []Tool | Array of function tools available to the model |
ToolChoice | any | Controls which tool (if any) the model should call. See Tools documentation for details |
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:| Property | Type | Description |
|---|---|---|
ID | string | Unique identifier for the completion |
Object | string | Object type (typically "chat.completion.chunk") |
Created | int64 | Unix timestamp of when the chunk was created |
Model | string | Model identifier used for the completion |
Choices | []StreamChoice | Array of streaming choices (typically one) |
StreamChoice Object
Each choice in theChoices array contains:
| Property | Type | Description |
|---|---|---|
Index | int | The index of this choice in the array |
Delta | *StreamDelta | The incremental update to the message |
FinishReason | *string | Reason why the generation stopped. Only present in the final chunk. Possible values: "stop", "length", "tool_calls", "content_filter", or nil |
StreamDelta Object
TheDelta object contains incremental updates:
| Property | Type | Description |
|---|---|---|
Role | *string | The role of the message (typically "assistant"). Only present in the first chunk |
Content | *string | Incremental text content. Each chunk contains a portion of the full response |
ToolCalls | []ToolCall | Array of tool calls (if any). See Tools documentation for details |
Convenience Methods
TheStreamChunk struct provides convenience methods for easier access:
| Method | Return Type | Description |
|---|---|---|
Text() | string | Shortcut to Choices[0].Delta.Content - the incremental text content (returns empty string if nil) |
Role() | string | Shortcut to Choices[0].Delta.Role - the message role (first chunk only, returns empty string if nil) |
FinishReason() | string | Shortcut to *Choices[0].FinishReason - the finish reason (final chunk only, returns empty string if nil) |
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
| Value | Description |
|---|---|
"stop" | Model generated a complete response and stopped naturally |
"length" | Response was cut off due to token limit |
"tool_calls" | Model requested tool/function calls |
"content_filter" | Content was filtered by safety systems |
"" (empty string) | Generation is still in progress (not the final chunk) |
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