Instructor
Instructor wraps the OpenAI client and returns a validated Pydantic object.
Connect
py
def plugboard() -> OpenAI:
return OpenAI(base_url=os.environ["PLUGBOARD_BASE_URL"], api_key=os.environ["PLUGBOARD_KEY"], max_retries=0)Modes
Mode.TOOLS (a tool built from your model, forced with tool_choice):
py
client = instructor.from_openai(plugboard(), mode=instructor.Mode.TOOLS)
meal = client.chat.completions.create(model=model, response_model=Meal, messages=[{"role": "user", "content": PROMPT}])Mode.JSON_SCHEMA (a json_schema response format):
py
client = instructor.from_openai(plugboard(), mode=instructor.Mode.JSON_SCHEMA)
meal = client.chat.completions.create(model=model, response_model=Meal, messages=[{"role": "user", "content": PROMPT}])Partial streaming with create_partial, which yields growing objects:
py
client = instructor.from_openai(plugboard(), mode=instructor.Mode.TOOLS)
partials = list(client.chat.completions.create_partial(model=model, response_model=Meal, messages=[{"role": "user", "content": PROMPT}]))
last = partials[-1]Limits
- The schema is not enforced by the provider. Keep
max_retriesabove 0 so Instructor retries an answer that fails validation. - With both a
toolslist and aresponse_model, the model may call one of your tools instead of giving the structured answer.