Skip to main content

Overview

Azure OpenAI Service provides the same powerful OpenAI models (GPT-4o, GPT-4o-mini) through Microsoft’s Azure cloud platform, offering:
  • Enterprise Security: Azure’s compliance and security features
  • Private Network: Deploy within your Azure virtual network
  • SLA Guarantees: 99.9% uptime commitment
  • Data Residency: Keep data within specific regions
  • Cost Management: Azure billing and cost controls
Perfect for enterprise deployments requiring compliance, security, and governance.

Prerequisites

1

Azure Account

Create an Azure account at portal.azure.com
2

Create Azure OpenAI Resource

  1. Go to Azure Portal
  2. Search for “Azure OpenAI”
  3. Click “Create”
  4. Select subscription, resource group, and region
  5. Wait for deployment to complete
3

Deploy a Model

  1. Go to your Azure OpenAI resource
  2. Navigate to “Model deployments”
  3. Click “Create new deployment”
  4. Select model (e.g., gpt-4o-mini)
  5. Give it a deployment name (e.g., gpt-4o-mini-deployment)
4

Get Credentials

From Azure OpenAI resource overview:
  • Endpoint: https://YOUR-RESOURCE-NAME.openai.azure.com/
  • API Key: Found in “Keys and Endpoint” section
  • API Version: e.g., 2024-02-15-preview
5

Install ScrapeGraphAI

pip install scrapegraphai
playwright install

Basic Configuration

import os
from dotenv import load_dotenv
from scrapegraphai.graphs import SmartScraperGraph

load_dotenv()

graph_config = {
    "llm": {
        "api_key": os.getenv("AZURE_OPENAI_API_KEY"),
        "model": "azure_openai/gpt-4o-mini",
        "api_version": "2024-02-15-preview",
        "azure_endpoint": os.getenv("AZURE_OPENAI_ENDPOINT"),
        "deployment_name": "gpt-4o-mini-deployment",
    },
    "verbose": True,
    "headless": False,
}

smart_scraper_graph = SmartScraperGraph(
    prompt="Extract the main article content",
    source="https://www.wired.com",
    config=graph_config,
)

result = smart_scraper_graph.run()
print(result)

Environment Variables

Create a .env file:
.env
AZURE_OPENAI_API_KEY=your-azure-api-key
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com/
AZURE_OPENAI_DEPLOYMENT=gpt-4o-mini-deployment
AZURE_OPENAI_API_VERSION=2024-02-15-preview
Then load in your code:
import os
from dotenv import load_dotenv

load_dotenv()

graph_config = {
    "llm": {
        "api_key": os.getenv("AZURE_OPENAI_API_KEY"),
        "model": "azure_openai/gpt-4o-mini",
        "api_version": os.getenv("AZURE_OPENAI_API_VERSION"),
        "azure_endpoint": os.getenv("AZURE_OPENAI_ENDPOINT"),
        "deployment_name": os.getenv("AZURE_OPENAI_DEPLOYMENT"),
    },
}
Always use environment variables for credentials - never hardcode them in your source code.

Available Models

Configuration Options

Required Parameters

graph_config = {
    "llm": {
        # Required
        "api_key": "your-azure-api-key",
        "model": "azure_openai/gpt-4o-mini",
        "azure_endpoint": "https://your-resource.openai.azure.com/",
        "deployment_name": "your-deployment-name",
        "api_version": "2024-02-15-preview",
    },
}
The deployment_name is the name you gave when deploying the model in Azure Portal, NOT the model name itself.

Optional Parameters

graph_config = {
    "llm": {
        "api_key": os.getenv("AZURE_OPENAI_API_KEY"),
        "model": "azure_openai/gpt-4o-mini",
        "azure_endpoint": os.getenv("AZURE_OPENAI_ENDPOINT"),
        "deployment_name": "gpt-4o-mini",
        "api_version": "2024-02-15-preview",
        
        # Optional
        "temperature": 0,  # Deterministic output
        "max_tokens": 4000,  # Limit response length
        "top_p": 1.0,  # Nucleus sampling
        "frequency_penalty": 0,  # Reduce repetition
        "presence_penalty": 0,  # Encourage topic diversity
    },
}

Authentication Methods

Complete Examples

import os
import json
from dotenv import load_dotenv
from scrapegraphai.graphs import SmartScraperGraph
from scrapegraphai.utils import prettify_exec_info

load_dotenv()

graph_config = {
    "llm": {
        "api_key": os.getenv("AZURE_OPENAI_API_KEY"),
        "model": "azure_openai/gpt-4o-mini",
        "api_version": "2024-02-15-preview",
        "azure_endpoint": os.getenv("AZURE_OPENAI_ENDPOINT"),
        "deployment_name": os.getenv("AZURE_OPENAI_DEPLOYMENT"),
        "temperature": 0,
    },
    "verbose": True,
    "headless": False,
}

smart_scraper = SmartScraperGraph(
    prompt="Extract the main article with title and author",
    source="https://www.wired.com",
    config=graph_config,
)

result = smart_scraper.run()
print(json.dumps(result, indent=4))

graph_exec_info = smart_scraper.get_execution_info()
print(prettify_exec_info(graph_exec_info))

Troubleshooting

Error: DeploymentNotFound: The API deployment for this resource does not existSolution:
  1. Check deployment name in Azure Portal
  2. Ensure it matches exactly (case-sensitive)
  3. Verify deployment is in “Succeeded” state
# Correct
"deployment_name": "gpt-4o-mini-deployment"  # As shown in Portal

# Wrong
"deployment_name": "gpt-4o-mini"  # Model name, not deployment name
Error: InvalidApiVersion: The API version is not supportedSolution: Use a valid API version:
"api_version": "2024-02-15-preview"  # Current stable version
Check Azure OpenAI API versions for latest.
Error: RateLimitError: Requests to the API are being rate limitedSolution:
  1. Increase quota in Azure Portal
  2. Add retry logic with backoff
  3. Distribute load across multiple deployments
import time
from tenacity import retry, wait_exponential, stop_after_attempt

@retry(wait=wait_exponential(min=1, max=60), stop=stop_after_attempt(5))
def scrape_with_retry(url):
    return scraper.run()
Error: AuthenticationError: Access deniedSolution:
  1. Verify API key is correct
  2. Check key hasn’t expired
  3. Ensure resource is not paused/deleted
  4. Verify network access (if using Private Endpoint)

Best Practices

Use Key Vault

Store credentials in Azure Key Vault:
from azure.keyvault.secrets import SecretClient
from azure.identity import DefaultAzureCredential

client = SecretClient(
    vault_url="https://myvault.vault.azure.net/",
    credential=DefaultAzureCredential()
)

api_key = client.get_secret("azure-openai-key").value

Multi-Region Deployment

Deploy to multiple regions for high availability:
  • Primary: East US
  • Failover: West Europe
  • Backup: Southeast Asia

Monitor Usage

Use Azure Monitor to track:
  • Token usage
  • Request latency
  • Error rates
  • Cost trends

Cost Management

Implement cost controls:
  • Set budget alerts
  • Use gpt-4o-mini for most tasks
  • Cache common responses
  • Monitor token usage

Regional Considerations

Model availability varies by Azure region. Popular regions for Azure OpenAI:
  • East US: Best availability, all models
  • West Europe: GDPR compliance
  • UK South: UK data residency
  • Australia East: APAC customers
  • Canada Central: Canadian data residency

Next Steps

Advanced Configuration

Learn about proxy rotation and browser settings

OpenAI

Compare with standard OpenAI API

Build docs developers (and LLMs) love