# Instructor

[Instructor](https://python.useinstructor.com/) is a Python library for extracting structured data from LLM responses with Pydantic models. It adds response-model validation and retry behavior on top of provider clients.

Instructor can wrap an OpenAI-compatible client that already points to AISIX. Use this setup when an application should keep Instructor's response-model workflow while routing model traffic through the gateway.

This guide uses Instructor with the OpenAI Responses API. You will configure the OpenAI Python client with an AISIX proxy URL, caller API key, and model alias, then pass it to Instructor.

## Prerequisites[​](#prerequisites "Direct link to Prerequisites")

Before starting, prepare the following:

* A Python environment supported by [Instructor](https://python.useinstructor.com/).
* A running AISIX gateway that the application can reach.
* An AISIX caller API key.
* A model alias the caller API key can access through the [Responses API](https://docs.api7.ai/ai-gateway/endpoints/responses-api.md).

If AISIX is already deployed for your organization, obtain the gateway URL, model alias, and caller API key from the team that manages it. Otherwise, follow the [AISIX OSS getting-started guide](https://docs.api7.ai/ai-gateway/getting-started/gateway-quickstart.md) or [AISIX Enterprise getting-started guide](https://docs.api7.ai/ai-gateway/getting-started/aisix-cloud-quickstart.md), or [contact API7](https://api7.ai/contact) for AISIX Cloud access.

## Configure Instructor[​](#configure-instructor "Direct link to Configure Instructor")

Install the Python packages if the application does not already include them:

```
pip install instructor openai "pydantic>=2"
```

Set the values that the application will use:

```
# AISIX_BASE_URL ends in /v1 and has no trailing slash

# The local quickstarts use http://127.0.0.1:3000/v1

export AISIX_BASE_URL="YOUR_AISIX_GATEWAY_URL/v1"

export AISIX_API_KEY="YOUR_CALLER_API_KEY"

export AISIX_MODEL="gpt-4o-prod"
```

Create an OpenAI client for AISIX, then pass it to Instructor in Responses mode:

```
import os



import instructor

from instructor import Mode

from openai import OpenAI

from pydantic import BaseModel





class Ticket(BaseModel):

  category: str

  priority: str





openai_client = OpenAI(

  base_url=os.environ["AISIX_BASE_URL"],

  api_key=os.environ["AISIX_API_KEY"],

)



client = instructor.from_openai(

  openai_client,

  mode=Mode.RESPONSES_TOOLS,

)



ticket = client.responses.create(

  model=os.environ["AISIX_MODEL"],

  response_model=Ticket,

  input="Classify this ticket: production login failures for all users.",

)



print(ticket.model_dump_json(indent=2))
```

The model value is the AISIX model alias, not the upstream provider model ID. AISIX governs the model request path: it authenticates the caller API key, resolves the alias, applies configured policies, records telemetry, and dispatches the request to the provider behind the alias. Instructor still owns schema validation and retry behavior.

Instructor also supports [Chat Completions modes](https://python.useinstructor.com/integrations/openai/) through `client.chat.completions.create(...)`. Use those modes when a workflow needs broad OpenAI-compatible chat support.

## Verify the Integration[​](#verify-the-integration "Direct link to Verify the Integration")

Run the script from the shell where the AISIX environment variables are set.

When the request succeeds, verify the following results:

* The script prints a validated Pydantic object.
* AISIX records one or more successful `POST /v1/responses` requests for the selected model alias.

Verify the request in the AISIX gateway logs. You can also use configured metrics or upstream provider logs.

If the request fails, first confirm that the caller API key can access the selected model alias and that the OpenAI client uses the AISIX `base_url`.

Instructor may retry requests when validation fails. Account for those retries when configuring rate limits and reviewing usage. With AISIX Enterprise or AISIX Cloud, include them when setting budgets. If validation keeps retrying, check whether the upstream model can satisfy the schema and whether the selected Instructor mode is supported by the AISIX route.

## Next Steps[​](#next-steps "Direct link to Next Steps")

* [Responses API](https://docs.api7.ai/ai-gateway/endpoints/responses-api.md): review gateway-facing Responses behavior.
* [OpenAI-Compatible API](https://docs.api7.ai/ai-gateway/endpoints/openai-compatible-chat.md): use Chat Completions when a workflow needs broad OpenAI-compatible support.
* [Budgets](https://docs.api7.ai/ai-gateway/traffic-controls/budgets.md): account for validation retries with control-plane spend controls.
