Alloy.Provider.OpenAIStream (alloy v0.16.0)

Copy Markdown View Source

Shared OpenAI-format SSE stream parser.

Used by Alloy.Provider.OpenAICompat for Chat Completions providers such as DeepSeek, Mistral, OpenRouter, Gemini, and Ollama. The native OpenAI and xAI providers use Responses API parsing instead. Each compatible provider calls stream/5 or stream/6 with its own URL, headers, and optional streaming options (:on_event, :deadline, :cancel_token); this module handles SSE parsing and response normalization.

OpenAI Streaming Format

data: {"choices":[{"index":0,"delta":{"content":"chunk"}}]}
data: {"choices":[{"index":0,"delta":{"tool_calls":[...]}}]}
data: [DONE]

Text deltas are emitted via on_chunk. Reasoning deltas ({:thinking_delta, text}) and tool call argument deltas ({:tool_call_delta, event}) are emitted via :on_event while being accumulated into the final response, which has the same shape as complete/3.

Summary

Functions

Execute a streaming request against an OpenAI-compatible endpoint.

Functions

stream(url, headers, body, on_chunk, req_options, opts \\ [])

@spec stream(
  String.t(),
  [{String.t(), String.t()}],
  map(),
  (String.t() -> :ok),
  keyword(),
  keyword()
) ::
  {:ok, Alloy.Provider.completion_response()} | {:error, term()}

Execute a streaming request against an OpenAI-compatible endpoint.

Returns {:ok, completion_response()} | {:error, term()}.