FrameworksInstructor
Instructor
Use nRouter with Instructor for structured outputs and Pydantic validation
Last updated
Instructor adds structured data extraction and schema enforcement on top of OpenAI-compatible APIs using Python and Pydantic. Point it at https://api.nrouter.ai/v1 — guardrails, caching, and rate-limits auto-apply to every structured extraction.
Installation
pip install instructor openai pydanticSetup
import os
from pydantic import BaseModel, Field
from openai import OpenAI
import instructor
# Initialize patched OpenAI client targeting nRouter
client = instructor.from_openai(
OpenAI(
base_url="https://api.nrouter.ai/v1",
api_key=os.environ["NROUTER_API_KEY"],
)
)Structured Extraction
class UserProfile(BaseModel):
name: str
skills: list[str] = Field(description="Core technical capabilities")
experience_level: str = Field(description="Junior, Mid, Senior, or Staff")
response = client.chat.completions.create(
model="gpt-5.4-mini",
response_model=UserProfile,
max_completion_tokens=1024,
messages=[
{
"role": "user",
"content": "Jason has 8 years of experience building distributed systems in Rust and Go.",
}
],
)
print(response.name) # Jason
print(response.experience_level) # Senior
print(response.skills) # ['Rust', 'Go', 'distributed systems']Guardrails & Cost Tracking
Because nRouter intercepts requests at the gateway:
- Server-side Guardrails: Any prompt containing PII or injection attempts is blocked before reaching the model.
- Cost Header: Responses carry
x-nr-request-costwith the exact dollar amount calculated for the call. - Semantic Caching: Identical extraction requests return instantly from cache when enabled.
Was this page helpful?