Alloy.Provider.OpenAIStream (alloy v0.10.1)

Copy Markdown View Source

Shared OpenAI-format SSE stream parser.

Used by all OpenAI-compatible providers (OpenAI, DeepSeek, Mistral, OpenRouter, xAI, Ollama). Each provider calls stream/5 with its own URL and headers; 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. Tool call argument deltas are accumulated silently. The final response 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)

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

Execute a streaming request against an OpenAI-compatible endpoint.

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