How Instructor uses Ecto for structured LLM outputs
mainInstructor bridges the gap between unstructured LLM text and structured Software 1.0 data by using Ecto schemas as the source of truth.
Instead of manually parsing LLM strings, you define an Ecto.Schema. Instructor automatically converts this schema into a JSON schema and instructs the LLM to conform to it (using techniques like function calling or BNF grammar sampling). The resulting output is then treated as standard Ecto data, allowing you to use familiar casting and validation patterns.
This approach allows for bidirectional interoperability: you can turn data into text for the LLM, and turn LLM text back into structured data for your application.
defmodule Recipe do
use Ecto.Schema
@primary_key false
embedded_schema do
field :title, :string
field :cook_time, :integer
field :steps, {:array, :string}
embeds_many :ingredients, Ingredients, primary_key: false do
field :name, :string
field :quantity, :decimal
field :unit, :string
end
end
end