Getting error in executing api using openai 1.2.4 with python

When I try to execute chatgpt api’s using openai 1.2.4 library with python I get following error:

Traceback (most recent call last):
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_exceptions.py”, line 10, in map_exceptions
yield
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_backends\sync.py”, line 168, in start_tls
raise exc
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_backends\sync.py”, line 163, in start_tls
sock = ssl_context.wrap_socket(
^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\ssl.py”, line 455, in wrap_socket
return self.sslsocket_class._create(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\ssl.py”, line 1046, in _create
self.do_handshake()
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\ssl.py”, line 1317, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_transports\default.py”, line 66, in map_httpcore_exceptions
yield
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_transports\default.py”, line 228, in handle_request
resp = self._pool.handle_request(req)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_sync\connection_pool.py”, line 268, in handle_request
raise exc
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_sync\connection_pool.py”, line 251, in handle_request
response = connection.handle_request(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_sync\connection.py”, line 99, in handle_request
raise exc
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_sync\connection.py”, line 76, in handle_request
stream = self._connect(request)
^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_sync\connection.py”, line 156, in _connect
stream = stream.start_tls(**kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_backends\sync.py”, line 152, in start_tls
with map_exceptions(exc_map):
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\contextlib.py”, line 155, in exit
self.gen.throw(value)
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_exceptions.py”, line 14, in map_exceptions
raise to_exc(exc) from exc
httpcore.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\openai_base_client.py”, line 858, in _request
response = self._client.send(request, auth=self.custom_auth, stream=stream)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_client.py”, line 901, in send
response = self._send_handling_auth(
^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_client.py”, line 929, in _send_handling_auth
response = self._send_handling_redirects(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_client.py”, line 966, in _send_handling_redirects
response = self._send_single_request(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_client.py”, line 1002, in _send_single_request
response = transport.handle_request(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_transports\default.py”, line 227, in handle_request
with map_httpcore_exceptions():
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\contextlib.py”, line 155, in exit
self.gen.throw(value)
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_transports\default.py”, line 83, in map_httpcore_exceptions
raise mapped_exc(message) from exc
httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_exceptions.py”, line 10, in map_exceptions
yield
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_backends\sync.py”, line 168, in start_tls
raise exc
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_backends\sync.py”, line 163, in start_tls
sock = ssl_context.wrap_socket(
^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\ssl.py”, line 455, in wrap_socket
return self.sslsocket_class._create(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\ssl.py”, line 1046, in _create
self.do_handshake()
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\ssl.py”, line 1317, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_transports\default.py”, line 66, in map_httpcore_exceptions
yield
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_transports\default.py”, line 228, in handle_request
resp = self._pool.handle_request(req)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_sync\connection_pool.py”, line 268, in handle_request
raise exc
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_sync\connection_pool.py”, line 251, in handle_request
response = connection.handle_request(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_sync\connection.py”, line 99, in handle_request
raise exc
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_sync\connection.py”, line 76, in handle_request
stream = self._connect(request)
^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_sync\connection.py”, line 156, in _connect
stream = stream.start_tls(**kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_backends\sync.py”, line 152, in start_tls
with map_exceptions(exc_map):
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\contextlib.py”, line 155, in exit
self.gen.throw(value)
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_exceptions.py”, line 14, in map_exceptions
raise to_exc(exc) from exc
httpcore.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\openai_base_client.py”, line 858, in _request
response = self._client.send(request, auth=self.custom_auth, stream=stream)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_client.py”, line 901, in send
response = self._send_handling_auth(
^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_client.py”, line 929, in _send_handling_auth
response = self._send_handling_redirects(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_client.py”, line 966, in _send_handling_redirects
response = self._send_single_request(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_client.py”, line 1002, in _send_single_request
response = transport.handle_request(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_transports\default.py”, line 227, in handle_request
with map_httpcore_exceptions():
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\contextlib.py”, line 155, in exit
self.gen.throw(value)
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_transports\default.py”, line 83, in map_httpcore_exceptions
raise mapped_exc(message) from exc
httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_exceptions.py”, line 10, in map_exceptions
yield
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_backends\sync.py”, line 168, in start_tls
raise exc
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_backends\sync.py”, line 163, in start_tls
sock = ssl_context.wrap_socket(
^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\ssl.py”, line 455, in wrap_socket
return self.sslsocket_class._create(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\ssl.py”, line 1046, in _create
self.do_handshake()
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\ssl.py”, line 1317, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_transports\default.py”, line 66, in map_httpcore_exceptions
yield
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_transports\default.py”, line 228, in handle_request
resp = self._pool.handle_request(req)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_sync\connection_pool.py”, line 268, in handle_request
raise exc
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_sync\connection_pool.py”, line 251, in handle_request
response = connection.handle_request(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_sync\connection.py”, line 99, in handle_request
raise exc
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_sync\connection.py”, line 76, in handle_request
stream = self._connect(request)
^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_sync\connection.py”, line 156, in _connect
stream = stream.start_tls(**kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_backends\sync.py”, line 152, in start_tls
with map_exceptions(exc_map):
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\contextlib.py”, line 155, in exit
self.gen.throw(value)
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpcore_exceptions.py”, line 14, in map_exceptions
raise to_exc(exc) from exc
httpcore.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\openai_base_client.py”, line 858, in _request
response = self._client.send(request, auth=self.custom_auth, stream=stream)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_client.py”, line 901, in send
response = self._send_handling_auth(
^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_client.py”, line 929, in _send_handling_auth
response = self._send_handling_redirects(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_client.py”, line 966, in _send_handling_redirects
response = self._send_single_request(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_client.py”, line 1002, in _send_single_request
response = transport.handle_request(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_transports\default.py”, line 227, in handle_request
with map_httpcore_exceptions():
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\contextlib.py”, line 155, in exit
self.gen.throw(value)
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\httpx_transports\default.py”, line 83, in map_httpcore_exceptions
raise mapped_exc(message) from exc
httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “C:\Users\kirti_sekharpandey\OneDrive - HCL Technologies Ltd\Documents\Chatgpt\Main Coding Files\PythonChatGptOpenaiCode.py”, line 29, in
completion = client.chat.completions.create(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\openai_utils_utils.py”, line 299, in wrapper
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\openai\resources\chat\completions.py”, line 594, in create
return self._post(
^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\openai_base_client.py”, line 1055, in post
return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\openai_base_client.py”, line 834, in request
return self._request(
^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\openai_base_client.py”, line 890, in _request
return self._retry_request(
^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\openai_base_client.py”, line 925, in _retry_request
return self._request(
^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\openai_base_client.py”, line 890, in _request
return self._retry_request(
^^^^^^^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\openai_base_client.py”, line 925, in _retry_request
return self._request(
^^^^^^^^^^^^^^
File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\openai_base_client.py”, line 897, in _request
raise APIConnectionError(request=request) from err
openai.APIConnectionError: Connection error.

Please help in resolving these errors.
Thanks in advance

Looks like there is not SSL certificate installed.

I would update your python to 3.8, that will likely fix the issues you are seeing.

I am also facing same issue with Python 3.12.0. Pls advice.

raise to_exc(exc) from exc

httpcore.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)

[quote="kirtishekharpandey19, post:1, topic:503995"File “C:\Users\kirti_sekharpandey\AppData\Local\Programs\Python\Python312\Lib\site-packages\openai_base_client.py”, line 897, in _request
[/quote]

You are using python 3.12, right? I am using 3.11, and getting the same error (with different line numbers).

  File "C:\Users\danie\anaconda3\envs\autogen-agi2\Lib\site-packages\openai\_base_client.py", line 896, in _request
    return self._retry_request(
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\danie\anaconda3\envs\autogen-agi2\Lib\site-packages\openai\_base_client.py", line 958, in _retry_request
    return self._request(
           ^^^^^^^^^^^^^^
  File "C:\Users\danie\anaconda3\envs\autogen-agi2\Lib\site-packages\openai\_base_client.py", line 905, in _request
    raise APIConnectionError(request=request) from err
openai.APIConnectionError: Connection error.

We are using Windows. My suspicion is that there is a firewall issue blocking the connection.