Converts Ecto schema modules to JSON Schema maps for structured output.
Requires {:ecto, "~> 3.0"} as an optional dependency. If Ecto is not
available, calling these functions will raise at runtime.
Type Mapping
| Ecto type | JSON Schema |
|---|---|
:string, :binary | {"type": "string"} |
:integer, :id | {"type": "integer"} |
:float, :decimal | {"type": "number"} |
:boolean | {"type": "boolean"} |
:map | {"type": "object"} |
{:array, inner} | {"type": "array", "items": ...} |
:date | {"type": "string", "format": "date"} |
:utc_datetime, etc. | {"type": "string", "format": "date-time"} |
embeds_one | nested object |
embeds_many | array of nested objects |
Excluded Fields
The following fields are automatically excluded: :id, :inserted_at,
:updated_at, and virtual fields.
Examples
defmodule MyApp.Person do
use Ecto.Schema
schema "people" do
field :name, :string
field :age, :integer
field :email, :string
timestamps()
end
end
schema = LlamaCppEx.Schema.to_json_schema(MyApp.Person)
# => %{"type" => "object", "properties" => %{"name" => ..., "age" => ..., "email" => ...}, ...}
# Use directly with generate/chat
{:ok, json} = LlamaCppEx.chat(model, messages, json_schema: schema, temp: 0.0)
Summary
Functions
Converts an Ecto schema module to a JSON Schema map.
Functions
Converts an Ecto schema module to a JSON Schema map.
Extracts fields from the schema, maps Ecto types to JSON Schema types,
and marks all non-virtual fields as required. Excludes :id, timestamp
fields (:inserted_at, :updated_at), and virtual fields.