Browse documentation

Get Structured Output

Get machine-readable answers from a model: JSON mode with response_format for JSON text, or Instructor with a Pydantic model for strict, validated output.

When a model's answer feeds code — an extraction pipeline, a classifier, a form filler — prose is the wrong shape. nRouter offers two documented routes to structured answers: JSON mode on the request itself, and strict schema enforcement through Instructor for Python.

JSON mode

Set response_format to {"type": "json_object"} on a chat request and the model answers in JSON — the shape documented in the cURL reference:

curl https://api.nrouter.ai/v1/chat/completions \
  -H "Authorization: Bearer $NROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.5",
    "messages": [
      {"role": "user", "content": "List the items in this order as JSON: two cables, one adapter."}
    ],
    "response_format": {"type": "json_object"}
  }'

JSON mode controls the format of the answer. It does not by itself guarantee the JSON carries the fields your code expects — that is what the second route adds.

Strict schemas with Instructor

Instructor wraps the OpenAI-compatible client and enforces a Pydantic model on the answer. You define the shape as a Python class, pass it as response_model, and get a validated object back — with automated retries when an answer does not fit the schema:

class UserProfile(BaseModel):
    name: str
    age: int
    occupation: str

profile = client.chat.completions.create(
    model="gpt-5.5",
    response_model=UserProfile,
    messages=[
        {"role": "user", "content": "Extract: Maria, 34, is a civil engineer."}
    ],
)

Instructor points at the same https://api.nrouter.ai/v1 base URL as every other client, so budgets, guardrails, and per-request cost attribution apply to structured calls exactly as they do to plain ones.

Which route to choose

NeedRoute
JSON text my own code will parse and checkJSON mode — one field on the request
A guaranteed shape, typed values, automatic retriesInstructor with a Pydantic response_model

Frequently asked questions

Was this page helpful?