Documentation
¶
Overview ¶
Example: structured_output — asking an LLM for strict JSON and decoding it.
What you'll learn:
- How to request JSON output with model.ChatRequest.ResponseFormat
- The difference between "json_object" (valid JSON) and "json_schema" (JSON conforming to a schema in Metadata["json_schema"])
- How to decode the model's reply into a typed Go struct
This example needs a real LLM provider (it makes one network call). Configure one via the environment (OPENAI_API_KEY, ANTHROPIC_API_KEY, GEMINI_API_KEY, …); see examples/internal/providers for the full list. With no provider set it prints the required env vars and exits cleanly.
Run:
OPENAI_API_KEY=sk-... go run ./examples/structured_output/
Click to show internal directories.
Click to hide internal directories.