ExAgent.Output (ExAgent v3.0.0)

View Source

Describes a structured output in an agent's output contract.

Stability — Stable.

tool/2 names a schema's output tool. function/4 adds an output function: the model calls the tool with the schema's arguments, and the run's output is the value the function returns for the validated map or Ecto struct.

Summary

Types

The result of an output function. {:ok, value} makes value the run's output; {:retry, message} sends message back to the model and counts toward :output_retries; {:error, reason} fails the run with {:output_function_failed, name, reason}.

Functions

An output function under the output tool name.

Configures a schema's tool description and optional branch name in a union.

Types

function_result()

@type function_result() :: {:ok, term()} | {:retry, String.t()} | {:error, term()}

The result of an output function. {:ok, value} makes value the run's output; {:retry, message} sends message back to the model and counts toward :output_retries; {:error, reason} fails the run with {:output_function_failed, name, reason}.

schema()

@type schema() :: module() | map()

spec()

@type spec() :: %ExAgent.Output{
  description: String.t() | nil,
  function: (map(), ExAgent.RunContext.t() -> function_result()) | nil,
  module: schema(),
  name: String.t() | nil
}

Functions

function(name, schema_module, fun, opts \\ [])

@spec function(
  String.t(),
  schema(),
  (map(), ExAgent.RunContext.t() -> function_result()),
  keyword()
) ::
  spec()

An output function under the output tool name.

The model calls name with the arguments of the JSON Schema or Ecto module. Once they pass the schema and the agent's :output_validators, the run calls fun.(output, ctx) once and its {:ok, value} becomes Result.output, with Result.output_name set to name. The name is any valid tool name, also for a single output, and must differ from the agent's tools.

{:retry, message} or raise ExAgent.ModelRetry sends message back to the model and counts toward :output_retries. {:error, reason}, an exception or any other return fails the run with {:output_function_failed, name, reason}, where an exception's reason is {:raised, message} and an unexpected return's is {:invalid_result, inspected}.

A crash before the run persists its result calls the function again, so it must be pure or idempotent: effects belong in tools with approval. In a run with a continuation, or in a durable composition or delegation, the value must be exact JSON (string keys, no structs or atoms other than true, false and nil), or the run fails with {:output_not_persistable, name}. Output functions need output_mode: :tool. The continuation records each branch as a function, so resuming with a plain output where a function was, or the reverse, fails with :continuation_output_changed.

Options: :description, the tool description the model sees.

tool(module, opts \\ [])

@spec tool(schema(), keyword()) :: spec()

Configures a schema's tool description and optional branch name in a union.