openai-api

APIConnectionError when trying to use OpenAI API


The sample code given here: https://platform.openai.com/docs/api-reference/chat?lang=python successfully runs in Google Colab environment.

But when I run it in my local virtual environment, it gives this error: APIConnectionError: Connection error.

I am using the same key. I am not in VPN.

In full error, I see ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate in certificate chain (_ssl.c:1020)

I already tried running

/Applications/Python*/Install\ Certificates.command

It did not help.

When I run the "curl" example given on the same page, in my shell, it works with the same key!! https://platform.openai.com/docs/api-reference/chat?lang=curl

What do I need to do?

Any guidance appreciated. Thanks


Solution

  • After applying this suggestion, which suggests using truststore, this problem is fixed: https://stackoverflow.com/a/79052345/727997

    pip install truststore
    

    then adding this to your python code above your openai calls:

    import truststore
    truststore.inject_into_ssl()
    # thing that calls requests.get
    

    Prior, I followed this to install the certificates: https://community.openai.com/t/ssl-certificate-verify-failed/32442/58