Azure OpenAI fails with an SSL certificate verification error
httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED]Short answer
The client checks the endpoint's certificate against its CA bundle, which is certifi by default. If a company proxy re-signs HTTPS traffic with its own CA, the check fails with CERTIFICATE_VERIFY_FAILED. Give the client the proxy's CA file through SSL_CERT_FILE, or build an SSL context from the system store. Do not set verify to False.
Fix it fast
1Find out which cause you have
See who signed the certificate your machine receives from the endpoint. A company name instead of Microsoft means a proxy sits in the middle.
openssl s_client -connect example-endpoint.openai.azure.com:443 -servername example-endpoint.openai.azure.com </dev/null 2>/dev/null | openssl x509 -noout -issuer2Match what you see, then jump to its fix
What does the error look like?
httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED]
Cause 1 of 2
A proxy re-signs HTTPS with a CA that Python does not know
1Check
HTTPX, which the OpenAI client uses, trusts the certifi bundle by default and respects SSL_CERT_FILE and SSL_CERT_DIR.
python -c "import certifi; print(certifi.where())"2Fix
Get the proxy's root CA in PEM format from your IT team and point SSL_CERT_FILE at a bundle that contains it.
export SSL_CERT_FILE=/etc/ssl/certs/company-ca-bundle.pem
python app.py3Verify
Run the request again; the verification error is gone.
python -c "from openai import AzureOpenAI; print('client ok')"Confirmed by python-httpx.org
Cause 2 of 2
You need the operating system's certificate store
1Check
Python's HTTP stack does not use the OS store unless asked. The truststore package lets HTTPX use it.
pip show truststore2Fix
Install truststore, and build an SSL context from it for the client you pass in.
pip install truststore
import ssl, truststore, httpx
from openai import AzureOpenAI
ctx = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
client = AzureOpenAI(
azure_endpoint="https://example-endpoint.openai.azure.com",
api_version="2023-07-01-preview",
http_client=httpx.Client(verify=ctx),
)3Verify
Send a small request with this client.
print('client built with system trust store')Confirmed by python-httpx.org
How do I stop it coming back?
- Treat verify=False as a debugging step on a throwaway machine, never in code that ships.
- Keep the CA bundle path in an environment variable, not in application code.
- Add the company CA to your Docker images when the same proxy applies in CI.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- MenuloA restaurant discovery platform in Germany, kept current by a Python ingestion pipeline.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
AI
Azure OpenAI says the API deployment for this resource does not exist
The API deployment for this resource does not exist
Azure OpenAI does not take a model name in the model field.
2 causes, each with a fixAI
The OpenAI client says the API key is missing
Missing credentials. Please pass an `api_key`
OpenAI() looks for an api_key argument first and then the OPENAI_API_KEY environment variable.
3 causes, each with a fixAI
OpenAI says the model is a chat model and not supported in the v1/completions endpoint
This is a chat model and not supported in the v1/completions endpoint
The v1/completions endpoint accepts completion models such as gpt-3.5-turbo-instruct.
3 causes, each with a fixAI
Old openai.ChatCompletion or openai.Embedding code breaks on a new SDK
APIRemovedInV1
The Python SDK was rewritten in version 1.0.
2 causes, each with a fixAI
TypeError when you index an OpenAI response like a dictionary
TypeError: 'ChatCompletion' object is not subscriptable
Since SDK 1.0 the OpenAI Python library returns Pydantic model objects instead of dictionaries, so response['choices'] fails.
2 causes, each with a fixAI
TypeError: Client.__init__() got an unexpected keyword argument 'proxies'
TypeError: Client.__init__() got an unexpected keyword argument 'proxies'
httpx 0.28.0 removed its deprecated proxies argument.
2 causes, each with a fix