How to configure proxy authentication for OpenAI API calls in Python

Issue with Proxy Setup for OpenAI API

I’m trying to connect to the OpenAI API through a proxy server but keep getting authentication errors. The proxy requires IPv4 protocol and I have valid credentials, but something is wrong with my configuration.

Error Message:

407 Proxy Authentication Required
Access to requested resource disallowed by administrator or you need valid username/password to use this resource

My current implementation:

import settings
from openai import OpenAI

proxy_config = {
    'https': 'http://user123:[email protected]:8080'
}

api_client = OpenAI(
    api_key=settings.openai_key,
    base_url=proxy_config.get('https'),
)

response = api_client.completions.create(
    model="gpt-3.5-turbo-instruct",
    prompt="Hello world test",
    max_tokens=10,
    temperature=0.5
)

print(response.choices[0].text)

I think the issue might be with how I’m passing the proxy configuration to the OpenAI client. Has anyone successfully used OpenAI API with proxy authentication? What’s the correct way to handle this?

I encountered this exact problem when implementing OpenAI integration behind our company firewall. The issue is definitely with how you’re handling the proxy configuration. Instead of modifying the base_url parameter, you need to create a custom httpx client with proper proxy settings. Here’s what finally worked for me after several failed attempts: create an httpx.Client instance with the proxies parameter set correctly, then pass this client to the OpenAI constructor using the http_client parameter. The key thing I discovered is that the OpenAI library expects the base_url to remain pointing to OpenAI’s servers, while the proxy handling happens at the transport layer. Also worth checking if your proxy requires NTLM authentication rather than basic auth, as this can cause the same 407 error even with correct credentials. I wasted considerable time assuming it was basic auth when our proxy actually needed NTLM.

looks like you’re confusing base_url with proxy settings there. base_url should stay as openai’s default endpoint, not your proxy. try using requests.Session() with proxies dict then pass it thru http_client param when creating OpenAI client. worked for me on similar setup last week.

You’re mixing up the proxy configuration with the base_url parameter. The base_url should point to OpenAI’s API endpoint, not your proxy server. The proxy configuration needs to be handled at the HTTP client level instead. I ran into this exact same issue last month when setting up our corporate proxy. The OpenAI Python library uses httpx under the hood, so you need to configure the proxy through the http_client parameter. Try creating a custom httpx client with your proxy settings and pass it to the OpenAI constructor. Also double-check that your proxy credentials don’t contain special characters that need URL encoding. I spent hours debugging what turned out to be an unescaped @ symbol in my password. The 407 error usually means the credentials are being passed but they’re malformed or incorrect.

I had similar troubles with proxy auth when working with OpenAI’s client library. The problem is that you’re setting base_url to your proxy server when it should remain as the default OpenAI endpoint. What worked for me was configuring the proxy through environment variables instead of trying to pass it directly to the client. Set HTTP_PROXY and HTTPS_PROXY environment variables with your credentials like http://user123:[email protected]:8080 and remove the base_url parameter entirely from your OpenAI client initialization. The underlying HTTP client will automatically pick up these environment variables for proxy configuration. Make sure your username and password are properly URL-encoded if they contain special characters. Another thing to verify is that your proxy server actually allows connections to api.openai.com on port 443, since some corporate proxies block AI services by default.