Dealing with Certificate Issues in the OpenAI Package on Your Local Machine

Akhil Kanugolu
3 min readJul 14, 2023

--

Problem: Are you encountering a [SSL: CERTIFICATE_VERIFY_FAILED] error when trying to use openai python package in youre local?

Code to use for openai:

import openai
openai.api_key = "sk-..."

# create a chat completion
chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])

# print the chat completion
print(chat_completion.choices[0].message.content)

Error Sample: (Caused by SSLError(SSLCertVerificationError(1, ‘[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)’)))

Solution to fix the Issue:

  1. Begin by opening the following link: https://api.openai.com/v1/engines. Once the page loads, locate and click on the “Cancel” button.

2. After canceling, click on lock icon beside the url and go to connection is secure -> Certificate is valid (Might differ based on the browser you’re using)

3. Click on Details tab and under Certificate Hierarchy Look for the top certificate in the hierarchy and select it and then click on export.

4. On your local machine, open cmd and type pip show certifi command which gives the Location of ceritifi package on you're system.

C:\Users\userprofile>pip show certifi
Name: certifi
Version: 2023.5.7
Summary: Python package for providing Mozilla's CA Bundle.
Home-page: https://github.com/certifi/python-certifi
Author: Kenneth Reitz
Author-email: me@kennethreitz.com
License: MPL-2.0
Location: c:\users\userprofile\appdata\local\programs\python\python39\lib\site-packages

Take note of the location provided in the output. It will be something like “c:\users\userprofile\appdata\local\programs\python\python39\lib\site-packages”

5. Now add \certifi at the end of path and in the certifi folder, locate the file named “cacert.pem” and open it with a text editor of your choice.

6. Scroll towards the bottom of the cacert.pem file until you find a suitable spot to copy & paste the certificate details from the .crt file that you exported from your browser earlier.

7. Save the cacert.pem file after adding the certificate details.

Now, when you execute your code, you should not encounter any errors related to certificates.

Happy Reading :)

--

--