Recipes Structured Output

Structured Output (JSON Extraction)

Ask an LLM for a Person and get back %{name: "Alice", age: 30, email: "alice@..."} — typed, validated, every time.

ReqLLM.generate_object/3 takes a schema and forces the model to return JSON that matches it. No “regex the markdown out of the response”. No retries because the model wrapped the JSON in ```​json fences. The provider does the structured-output dance and ReqLLM hands you a map. This is what you reach for when you’re extracting data from emails, normalising user-submitted free text, or replacing a brittle regex-based parser.

When to reach for this

  • Replacing a regex-based parser that breaks every time someone phrases things differently.
  • Extracting fields from inbound emails, scanned PDFs, or user-submitted free text.
  • Normalising data before it hits an Ecto changeset — pair with validate_required/2 on the way in.
  • Provider-specific tip: OpenAI's structured output is rock-solid. Anthropic does it well via tool calling. Gemini's JSON mode is improving fast.