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.
Arguments
| Parameter | Type | Description |
|---|---|---|
model | impl Into<String> | The model identifier to use (e.g., "gpt-4o") |
input | impl Into<Input> | The input for the completion. Can be a string (&str or String), Vec<Message>, or InputObject |
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:
Vec<Message> or InputObject
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 |
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 Result containing a Stream that yields Result<StreamChunk>. Each chunk contains incremental updates to the response.
StreamChunk Object
Each chunk yielded by the stream has the following structure:| Property | Type | Description |
|---|---|---|
id | String | Unique identifier for the completion |
object | String | Object type (typically "chat.completion.chunk") |
created | u64 | Unix timestamp of when the chunk was created |
model | String | Model identifier used for the completion |
choices | Vec<StreamChoice> | Array of streaming choices (typically one) |
StreamChoice Object
Each choice in thechoices array contains:
| Property | Type | Description |
|---|---|---|
index | u32 | The index of this choice in the array |
delta | StreamDelta | The incremental update to the message |
finish_reason | Option<String> | Reason why the generation stopped. Only present in the final chunk. Possible values: "stop", "length", "tool_calls", "content_filter", or None |
StreamDelta Object
Thedelta object contains incremental updates:
| Property | Type | Description |
|---|---|---|
role | Option<Role> | The role of the message (typically Role::Assistant). Only present in the first chunk |
content | Option<String> | Incremental text content. Each chunk contains a portion of the full response |
tool_calls | Option<Vec<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() | Option<&str> | Shortcut to choices[0].delta.content.as_deref() - the incremental text content |
role() | Option<&Role> | Shortcut to choices[0].delta.role.as_ref() - the message role (first chunk only) |
finish_reason() | Option<&str> | Shortcut to choices[0].finish_reason.as_deref() - the finish reason (final chunk only) |
Understanding Streaming Behavior
Chunk Structure
- First chunk: Contains
role(typicallyRole::Assistant) and may contain initialcontent - Content chunks: Contain incremental
contentupdates - Final chunk: Contains
finish_reasonindicating 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 |
None | 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 at two levels:
- Initial error: When creating the stream (returns
Result<Stream>) - Stream errors: Individual chunks may contain errors (returns
Result<StreamChunk>)