Ollama Structured Outputs Release

Ollama has introduced support for structured outputs, enabling developers to constrain a language model's response to a specific format defined by a JSON schema. This update provides greater reliability and consistency than previous JSON modes and is supported across Ollama's cURL API, Python, and JavaScript libraries.

Key Capabilities and Use Cases

Structured outputs allow models to adhere strictly to a predefined schema, which is critical for integrating LLM responses into programmatic workflows. Primary use cases include:

  • Data Parsing: Extracting structured information from unstructured documents.
  • Visual Analysis: Extracting data and descriptions from images using vision models.
  • Response Standardization: Ensuring all language model responses follow a consistent format for downstream processing.

Implementation Methods

To use structured outputs, the format parameter must be passed in the request. This can be achieved through several interfaces:

cURL API

The format parameter accepts a JSON schema object defining the required properties and their types (e.g., string, array).

Python Library

Ollama recommends using Pydantic to define the schema. The schema is passed to the format parameter using the model_json_schema() method.

JavaScript Library

Ollama recommends using Zod in conjunction with zod-to-json-schema to serialize the schema for the format parameter.

OpenAI Compatibility

Ollama supports the OpenAI-compatible beta.chat.completions.parse method, allowing developers to use Pydantic models as the response_format when connecting to a local Ollama instance.

Advanced Applications

Structured Data Extraction

By defining a schema (such as a PetList containing a list of Pet objects), the model can parse a natural language description of multiple entities and return them as a validated JSON array.

Vision Model Integration

Structured outputs are compatible with vision models like llama3.2-vision. This allows for detailed image analysis where the model returns specific fields such as summary, objects (with confidence scores), scene, colors, and time_of_day in a structured format.

Best Practices for Reliability

To ensure the most deterministic and reliable structured outputs, Ollama suggests the following practices:

  • Schema Definition: Use Pydantic (Python) or Zod (JavaScript) to define response schemas.
  • Prompt Engineering: Explicitly include the phrase "return as JSON" in the prompt to guide the model.
  • Deterministic Sampling: Set the temperature to 0 to reduce variability in the output.

Future Roadmap

Ollama has outlined several upcoming enhancements to improve controlled generation:

  • Exposing logits for more granular control over generation.
  • Improving the performance and accuracy of structured outputs.
  • Implementing GPU acceleration for sampling.
  • Expanding support for formats beyond JSON schema.

Sources

Related

  • Dispatch
  • Dispatch
  • Dispatch
  • Dispatch
  • Dispatch