Force the model to produce JSON matching a schema.
Wraps Candil.Backend.chat/3 with:
- A system prompt that tells the model to respond with JSON only.
- A post-validation step that re-parses the response and rejects anything that doesn't match the schema (best-effort: required properties present and primitive types correct).
On validation failure, retries once with a feedback message describing the mismatch.
Example
schema = %{
"type" => "object",
"properties" => %{
"title" => %{"type" => "string"},
"year" => %{"type" => "integer"}
},
"required" => ["title", "year"]
}
Candil.Structured.complete(:llama3, "Recommend a sci-fi book", schema)
# => {:ok, %{"title" => "Dune", "year" => 1965}}
Summary
Functions
Ask the model to complete a prompt with output that conforms to the given JSON schema. Returns the parsed map on success.
Types
Functions
Ask the model to complete a prompt with output that conforms to the given JSON schema. Returns the parsed map on success.
Options
:backend— explicitCandil.Backendmodule to use. Defaults toCandil.Backend.for(Backend.infer_provider(:local, model), model).:max_retries— number of retry attempts after validation failure (default 1; total attempts = 1 + max_retries).:temperature— passed through to the backend.