ToolCall.Stream (ToolCall v0.4.0)

Copy Markdown

Aggregates provider streaming deltas into complete tool calls.

run/3 takes a complete call: a tool name and a decoded arguments map. While a response streams, the provider sends that call in pieces. This module folds the provider's chunks and answers {:ok, calls}, where each call is %{id: id | nil, name: name, arguments: arguments_map} and is ready for ToolCall.run/3.

chunks
|> ToolCall.Stream.aggregate(:openai)
#=> {:ok, [%{id: "call_1", name: "get_weather", arguments: %{"city" => "Oslo"}}]}

or, chunk by chunk:

acc = ToolCall.Stream.new(:anthropic)
acc = Enum.reduce(chunks, acc, &ToolCall.Stream.feed(&2, &1))
ToolCall.Stream.finish(acc)

Supported streams:

  • :openai - Chat Completions delta.tool_calls fragments, keyed by index. One choice is aggregated, the first by default; pass choice: n to new/2 or aggregate/3 to select another. A choice is matched by index when the chunk carries it, otherwise by list position.
  • :anthropic - Messages content_block_start for a tool_use block plus input_json_delta fragments (and the block's initial input when there are none).
  • :gemini - streamGenerateContent parts with a complete functionCall. One candidate is aggregated, the first by default; pass choice: n to select another; a candidate is matched by index when present. Gemini has no call id, so id is nil.

The concatenated JSON is decoded with the standard JSON module, so arguments is a map. Invalid JSON, or an Anthropic input that is not a map when there are no fragments, is {:error, {:invalid_stream_arguments, {id, name, payload}}}; a call that never receives a name is {:error, {:missing_name, id}}, and a selected choice that never appears is {:error, {:choice_not_found, choice}}.

Summary

Functions

Aggregates an enumerable of chunks in one pass.

Folds one streamed chunk into the accumulator.

Returns the completed calls in their provider order.

Starts an accumulator for provider (:openai, :anthropic or :gemini).

Types

call()

@type call() :: %{
  id: String.t() | nil,
  name: String.t(),
  arguments: %{optional(String.t()) => term()}
}

error()

@type error() ::
  {:missing_name, String.t() | nil}
  | {:invalid_stream_arguments, {String.t() | nil, String.t() | nil, term()}}
  | {:choice_not_found, non_neg_integer()}

provider()

@type provider() :: :openai | :anthropic | :gemini

t()

@type t() :: %ToolCall.Stream{
  calls: %{required(term()) => map()},
  choice: non_neg_integer(),
  choice_seen?: boolean(),
  next: non_neg_integer(),
  order: [term()],
  provider: provider(),
  saw_choices?: boolean()
}

Functions

aggregate(chunks, provider)

@spec aggregate(Enumerable.t(), provider()) :: {:ok, [call()]} | {:error, error()}

Aggregates an enumerable of chunks in one pass.

aggregate(chunks, provider, opts)

@spec aggregate(Enumerable.t(), provider(), keyword()) ::
  {:ok, [call()]} | {:error, error()}

feed(acc, chunk)

@spec feed(t(), map()) :: t()

Folds one streamed chunk into the accumulator.

Chunks that carry no tool-call data (text deltas, message metadata) are ignored.

finish(acc)

@spec finish(t()) :: {:ok, [call()]} | {:error, error()}

Returns the completed calls in their provider order.

A call with a missing name or unparsable arguments answers {:error, reason} and drops the whole stream result.

new(provider, opts \\ [])

@spec new(provider(), keyword()) :: t()

Starts an accumulator for provider (:openai, :anthropic or :gemini).

:choice selects the OpenAI choice or Gemini candidate, starting at 0. The default is the first choice. Anthropic has a single choice.