- Code files from the deeplearning.ai short course on Structured LLM Output
- Certificate
- Handwritten Notes
Structured outputs transform raw LLM responses into predictable, machine-readable formats that:
- Ensure consistency in response format and structure
- Simplify integration with downstream systems and databases
- Reduce hallucinations by constraining output scope and format
- Improve reliability for production applications
- Enable validation of output against schema requirements
- Facilitate parsing without complex regex or post-processing
Uses JSON schema to define the structure directly in the API call, enforced by the model itself.
client.beta.chat.completions.parse(
response_format=MySchema
)
Iteratively improves outputs by validating against schemas and retrying when validation fails.
instructor_client.chat.completions.create(
response_model=MySchema,
max_retries=3
)
Uses parsing expression grammars to constrain token generation at inference time.
outlines.generate.json(model, MySchema)
# or
outlines.generate.regex(model, pattern)