Understand the request and response
An API defines how software requests an operation and receives a result. HTTP commonly carries the request and response, while JSON represents structured data such as named fields and arrays. This separates an application's interface from the model behind it. [source]
A model returning valid JSON has satisfied a formatting condition. The values inside it may still be wrong. Treat syntax validation, schema validation, and source verification as separate stages.
Prepare a local smoke test
First complete the local-model lesson. You need a running Ollama service and a downloaded local model. Copy its exact tag from ollama ls. Save the script below as local_request.py. Replace YOUR_LOCAL_MODEL_TAG before running it with Python 3.
The script sends a fictional sentence only to the loopback address. It does not upload a document or call a cloud API.
import json
from urllib.request import Request, urlopen
payload = {
"model": "YOUR_LOCAL_MODEL_TAG",
"prompt": "Summarize: Delivery is proposed for 20 March, subject to inspection.",
"stream": False
}
request = Request(
"http://127.0.0.1:11434/api/generate",
data=json.dumps(payload).encode("utf-8"),
headers={"Content-Type": "application/json"},
method="POST"
)
with urlopen(request, timeout=120) as response:
result = json.load(response)
print(result["response"])Python's JSON module handles encoding and decoding; urllib.request sends the request. Ollama documents its local API and streaming behavior. Disabling streaming here lets the introductory example read one response object. [source] [source] [source]
Inspect the layers
The payload defines the model, input, and response mode. The request defines the address, method, headers, and body. The response object contains the model's generated text and other fields. Printing response is different from validating its substantive content.
If the connection is refused, check whether the local service is running and whether its configured address differs. If the model is missing, check its tag and download status. If the request times out, inspect the service before retrying repeatedly. The example is a learning script, not a production retry policy.
Design a structured extraction
For a chronology, you might request fields for document ID, event date, source passage, and uncertainty. Validate that required fields exist and that dates have the permitted form. Then compare the event and passage with the original. A field called verified: true is a model-generated claim unless an actual verification process sets it.
Try it: a perfect schema
Output: {"event_date":"2026-03-20","status":"completed"}. The source says only “inspection proposed for 20 March.” What passed, and what failed?
Check your understanding
The object may be valid JSON and match a schema. The event status is unsupported. The review should reject or correct it. Structured output makes validation easier to automate; it does not turn a prediction into an established event.