AgentSea.Structured (agentsea_structured v0.1.0)

Copy Markdown

Structured output: extract a validated Ecto struct from an LLM.

The Elixir analogue of "Zod schema → validated object" is "Ecto schema → validated changeset". You define an embedded schema with a changeset/2, and extract/3 prompts a provider for JSON, casts it through the changeset, and — on a parse or validation error — feeds the problem back to the model and retries (up to :max_retries).

Works over any AgentSea.Provider (so requests can run through the gateway) and needs no network in tests.

Example

defmodule Person do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key false
  embedded_schema do
    field :name, :string
    field :age, :integer
  end

  def changeset(struct, params) do
    struct
    |> cast(params, [:name, :age])
    |> validate_required([:name, :age])
    |> validate_number(:age, greater_than: 0)
  end
end

{:ok, %Person{}} =
  AgentSea.Structured.extract(Person, "Ada Lovelace, 36",
    provider: {AgentSea.Providers.Anthropic, []},
    model: "claude-opus-4-8"
  )

Summary

Functions

Extract a schema struct from input.

Types

input()

@type input() :: String.t() | [AgentSea.Provider.message()]

schema()

@type schema() :: module()

Functions

extract(schema, input, opts)

@spec extract(schema(), input(), keyword()) :: {:ok, struct()} | {:error, term()}

Extract a schema struct from input.

Options:

  • :provider{module, opts} (required); module implements AgentSea.Provider
  • :model — model id (or supply it in the provider opts)
  • :max_retries — validation/parse retries (default 3)
  • :system_prompt— extra instructions, prepended to the schema instructions