Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in SSL requests.exceptions.SSLError: HTTPSConnectionPool when running as executable #648

Open
tomar-mohit opened this issue Dec 7, 2024 · 1 comment
Assignees
Labels
component:support How to do xyz? status:triaged Issue/PR triaged to the corresponding sub-team type:help Support-related issues

Comments

@tomar-mohit
Copy link

Description of the bug:

So, below is my code

import google.generativeai as genai
import os


dirPath = os.path.dirname(os.path.realpath(__file__))

if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
    print('running in a PyInstaller bundle')
    dirPath = os.getcwd()
else:
    print('running in a normal Python process')

filePath = os.path.join(dirPath, "words.txt")
lines = []
with open(filePath) as file:
    lines = [line.strip() for line in file]


settingObj = [
        {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_ONLY_HIGH"},
        {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_ONLY_HIGH"},
        {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_ONLY_HIGH"},
        {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_ONLY_HIGH"}
    ]
key = 'mykey'
genai.configure(api_key=key, transport='rest')
model = genai.GenerativeModel("gemini-1.5-flash")
chat = model.start_chat(
    history=[
        {"role": "user", "parts": "Hello"},
        {"role": "model", "parts": "Great to meet you. What would you like to know?"},
    ]
)

query = ''
while query != 'Exit' and len(lines) > 0:
    wd = lines.pop()                  # pop last element O(1)
    msg = "Ask me to guess the meaning of '{}' and tell me if I guessed correct.".format(wd)
    response = chat.send_message(msg, safety_settings=settingObj)
    print (response.text)
    query = input()
    response = chat.send_message(query, safety_settings=settingObj)
    print (response.text)
    query = input()
print('quitting now at the end')

It runs PERFECTLY FINE inside visual studio code. No errors at all. BUT when I create an exe using
pyinstaller --onefile words.py and run that exe, I get a huge exception.

running in a PyInstaller bundle
Traceback (most recent call last):
  File "words.py", line 52, in <module>
  File "google\generativeai\generative_models.py", line 578, in send_message
  File "google\generativeai\generative_models.py", line 331, in generate_content
  File "google\ai\generativelanguage_v1beta\services\generative_service\client.py", line 827, in generate_content
  File "google\api_core\gapic_v1\method.py", line 131, in __call__
  File "google\api_core\retry\retry_unary.py", line 293, in retry_wrapped_func
  File "google\api_core\retry\retry_unary.py", line 153, in retry_target
  File "google\api_core\retry\retry_base.py", line 212, in _retry_error_helper
  File "google\api_core\retry\retry_unary.py", line 144, in retry_target
  File "google\api_core\timeout.py", line 120, in func_with_timeout
  File "google\api_core\grpc_helpers.py", line 76, in error_remapped_callable
  File "google\ai\generativelanguage_v1beta\services\generative_service\transports\rest.py", line 835, in __call__
  File "requests\sessions.py", line 637, in post
  File "google\auth\transport\requests.py", line 538, in request
  File "requests\sessions.py", line 589, in request
  File "requests\sessions.py", line 703, in send
  File "requests\adapters.py", line 698, in send
requests.exceptions.SSLError: HTTPSConnectionPool(host='generativelanguage.googleapis.com', port=443): Max retries exceeded with url: /v1beta/models/gemini-1.5-flash:generateContent?%24alt=json%3Benum-encoding%3Dint (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1002)')))
[PYI-27844:ERROR] Failed to execute script 'words' due to unhandled exception!

Why is the behavior different when running in Visual Studio Code vs compiling as exe and running?

This is what I've tried, change genai.configure(api_key=key, transport='rest') to genai.configure(api_key=key) and then create exe again using this new setting by pyinstaller --onefile words.py. And this time, when I run the exe, I get a completely different error.

running in a PyInstaller bundle
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
E0000 00:00:1733581366.030377   13804 ssl_transport_security.cc:1654] Handshake failed with fatal error SSL_ERROR_SSL: error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED
E0000 00:00:1733581366.059071   37716 ssl_transport_security.cc:1654] Handshake failed with fatal error SSL_ERROR_SSL: error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED
E0000 00:00:1733581366.081933   13804 ssl_transport_security.cc:1654] Handshake failed with fatal error SSL_ERROR_SSL: error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED
E0000 00:00:1733581366.105808   41104 ssl_transport_security.cc:1654] Handshake failed with fatal error SSL_ERROR_SSL: error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED
E0000 00:00:1733581366.130128   34388 ssl_transport_security.cc:1654] Handshake failed with fatal error SSL_ERROR_SSL: error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED
E0000 00:00:1733581366.153851   13804 ssl_transport_security.cc:1654] Handshake failed with fatal error SSL_ERROR_SSL: error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED
E0000 00:00:1733581366.176788   13804 ssl_transport_security.cc:1654] Handshake failed with fatal error SSL_ERROR_SSL: error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED
E0000 00:00:1733581366.206581   35272 ssl_transport_security.cc:1654] Handshake failed with fatal error SSL_ERROR_SSL: error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED

Actual vs expected behavior:

Actual Behavior
2 different errors, as mentioned above, with and without the transport='rest' part when running as compiled exe.

Expected Behavior
It should work with proper chat as it does inside Visual Studio Code.

Any other information you'd like to share?

I do not have any proxy or VPN or anything like that.

@gmKeshari gmKeshari added status:triaged Issue/PR triaged to the corresponding sub-team type:help Support-related issues component:support How to do xyz? labels Dec 9, 2024
@tomar-mohit
Copy link
Author

Hi, I just wanted to check for any update on this. I see issues raised after this all have at least one comment, so most likely, there is some activity on those. But there is nothing on this, and I see it has been triaged. Can I get some updates on whether this is being traced or the current status?

@gmKeshari gmKeshari assigned gmKeshari and pamorgan and unassigned gmKeshari Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:support How to do xyz? status:triaged Issue/PR triaged to the corresponding sub-team type:help Support-related issues
Projects
None yet
Development

No branches or pull requests

3 participants