From 29c85ad2c2cf71aa6fede6fa195cf9c5ef479309 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <209825114+claude[bot]@users.noreply.github.com> Date: Fri, 20 Jun 2025 17:20:41 +0000 Subject: [PATCH] feat: add Azure OpenAI support to auto_client.py - Add azure_openai provider to supported_providers list - Implement Azure OpenAI client creation with environment variable support - Support both AZURE_OPENAI_API_KEY and AZURE_OPENAI_ENDPOINT env vars - Add azure_openai/model format support for from_provider() function - Update tests to include azure_openai provider - Update Azure documentation with auto_client usage examples Resolves #1622 Co-authored-by: Jason Liu --- docs/integrations/azure.md | 32 ++++++++++++++++++++++ instructor/auto_client.py | 55 ++++++++++++++++++++++++++++++++++++++ tests/test_auto_client.py | 1 + 3 files changed, 88 insertions(+) diff --git a/docs/integrations/azure.md b/docs/integrations/azure.md index c3742f29d..7d3553f5c 100644 --- a/docs/integrations/azure.md +++ b/docs/integrations/azure.md @@ -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: diff --git a/instructor/auto_client.py b/instructor/auto_client.py index 68d3097c8..8552a9868 100644 --- a/instructor/auto_client.py +++ b/instructor/auto_client.py @@ -11,6 +11,7 @@ # List of supported providers supported_providers = [ "openai", + "azure_openai", "anthropic", "google", "mistral", @@ -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) @@ -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=` or pass it as kwarg 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=` or pass it as kwarg azure_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 diff --git a/tests/test_auto_client.py b/tests/test_auto_client.py index 4d8300f00..753fab177 100644 --- a/tests/test_auto_client.py +++ b/tests/test_auto_client.py @@ -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",