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.

Extract validated, machine-readable structured data from models using native JSON mode or strict schema enforcement with Instructor and Pydantic across any OpenAI-compatible client library.

Methods

JSON Mode

Pass response_format: {"type": "json_object"} to constrain completions to valid JSON:

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 items as JSON."}], "response_format": {"type": "json_object"}}'

Strict Schemas with Instructor

Instructor wraps clients to enforce Pydantic models with auto-retries:

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

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

Calls route through https://api.nrouter.ai/v1 and settle at list price via x-nr-request-cost. Related guides: Tool Calling, Create Embeddings, and Reasoning Models.

Frequently asked questions

Does JSON mode guarantee my fields are present?

No; JSON mode guarantees valid syntax only, while Instructor with Pydantic enforces required keys and types.

Can structured output stream?

Yes; Instructor supports streamed structured responses with validation retries. See the Instructor guide.

Does structured output change what a request costs?

No; structured requests settle at the served model's exact list price reported under x-nr-request-cost.

Was this page helpful?