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 Completionsdelta.tool_callsfragments, keyed byindex. One choice is aggregated, the first by default; passchoice: ntonew/2oraggregate/3to select another. A choice is matched byindexwhen the chunk carries it, otherwise by list position.:anthropic- Messagescontent_block_startfor atool_useblock plusinput_json_deltafragments (and the block's initialinputwhen there are none).:gemini-streamGenerateContentparts with a completefunctionCall. One candidate is aggregated, the first by default; passchoice: nto select another; a candidate is matched byindexwhen present. Gemini has no call id, soidisnil.
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
@type error() :: {:missing_name, String.t() | nil} | {:invalid_stream_arguments, {String.t() | nil, String.t() | nil, term()}} | {:choice_not_found, non_neg_integer()}
@type provider() :: :openai | :anthropic | :gemini
@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
@spec aggregate(Enumerable.t(), provider()) :: {:ok, [call()]} | {:error, error()}
Aggregates an enumerable of chunks in one pass.
@spec aggregate(Enumerable.t(), provider(), keyword()) :: {:ok, [call()]} | {:error, error()}
Folds one streamed chunk into the accumulator.
Chunks that carry no tool-call data (text deltas, message metadata) are ignored.
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.
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.