Agents / Intermediate

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.

Verified sourceSource checked 9/23/2026
LOCALRENTED GPU

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.
View the setup source

Sources and files

Publisher gpt-oss local instructions Ollama JSON schema and validation examples

Choose 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

1

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:20b
2

Install client and validator

Create a Python environment and install the Ollama client plus Pydantic for schema validation.

pip install ollama pydantic
3

Define 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.

4

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))
5

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.

6

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

The response does not match the schema

Use model_json_schema() in the format field, lower temperature, and validate the JSON with Pydantic as the official guide demonstrates.

The JSON is valid but fields are wrong

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 · 20B
MY HARDWARE

Will it run on your machine?

Save your machine to see a personalized rating and its reasoning.

Add my hardware

Keep exploring