8000 feat: add Azure OpenAI support to auto_client.py by jxnl · Pull Request #1633 · 567-labs/instructor · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add Azure OpenAI support to auto_client.py #1633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions docs/integrations/azure.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,38 @@ client = AzureOpenAI(
client = instructor.from_openai(client)
```

## Using Auto Client (Recommended)

The easiest way to get started with Azure OpenAI is using the `from_provider` method:

```python
import instructor
import os

# Set your Azure OpenAI credentials
os.environ["AZURE_OPENAI_API_KEY"] = "your-api-key"
os.environ["AZURE_OPENAI_ENDPOINT"] = "https://your-resource.openai.azure.com/"

# Create client using the provider string
client = instructor.from_provider("azure_openai/gpt-4o-mini")

# Or async client
async_client = instructor.from_provider("azure_openai/gpt-4o-mini", async_client=True)
```

You can also pass credentials as parameters:

```python
import instructor

client = instructor.from_provider(
"azure_openai/gpt-4o-mini",
api_key="your-api-key",
azure_endpoint="https://your-resource.openai.azure.com/",
api_version="2024-02-01" # Optional, defaults to 2024-02-01
)
```

## Basic Usage

Here's a simple example using a Pydantic model:
Expand Down
55 changes: 55 additions & 0 deletions instructor/auto_client.py
10000
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# List of supported providers
supported_providers = [
"openai",
"azure_openai",
"anthropic",
"google",
"mistral",
Expand Down Expand Up @@ -82,10 +83,12 @@ def from_provider(
>>> import instructor
>>> # Sync clients
>>> client = instructor.from_provider("openai/gpt-4")
>>> client = instructor.from_provider("azure_openai/gpt-4")
>>> client = instructor.from_provider("anthropic/claude-3-sonnet")
>>> client = instructor.from_provider("ollama/llama2")
>>> # Async clients
>>> async_client = instructor.from_provider("openai/gpt-4", async_client=True)
>>> async_client = instructor.from_provider("azure_openai/gpt-4", async_client=True)
"""
try:
provider, model_name = model.split("/", 1)
Expand Down Expand Up @@ -117,6 +120,58 @@ def from_provider(
"Install it with `pip install openai`."
) from None

elif provider == "azure_openai":
try:
import os
from openai import AzureOpenAI, AsyncAzureOpenAI
from instructor import from_openai

# Get required Azure OpenAI configuration from environment
api_key = kwargs.pop("api_key", os.environ.get("AZURE_OPENAI_API_KEY"))
azure_endpoint = kwargs.pop("azure_endpoint", os.environ.get("AZURE_OPENAI_ENDPOINT"))
api_version = kwargs.pop("api_version", "2024-02-01")

if not api_key:
from instructor.exceptions import ConfigurationError
raise ConfigurationError(
"AZURE_OPENAI_API_KEY is not set. "
"Set it with `export AZURE_OPENAI_API_KEY=<your-api-key>` or pass it as kwarg api_key=<your-api-key>"
)

if not azure_endpoint:
from instructor.exceptions import ConfigurationError
raise ConfigurationError(
"AZURE_OPENAI_ENDPOINT is not set. "
"Set it with `export AZURE_OPENAI_ENDPOINT=<your-endpoint>` or pass it as kwarg azure_endpoint=<your-endpoint>"
)

client = (
AsyncAzureOpenAI(
api_key=api_key,
api_version=api_version,
azure_endpoint=azure_endpoint,
)
if async_client
else AzureOpenAI(
api_key=api_key,
api_version=api_version,
azure_endpoint=azure_endpoint,
)
)
return from_openai(
client,
model=model_name,
mode=mode if mode else instructor.Mode.TOOLS,
**kwargs,
)
except ImportError:
from instructor.exceptions import ConfigurationError

raise ConfigurationError(
"The openai package is required to use the Azure OpenAI provider. "
"Install it with `pip install openai`."
) from None

elif provider == "anthropic":
try:
import anthropic
Expand Down
1 change: 1 addition & 0 deletions tests/test_auto_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class User(BaseModel):
"anthropic/claude-3-5-haiku-latest",
"google/gemini-2.0-flash",
"openai/gpt-4o-mini",
"azure_openai/gpt-4o-mini",
"mistral/ministral-8b-latest",
"cohere/command-r-plus",
"perplexity/sonar-pro",
Expand Down
Loading
0