Skip to main content
The stream() method is used to make streaming chat completion requests to the Edgee AI Gateway. It returns an AsyncGenerator<StreamChunk> that yields response chunks as they arrive from the API.

Arguments

The stream() method accepts two arguments:

Input Types

String Input

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

InputObject

When input is an InputObject, 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 an AsyncGenerator<StreamChunk>. 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 the choices array contains: Example - Handling Multiple Choices:

StreamDelta Object

The delta object contains incremental updates:

Convenience Properties

The StreamChunk class provides convenience getters for easier access: Example - Using Convenience Properties:

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 finish_reason 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 throw errors: