Alloy.Provider.Error exception (alloy v0.16.0)

Copy Markdown View Source

A provider failure with enough structure for the loop to act on it.

The built-in HTTP providers return {:error, %Alloy.Provider.Error{}}. The loop reads :kind to decide whether to retry (retryable?/1), how long to wait (:retry_after_ms, from the Retry-After header), and whether to compact the conversation and try again (:context_overflow).

Custom providers can return this struct to get the same behaviour; plain string errors keep working.

Alloy.Result.error stays a string — Exception.message/1 of this struct, in the same "<type>: <message>" / "HTTP <status>: <body>" shape earlier versions returned — except for kind: :cancelled, where cooperative cancellation sets result.status and result.error to the atom :cancelled. In all cases (including :cancelled), the struct itself is available as result.metadata.run.provider_error. It also implements String.Chars, so interpolating it keeps working.

Kinds

  • retried: :rate_limited, :overloaded, :server_error, :timeout, :network, :malformed_reply (transient garbled/truncated JSON, undecodable SSE frames, or invalid model tool-call argument JSON; retries only run before any stream output is emitted and are bounded by :max_retries)
  • not retried: :context_overflow (the loop compacts once instead), :protocol_error (valid JSON that violates the provider wire schema — non-object top-level responses, malformed choices/candidates/blocks, missing required tool-call id/name, or mismatched SSE event deltas), :quota (billing or spend limit), :auth, :invalid_request, :session_uncertain (a persisted CLI turn may have been recorded), :shutdown (interrupted by supervisor shutdown; skips retries and fallbacks — on a resumed Codex session, also rotate :session_generation), :cancelled (interrupted cooperatively via Alloy.Cancel; skips retries and fallbacks — on a resumed Codex session, also rotate :session_generation), :unknown

Summary

Functions

Builds an error reported inside a successful HTTP response: a failed response object or an error event in an SSE stream. Accepts the body shapes from_response/3 reads and classifies by code, type, and numeric HTTP status codes (400..599) carried in error.code.

Builds an error from a non-success HTTP response.

Builds an error from a transport failure (connection refused, closed, timed out) reported by Req, Finch or Mint.

Whether the loop should retry after this error.

Types

kind()

@type kind() ::
  :rate_limited
  | :overloaded
  | :server_error
  | :timeout
  | :network
  | :malformed_reply
  | :protocol_error
  | :session_uncertain
  | :context_overflow
  | :quota
  | :auth
  | :invalid_request
  | :shutdown
  | :cancelled
  | :unknown

t()

@type t() :: %Alloy.Provider.Error{
  __exception__: term(),
  code: String.t() | nil,
  kind: kind(),
  message: String.t(),
  retry_after_ms: non_neg_integer() | nil,
  status: pos_integer() | nil,
  type: String.t() | nil
}

Functions

from_body(body)

@spec from_body(term()) :: t()

Builds an error reported inside a successful HTTP response: a failed response object or an error event in an SSE stream. Accepts the body shapes from_response/3 reads and classifies by code, type, and numeric HTTP status codes (400..599) carried in error.code.

from_response(status, headers, body)

@spec from_response(pos_integer(), map() | [{String.t(), String.t()}], term()) :: t()

Builds an error from a non-success HTTP response.

Reads the error body shapes used by Anthropic ({"error": {"type", ...}}), OpenAI ({"error": {"type", "code", ...}}), Gemini ({"error": {"status", ...}}, sometimes wrapped in a list) and most OpenAI-compatible servers. headers may be Req's header map or a list of {name, value} tuples.

from_transport(reason)

@spec from_transport(term()) :: t()

Builds an error from a transport failure (connection refused, closed, timed out) reported by Req, Finch or Mint.

retryable?(error)

@spec retryable?(t()) :: boolean()

Whether the loop should retry after this error.