Ssl_transport_security.cc:1659] Handshake failed with fatal error SSL_ERROR_SSL: error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED

This is my code snippet,

`import os
import google.generativeai as genai
from dotenv import load_dotenv
load_dotenv(“./.env”)

genai.configure(api_key=os.getenv(“GOOGLE_API_KEY”))
for m in genai.list_models():
if ‘generateContent’ in m.supported_generation_methods:
print(m.name)
`

I am working on company’s intra network on a mac system. There is not predefined proxy in my environment.
But instead I am always getting the following error,

E0000 00:00:1734580141.896243 7449930 ssl_transport_security.cc:1659] Handshake failed with fatal error SSL_ERROR_SSL: error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED
E0000 00:00:1734580141.923464 7449928 ssl_transport_security.cc:1659] Handshake failed with fatal error SSL_ERROR_SSL: error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED
E0000 00:00:1734580141.952849 7449928 ssl_transport_security.cc:1659] Handshake failed with fatal error SSL_ERROR_SSL: error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED

1 Like

Since its Mac system on corporate intranet, that can be due to self-signed SSL certificates used in the network or untrusted CA certificates in the Python/OS trust store. Other possibility can be proxy or firewall intercepting SSL.

So you may try installing Certifi and update the cert store.

pip install --upgrade certifi

Then add them into the code before any API calls.

import certifi
import ssl
import urllib.request

ssl_context=ssl.create_default_context(certifi.where())
urllib.request.urlopen("https://www.google.com",context=ssl_context)

If its trust certificates issue, then we can try running:

/Applications/Python\3.x/Install\Certificates.command

(replace 3.x with the exact python version)