Skip to main content

serapeum azure openai integration

Project description

Serapeum Azure OpenAI Provider

Azure OpenAI integration for the Serapeum LLM framework

The serapeum-azure-openai package extends serapeum-openai to work with Azure OpenAI Service deployments. It provides:

  • Azure Authentication: API key and Microsoft Entra ID (Azure AD) support
  • Deployment Management: Routes requests to your named Azure deployments
  • Full Feature Parity: Uses the same chat, streaming, tool calling, and structured output capabilities as the OpenAI provider via shared base classes

The public classes are Completions and Responses, which are built from an AzureClient mixin combined with the corresponding serapeum.openai base classes (OpenAICompletions and OpenAIResponses), so all OpenAI provider features work transparently with Azure deployments.

Installation

From Source

cd libs/providers/azure-openai
uv sync --active

From PyPI (when published)

pip install serapeum-azure-openai

Prerequisites

  1. An Azure OpenAI resource with at least one model deployment
  2. Either an API key or Microsoft Entra ID credentials

Set the required environment variables:

export AZURE_OPENAI_API_KEY="your-azure-api-key"
export AZURE_OPENAI_ENDPOINT="https://YOUR_RESOURCE.openai.azure.com/"
export OPENAI_API_VERSION="2024-02-01"

Quick Start

API Key Authentication

from serapeum.azure_openai import Completions
from serapeum.core.llms import Message, MessageRole

llm = Completions(
    engine="my-gpt4o-deployment",
    model="gpt-4o",
    api_key="your-azure-api-key",
    azure_endpoint="https://YOUR_RESOURCE.openai.azure.com/",
    api_version="2024-02-01",
)

messages = [
    Message(role=MessageRole.USER, content="Explain quantum computing in one sentence.")
]
response = llm.chat(messages)
print(response.message.content)

Using the Responses API

from serapeum.azure_openai import Responses

llm = Responses(
    engine="my-gpt4o-deployment",
    model="gpt-4o",
    api_key="your-azure-api-key",
    azure_endpoint="https://YOUR_RESOURCE.openai.azure.com/",
    api_version="2024-02-01",
)

Microsoft Entra ID (Azure AD) Authentication

from azure.identity import DefaultAzureCredential, get_bearer_token_provider
from serapeum.azure_openai import Completions

credential = DefaultAzureCredential()
token_provider = get_bearer_token_provider(
    credential, "https://cognitiveservices.azure.com/.default"
)

llm = Completions(
    engine="my-gpt4o-deployment",
    model="gpt-4o",
    azure_ad_token_provider=token_provider,
    use_azure_ad=True,
    azure_endpoint="https://YOUR_RESOURCE.openai.azure.com/",
    api_version="2024-02-01",
)

Features

Since Completions and Responses share the same base classes as the OpenAI provider, all features from serapeum-openai are available. See the OpenAI provider README for full usage examples of:

  • Streaming (sync and async)
  • Tool calling
  • Structured outputs with Pydantic models
  • Completion-style usage with prompt templates

The only difference is initialization: use Completions or Responses with your deployment name and Azure endpoint instead of the OpenAI equivalents with an API key.

Configuration

from serapeum.azure_openai import Completions

llm = Completions(
    engine="my-deployment",                # Required: Azure deployment name
    model="gpt-4o",                        # Required: model name
    azure_endpoint="https://...",          # Azure resource endpoint
    api_key="...",                         # Azure API key (or env var)
    api_version="2024-02-01",             # Required: API version
    use_azure_ad=False,                    # Use Entra ID authentication
    azure_ad_token_provider=None,          # Custom token provider callable
    temperature=0.7,                       # Sampling temperature
    max_tokens=1024,                       # Max tokens to generate
    timeout=60.0,                          # Request timeout
    additional_kwargs={},                  # Extra API parameters
)

The engine parameter accepts several aliases: deployment_name, deployment_id, deployment, or azure_deployment.

Class Hierarchy

AzureClient (mixin)
├── Completions(AzureClient, OpenAICompletions)  ─── Chat Completions API
└── Responses(AzureClient, OpenAIResponses)      ─── Responses API

AzureClient handles Azure-specific fields (engine, azure_endpoint, use_azure_ad, azure_ad_token_provider) and overrides SDK client construction to target Azure OpenAI endpoints.

Package Layout

src/serapeum/azure_openai/
├── __init__.py    # Lazy imports (AzureClient, Completions, Responses, SyncAzureOpenAI, AsyncAzureOpenAI)
├── llm.py         # AzureClient mixin, Completions, and Responses classes
└── utils.py       # Azure AD token refresh, alias resolution

Testing

# All tests (from repo root)
python -m pytest libs/providers/azure-openai/tests

# Skip end-to-end tests
python -m pytest libs/providers/azure-openai/tests -m "not e2e"

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

serapeum_azure_openai-0.1.0.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

serapeum_azure_openai-0.1.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file serapeum_azure_openai-0.1.0.tar.gz.

File metadata

  • Download URL: serapeum_azure_openai-0.1.0.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for serapeum_azure_openai-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9a0f8d11bb138bdd5f97f47244b78b42fbc24da9a7720abb109571d016212322
MD5 38b30de078b34b960493d393b9f8a261
BLAKE2b-256 1ee79f0c00fb58ae7345251ca3076bfcb9761691be9fa8cd585a1882f370d33b

See more details on using hashes here.

File details

Details for the file serapeum_azure_openai-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for serapeum_azure_openai-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3e03e2265ccda1f56a460a69b0c761cf69e0861b84654aa353d092d76b1285ab
MD5 8cdbfa3fed08efc7b2389328e0a68fed
BLAKE2b-256 7f45359ab290e5830d70346e7925336d8eac40b646e8687f8f185a56ed995559

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page