Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstalliTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
To extract text into data your Python code can trust, send Ollama a JSON Schema through the chat API’s format parameter, then validate the complete response with Pydantic. A prompt alone does not enforce your application’s field and type requirements, and successful validation does not prove that the extracted values are factually correct.
Define the fields your application needs
Use a Pydantic model to describe the output shape. Its field names and types become the contract your application expects; adapt them to the source text and decide explicitly how your application should handle absent or ambiguous information.
from pydantic import BaseModel
class Item(BaseModel):
name: str
quantity: int
For example, this model requires a string named name and an integer named quantity. If the source does not establish a quantity, a required integer may not represent your needs: design the model and downstream handling to match the extraction task rather than silently treating an unknown value as a fact.
Pass the schema to Ollama and validate the response
The documented Ollama Python pattern passes Item.model_json_schema() as the chat call’s format, then parses the assistant’s message content with Item.model_validate_json(). Replace your-installed-model with a model available in your Ollama environment.
#1 Best Overall
from ollama import chat
from pydantic import BaseModel
class Item(BaseModel):
name: str
quantity: int
response = chat(
model="your-installed-model",
messages=[
{
"role": "user",
"content": (
"Extract the item name and quantity from the text below. "
"Do not infer a quantity if none is stated.nn"
"Text: ..."
),
}
],
format=Item.model_json_schema(),
options={"temperature": 0},
)
item = Item.model_validate_json(response.message.content)
print(item)
This follows the official Python example’s approach; it is not a claim that this particular adapted snippet was executed. The Ollama documentation also recommends including the schema as text in the prompt to ground the response. Passing the schema through format is the machine-readable constraint; clear instructions help explain how to treat the input and uncertain values. See the Ollama structured outputs documentation and the ollama-python examples.
Temperature zero is the documented example’s setting for more deterministic output. It can reduce variability; it does not guarantee identical answers or correct extraction, so keep validation and application-level checks.
Rank #2
Choose JSON mode or a schema
Ollama supports both the string json and a JSON Schema object in format. Choose based on what the caller needs:
Free tools Windows power users keep installed
One-click scans. No signup required.
| Approach | What it requests | When it fits |
|---|---|---|
format="json" |
A valid JSON object, without specifying your application’s field-level contract. | When the caller needs JSON but does not need a declared set of properties and types. |
JSON Schema in format |
Output constrained by the supplied schema. | When code expects known fields and types; a Pydantic model can supply the schema and validate the result. |
These are response-format options, not substitutes for checking the meaning of the extracted values. The Ollama API documentation describes the format parameter and response streaming.
Validate only complete responses
The Python example above uses a complete response object. Ollama also supports streaming, where replies arrive as a sequence of response objects. Do not pass an individual partial fragment to Pydantic as though it were a finished extraction: first assemble the complete assistant content, then validate it. The API documentation covers its streaming behavior.
Handle invalid or misleading extractions
model_validate_json() checks whether the response content can be parsed into the declared model. It does not check whether the model interpreted the source correctly. Treat validation as a structural gate, then apply checks appropriate to your data and risk:
- Confirm that each extracted value is supported by the input text.
- Define and enforce how missing or ambiguous fields should be represented or handled.
- Catch parsing or validation failures so malformed or incompatible output does not flow into code that assumes valid fields.
- For consequential decisions, add human review or domain-specific verification rather than relying on schema conformance alone.
Check compatibility when the example fails
Ollama’s API and Python client syntax can change. A historical issue opened on December 7, 2024, reports a format type error with ollama-python 0.4.3; that report is evidence of a past compatibility problem, not proof of a current minimum version or ongoing defect. If a copied call fails, check the current structured outputs documentation and the historical issue report against the client and Ollama versions you are actually running.
Ollama’s rolling documentation states that Ollama Cloud currently does not support structured outputs. Because this capability statement can change, verify the current documentation before designing a Cloud deployment around it.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

