Extract structured data with a local model
Use gpt-oss 20B through Ollama to turn a short text sample into validated JSON fields, then check the output against the source.
Before you begin
- Difficulty
- Intermediate
- Software
- Ollama, Python, Pydantic
- Hardware
- Ollama lists a 14 GB gpt-oss 20B package. Leave additional GPU or unified memory for the runtime, context, and validation process.
Sources and files
Publisher gpt-oss local instructions Ollama JSON schema and validation examplesChoose a model for this task
The steps below use the recommended model. Alternatives have their own package and command; open their model pages before switching.
The workflow
Install the local model runtime
Install Ollama, start its service, and use the publisher-documented 20B package. The first run downloads the weights.
ollama run gpt-oss:20bInstall client and validator
Create a Python environment and install the Ollama client plus Pydantic for schema validation.
pip install ollama pydanticDefine your output schema
Start with a small object, such as a name, date, and list of items from a short sample. Use a Pydantic BaseModel and make fields optional only when they truly may be absent.
Request schema-constrained JSON
Save this source-derived example as extract.py, then run python extract.py. The format parameter constrains the response and Pydantic validates it. Replace the sample only after checking its output.
from ollama import chat
from pydantic import BaseModel
class Item(BaseModel):
name: str
price: float
class Receipt(BaseModel):
store: str
items: list[Item]
sample = "Corner Cafe receipt: Coffee $3.00, Tea $2.00."
response = chat(
model="gpt-oss:20b",
messages=[{"role": "user", "content": "Extract the store and items: " + sample}],
format=Receipt.model_json_schema(),
options={"temperature": 0},
)
receipt = Receipt.model_validate_json(response.message.content)
print(receipt.model_dump_json(indent=2))Validate and compare
Parse response.message.content with your Pydantic model_validate_json() method. Compare every extracted field to the source text; JSON validity does not prove factual accuracy.
Run your real document sample
Only after the small example passes, replace it with a document you are allowed to process. Keep the endpoint local, record failed validations, and review ambiguous fields manually.
When it doesn’t go to plan
Use model_json_schema() in the format field, lower temperature, and validate the JSON with Pydantic as the official guide demonstrates.
Shorten the input, quote the exact source span in the prompt, and review ambiguous values. Structured output constrains shape, not truth.
The model behind this workflow
gpt-oss · 20BWill it run on your machine?
Save your machine to see a personalized rating and its reasoning.
Add my hardwareKeep exploring
Set up a private AI chat assistant
Install a local runtime, run Qwen3.5 9B, confirm responses, and know when to choose the smaller 4B package.
Build a private local coding assistant
Connect an open coding-capable model in Ollama to Cline, run a small repository task, and review the result locally.
Generate images on your own GPU
Use the official FLUX.2 Klein 4B ComfyUI template with exact model files, a first prompt, and an output check.