Configuration options for creating and customizing Azure OpenAI provider instances. Provides flexible authentication and connection settings for different Azure OpenAI deployment scenarios.
Factory function for creating customized Azure OpenAI provider instances with specific configuration options.
/**
* Create an Azure OpenAI provider instance with custom configuration
* @param options - Configuration options for the provider
* @returns Configured Azure OpenAI provider instance
*/
function createAzure(options?: AzureOpenAIProviderSettings): AzureOpenAIProvider;Usage Examples:
import { createAzure } from "@ai-sdk/azure";
// Basic configuration with resource name
const azure = createAzure({
resourceName: "my-azure-openai-resource",
apiKey: "your-api-key-here",
});
// Configuration with custom base URL
const azureProxy = createAzure({
baseURL: "https://my-proxy.company.com/openai",
apiKey: "your-api-key-here",
});
// Configuration with custom headers and API version
const azureCustom = createAzure({
resourceName: "production-resource",
apiKey: "prod-api-key",
apiVersion: "2024-08-01-preview",
headers: {
"X-Custom-Header": "my-value",
"User-Agent": "MyApp/1.0",
},
});
// Use the configured provider
const model = azure("gpt-4o");Pre-configured provider instance that uses environment variables for configuration.
/**
* Default Azure OpenAI provider instance configured via environment variables
* Uses AZURE_API_KEY and AZURE_RESOURCE_NAME environment variables
*/
const azure: AzureOpenAIProvider;Usage Example:
import { azure } from "@ai-sdk/azure";
import { generateText } from "ai";
// Requires AZURE_API_KEY and AZURE_RESOURCE_NAME environment variables
const { text } = await generateText({
model: azure("gpt-4o"),
prompt: "Hello, world!",
});interface AzureOpenAIProviderSettings {
/**
* Name of the Azure OpenAI resource. Either this or baseURL can be used.
* The resource name is used in the assembled URL:
* https://{resourceName}.openai.azure.com/openai/deployments/{modelId}{path}
*/
resourceName?: string;
/**
* Use a different URL prefix for API calls, e.g. to use proxy servers.
* Either this or resourceName can be used. When a baseURL is provided,
* the resourceName is ignored. With a baseURL, the resolved URL is
* {baseURL}/{modelId}{path}
*/
baseURL?: string;
/**
* API key for authenticating requests
*/
apiKey?: string;
/**
* Custom headers to include in the requests
*/
headers?: Record<string, string>;
/**
* Custom fetch implementation. You can use it as a middleware to
* intercept requests, or to provide a custom fetch implementation
* for e.g. testing
*/
fetch?: FetchFunction;
/**
* Custom api version to use. Defaults to '2025-03-01-preview'
*/
apiVersion?: string;
}
type FetchFunction = typeof globalThis.fetch;# Set environment variables for default provider
export AZURE_API_KEY="your-azure-openai-api-key"
export AZURE_RESOURCE_NAME="your-azure-resource-name"import { azure } from "@ai-sdk/azure";
// Uses environment variables automatically
const model = azure("gpt-4o");import { createAzure } from "@ai-sdk/azure";
const azure = createAzure({
resourceName: "my-openai-resource",
apiKey: "sk-...",
});
// This will make requests to:
// https://my-openai-resource.openai.azure.com/openai/deployments/gpt-4o/chat/completions
const model = azure("gpt-4o");import { createAzure } from "@ai-sdk/azure";
// For proxy or custom endpoints
const azure = createAzure({
baseURL: "https://api.company.com/azure-openai",
apiKey: "sk-...",
});
// This will make requests to:
// https://api.company.com/azure-openai/gpt-4o/chat/completions
const model = azure("gpt-4o");import { createAzure } from "@ai-sdk/azure";
const azure = createAzure({
resourceName: "my-resource",
apiKey: "sk-...",
apiVersion: "2024-08-01-preview", // Use specific API version
});
const model = azure("gpt-4o");import { createAzure } from "@ai-sdk/azure";
const azure = createAzure({
resourceName: "my-resource",
apiKey: "sk-...",
headers: {
"X-Request-ID": "unique-request-id",
"X-Environment": "production",
"User-Agent": "MyApp/2.1.0",
},
});
const model = azure("gpt-4o");import { createAzure } from "@ai-sdk/azure";
// Custom fetch for logging and monitoring
const customFetch: FetchFunction = async (url, options) => {
console.log("Making request to:", url);
console.log("Request options:", options);
const startTime = Date.now();
const response = await fetch(url, options);
const endTime = Date.now();
console.log("Request completed in:", endTime - startTime, "ms");
console.log("Response status:", response.status);
return response;
};
const azure = createAzure({
resourceName: "my-resource",
apiKey: "sk-...",
fetch: customFetch,
});
const model = azure("gpt-4o");import { createAzure } from "@ai-sdk/azure";
// Mock fetch for testing
const mockFetch: FetchFunction = async (url, options) => {
return new Response(JSON.stringify({
choices: [{ message: { content: "Mocked response" } }]
}), {
status: 200,
headers: { "Content-Type": "application/json" },
});
};
const testAzure = createAzure({
resourceName: "test-resource",
apiKey: "test-key",
fetch: mockFetch,
});
// Use in tests
const model = testAzure("gpt-4o");import { createAzure } from "@ai-sdk/azure";
const environments = {
development: createAzure({
resourceName: "dev-openai-resource",
apiKey: process.env.DEV_AZURE_API_KEY,
apiVersion: "2024-08-01-preview",
}),
staging: createAzure({
resourceName: "staging-openai-resource",
apiKey: process.env.STAGING_AZURE_API_KEY,
apiVersion: "2025-03-01-preview",
}),
production: createAzure({
resourceName: "prod-openai-resource",
apiKey: process.env.PROD_AZURE_API_KEY,
apiVersion: "2025-03-01-preview",
headers: {
"X-Environment": "production",
},
}),
};
// Select environment-specific provider
const env = process.env.NODE_ENV || "development";
const azure = environments[env as keyof typeof environments];
const model = azure("gpt-4o");const azure = createAzure({
resourceName: "my-resource",
apiKey: "your-api-key", // Direct API key
});
// Or via environment variable
const azureEnv = createAzure({
resourceName: "my-resource",
// apiKey will be loaded from AZURE_API_KEY environment variable
});The provider automatically loads configuration from environment variables when not explicitly provided:
AZURE_API_KEY - API key for authenticationAZURE_RESOURCE_NAME - Azure OpenAI resource name// Automatically uses AZURE_API_KEY and AZURE_RESOURCE_NAME
const azure = createAzure();
// Equivalent to the default exported instance
import { azure } from "@ai-sdk/azure";The provider constructs URLs differently based on configuration:
https://{resourceName}.openai.azure.com/openai/deployments/{deploymentId}{path}?api-version={apiVersion}{baseURL}/{deploymentId}{path}?api-version={apiVersion}https://{resourceName}.openai.azure.com/openai/responses?api-version={apiVersion}import { createAzure } from "@ai-sdk/azure";
import { generateText } from "ai";
const azure = createAzure({
resourceName: "my-resource",
apiKey: "invalid-key", // This will cause authentication errors
});
try {
const { text } = await generateText({
model: azure("gpt-4o"),
prompt: "Hello",
});
} catch (error) {
console.error("Configuration error:", error);
// Handle authentication, network, or configuration errors
}