Structured output
Ask for structured data and receive it as a typed Nim value with
generateObject.
The Recipe type supplies the JSON Schema automatically. nimgent validates the
model response, converts it to Recipe, and exposes the result through
recipe.value.
import std/[os, strutils]import nimgentimport nimgent/providers/openai
type Recipe = object name: string servings: int ingredients: seq[string]
let model = openAI(getEnv("OPENAI_API_KEY")).model("gpt-4o-mini")
echo "calling the model..."let recipe = generateObject[Recipe]( model, prompt = "A weeknight lasagna.")
echo "name: ", recipe.value.nameecho "servings: ", recipe.value.servingsecho "ingredients: ", recipe.value.ingredients.join(", ")Run it from the repository root:
OPENAI_API_KEY=... nim c -r examples/structured_output.nim