Structured outputs
Summary
Ollama has introduced support for structured outputs, enabling users to constrain a language model's responses to a specific format defined by a JSON schema. This feature, released on December 6, 2024, is accessible via updated Ollama Python and JavaScript libraries, as well as cURL requests. Structured outputs offer enhanced reliability and consistency compared to traditional JSON mode, facilitating use cases such as parsing data from documents, extracting information from images, and standardizing all language model responses. The implementation allows for schema definition using Pydantic in Python or Zod in JavaScript, with examples provided for data extraction and image description using models like llama3.1 and llama3.2-vision. OpenAI compatibility is also demonstrated.
Key takeaway
For AI Engineers building applications requiring precise data extraction or consistent LLM responses, integrating Ollama's new structured output feature is crucial. You should define your output schemas using Pydantic or Zod and set the model's temperature to 0 to ensure deterministic and reliable JSON formatting. This approach will significantly improve the robustness and predictability of your LLM-powered workflows, reducing post-processing complexity.
Key insights
Ollama now supports constraining LLM outputs to a JSON schema for reliable, consistent structured data.
Principles
- JSON schema defines output format.
- Pydantic/Zod simplify schema definition.
Method
Define a JSON schema (e.g., with Pydantic/Zod), pass it to the Ollama API's `format` parameter, and set temperature to 0 for deterministic output.
In practice
- Extract data from documents.
- Describe images with vision models.
- Standardize all LLM responses.
Topics
- Structured Outputs
- JSON Schema
- Ollama
- Data Extraction
- Vision Models
Code references
Best for: AI Engineer, Machine Learning Engineer, Software Engineer
Related on AIssential
Editorial summary, takeaway, and curation by AIssential. Original article published by Ollama Blog.