Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/openai/openai-python/llms.txt

Use this file to discover all available pages before exploring further.

The OpenAI Python SDK uses httpx for HTTP requests, which provides comprehensive proxy support.

Basic Proxy Configuration

The simplest way to configure a proxy is by customizing the HTTP client:
import httpx
from openai import OpenAI, DefaultHttpxClient

client = OpenAI(
    http_client=DefaultHttpxClient(
        proxy="http://my.proxy.example.com:8080",
    ),
)

Environment Variables

You can also use environment variables to configure proxies globally:
export HTTP_PROXY="http://my.proxy.example.com:8080"
export HTTPS_PROXY="http://my.proxy.example.com:8080"
The httpx client will automatically use these environment variables:
from openai import OpenAI

client = OpenAI()
# Proxy is automatically configured from environment variables

Authenticated Proxies

For proxies requiring authentication, include credentials in the proxy URL:
import httpx
from openai import OpenAI, DefaultHttpxClient

client = OpenAI(
    http_client=DefaultHttpxClient(
        proxy="http://username:password@my.proxy.example.com:8080",
    ),
)
Or via environment variables:
export HTTPS_PROXY="http://username:password@my.proxy.example.com:8080"

Custom Proxy Configuration

For more advanced proxy configurations, you can customize the httpx transport:
import httpx
from openai import OpenAI, DefaultHttpxClient

client = OpenAI(
    http_client=DefaultHttpxClient(
        proxy="http://my.proxy.example.com:8080",
        transport=httpx.HTTPTransport(
            local_address="0.0.0.0",
            retries=3,
        ),
    ),
)

Different Proxies for HTTP and HTTPS

You can specify different proxies for HTTP and HTTPS:
import httpx
from openai import OpenAI, DefaultHttpxClient

proxies = {
    "http://": "http://my.http.proxy.example.com:8080",
    "https://": "http://my.https.proxy.example.com:8080",
}

client = OpenAI(
    http_client=DefaultHttpxClient(
        proxies=proxies,
    ),
)
Or with environment variables:
export HTTP_PROXY="http://my.http.proxy.example.com:8080"
export HTTPS_PROXY="http://my.https.proxy.example.com:8080"

SOCKS Proxies

For SOCKS proxy support, install the httpx[socks] extra:
pip install httpx[socks]
Then configure a SOCKS proxy:
import httpx
from openai import OpenAI, DefaultHttpxClient

client = OpenAI(
    http_client=DefaultHttpxClient(
        proxy="socks5://my.socks.proxy.example.com:1080",
    ),
)

Per-Request Proxy

You can configure proxies on a per-request basis using with_options():
import httpx
from openai import OpenAI, DefaultHttpxClient

client = OpenAI()

# Use proxy for this request only
completion = client.with_options(
    http_client=DefaultHttpxClient(
        proxy="http://my.proxy.example.com:8080",
    )
).chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}],
)

Async Proxy Configuration

Proxy configuration works the same way with the async client:
import httpx
from openai import AsyncOpenAI, DefaultAsyncHttpxClient

client = AsyncOpenAI(
    http_client=DefaultAsyncHttpxClient(
        proxy="http://my.proxy.example.com:8080",
    ),
)

Disabling Proxies

To disable environment variable proxies for specific requests:
import httpx
from openai import OpenAI, DefaultHttpxClient

client = OpenAI(
    http_client=DefaultHttpxClient(
        proxies={},  # Empty dict disables environment variable proxies
    ),
)

Troubleshooting

Testing Proxy Connection

Verify your proxy configuration:
import httpx

# Test proxy connection
try:
    response = httpx.get(
        "https://api.openai.com/v1/models",
        proxies={"https://": "http://my.proxy.example.com:8080"},
        headers={"Authorization": "Bearer YOUR_API_KEY"},
    )
    print("Proxy working:", response.status_code)
except Exception as e:
    print("Proxy error:", e)

Common Issues

Increase the timeout for proxy connections:
from openai import OpenAI, DefaultHttpxClient

client = OpenAI(
    http_client=DefaultHttpxClient(
        proxy="http://my.proxy.example.com:8080",
    ),
    timeout=60.0,  # Increase timeout to 60 seconds
)
For corporate proxies with custom certificates, you may need to configure SSL verification:
import httpx
from openai import OpenAI, DefaultHttpxClient

client = OpenAI(
    http_client=DefaultHttpxClient(
        proxy="http://my.proxy.example.com:8080",
        verify=False,  # Not recommended for production
    ),
)
Ensure your credentials are properly URL-encoded:
from urllib.parse import quote

username = quote("user@example.com")
password = quote("p@ssw0rd!")
proxy = f"http://{username}:{password}@proxy.example.com:8080"

Learn More

Build docs developers (and LLMs) love