Connection error when processing long prompt - Server disconnected without sending a response

Hi,

I am trying to run this function:

def run_gpt4turbo(full_prompt):
    """
    API interaction. Returns GPT's completion.
    """

    with open(f"{MAIN_DIR}/apikey_gpt4turbo.txt", "r") as f:
        api_key = f.read().strip()
    
    client = AzureOpenAI(
        api_key=api_key,
        api_version="2023-09-01-preview",
        azure_endpoint="https://<resourcename>.openai.azure.com",
    )
    
    chat_completion = client.chat.completions.create(
        messages=[
            {"role": "system", "content": "You are an AI assistant that helps with extracting data."},
            {"role":"user","content":full_prompt},
        ],
        model='gpt4-turbo',
    )

    return chat_completion.choices[0].message.content

with a relatively big full_prompt (70.500 tokens), however still within the token limit of gpt4-turbo (120.000 tokens). When running it, OpenAI seems to lose connection before finishing the processing of the prompt.
This is the error message received:

Traceback (most recent call last):
  File "C:\python_venv\venv\Lib\site-packages\httpx\_transports\default.py", line 67, in map_httpcore_exceptions
    yield
  File "C:\python_venv\venv\Lib\site-packages\httpx\_transports\default.py", line 231, in handle_request
    resp = self._pool.handle_request(req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpcore\_sync\connection_pool.py", line 268, in handle_request
    raise exc
  File "C:\python_venv\venv\Lib\site-packages\httpcore\_sync\connection_pool.py", line 251, in handle_request
    response = connection.handle_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpcore\_sync\connection.py", line 103, in handle_request
    return self._connection.handle_request(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpcore\_sync\http11.py", line 133, in handle_request
    raise exc
  File "C:\python_venv\venv\Lib\site-packages\httpcore\_sync\http11.py", line 111, in handle_request
    ) = self._receive_response_headers(**kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpcore\_sync\http11.py", line 176, in _receive_response_headers
    event = self._receive_event(timeout=timeout)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpcore\_sync\http11.py", line 226, in _receive_event
    raise RemoteProtocolError(msg)
httpcore.RemoteProtocolError: Server disconnected without sending a response.

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

Traceback (most recent call last):
  File "C:\python_venv\venv\Lib\site-packages\openai\_base_client.py", line 897, in _request
    response = self._client.send(
               ^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpx\_client.py", line 915, in send
    response = self._send_handling_auth(
               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpx\_client.py", line 943, in _send_handling_auth
    response = self._send_handling_redirects(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpx\_client.py", line 980, in _send_handling_redirects
    response = self._send_single_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpx\_client.py", line 1016, in _send_single_request
    response = transport.handle_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpx\_transports\default.py", line 230, in handle_request
    with map_httpcore_exceptions():
  File "C:\Users\SofieKnøsgaard(SKNQ)\AppData\Local\Programs\Python\Python311\Lib\contextlib.py", line 158, in __exit__
    self.gen.throw(typ, value, traceback)
  File "C:\python_venv\venv\Lib\site-packages\httpx\_transports\default.py", line 84, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.RemoteProtocolError: Server disconnected without sending a response.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\python_venv\venv\Lib\site-packages\httpx\_transports\default.py", line 67, in map_httpcore_exceptions
    yield
  File "C:\python_venv\venv\Lib\site-packages\httpx\_transports\default.py", line 231, in handle_request
    resp = self._pool.handle_request(req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpcore\_sync\connection_pool.py", line 268, in handle_request
    raise exc
  File "C:\python_venv\venv\Lib\site-packages\httpcore\_sync\connection_pool.py", line 251, in handle_request
    response = connection.handle_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpcore\_sync\connection.py", line 103, in handle_request
    return self._connection.handle_request(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpcore\_sync\http11.py", line 133, in handle_request
    raise exc
  File "C:\python_venv\venv\Lib\site-packages\httpcore\_sync\http11.py", line 111, in handle_request
    ) = self._receive_response_headers(**kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpcore\_sync\http11.py", line 176, in _receive_response_headers
    event = self._receive_event(timeout=timeout)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpcore\_sync\http11.py", line 226, in _receive_event
    raise RemoteProtocolError(msg)
httpcore.RemoteProtocolError: Server disconnected without sending a response.

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

Traceback (most recent call last):
  File "C:\python_venv\venv\Lib\site-packages\openai\_base_client.py", line 897, in _request
    response = self._client.send(
               ^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpx\_client.py", line 915, in send
    response = self._send_handling_auth(
               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpx\_client.py", line 943, in _send_handling_auth
    response = self._send_handling_redirects(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpx\_client.py", line 980, in _send_handling_redirects
    response = self._send_single_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpx\_client.py", line 1016, in _send_single_request
    response = transport.handle_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpx\_transports\default.py", line 230, in handle_request
    with map_httpcore_exceptions():
  File "C:\Users\SofieKnøsgaard(SKNQ)\AppData\Local\Programs\Python\Python311\Lib\contextlib.py", line 158, in __exit__
    self.gen.throw(typ, value, traceback)
  File "C:\python_venv\venv\Lib\site-packages\httpx\_transports\default.py", line 84, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.RemoteProtocolError: Server disconnected without sending a response.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\python_venv\venv\Lib\site-packages\httpx\_transports\default.py", line 67, in map_httpcore_exceptions
    yield
  File "C:\python_venv\venv\Lib\site-packages\httpx\_transports\default.py", line 231, in handle_request
    resp = self._pool.handle_request(req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpcore\_sync\connection_pool.py", line 268, in handle_request
    raise exc
  File "C:\python_venv\venv\Lib\site-packages\httpcore\_sync\connection_pool.py", line 251, in handle_request
    response = connection.handle_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpcore\_sync\connection.py", line 103, in handle_request
    return self._connection.handle_request(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpcore\_sync\http11.py", line 133, in handle_request
    raise exc
  File "C:\python_venv\venv\Lib\site-packages\httpcore\_sync\http11.py", line 111, in handle_request
    ) = self._receive_response_headers(**kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpcore\_sync\http11.py", line 176, in _receive_response_headers
    event = self._receive_event(timeout=timeout)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpcore\_sync\http11.py", line 226, in _receive_event
    raise RemoteProtocolError(msg)
httpcore.RemoteProtocolError: Server disconnected without sending a response.

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

Traceback (most recent call last):
  File "C:\python_venv\venv\Lib\site-packages\openai\_base_client.py", line 897, in _request
    response = self._client.send(
               ^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpx\_client.py", line 915, in send
    response = self._send_handling_auth(
               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpx\_client.py", line 943, in _send_handling_auth
    response = self._send_handling_redirects(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpx\_client.py", line 980, in _send_handling_redirects
    response = self._send_single_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpx\_client.py", line 1016, in _send_single_request
    response = transport.handle_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\httpx\_transports\default.py", line 230, in handle_request
    with map_httpcore_exceptions():
  File "C:\Users\SofieKnøsgaard(SKNQ)\AppData\Local\Programs\Python\Python311\Lib\contextlib.py", line 158, in __exit__
    self.gen.throw(typ, value, traceback)
  File "C:\python_venv\venv\Lib\site-packages\httpx\_transports\default.py", line 84, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.RemoteProtocolError: Server disconnected without sending a response.

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

Traceback (most recent call last):
  File "C:\Users\SofieKnøsgaard\Documents\GIT_repos\genai\PythonScripts\prompt_generate.py", line 162, in <module>
    completion = run_gpt4turbo(full_prompt)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\SofieKnøsgaard\Documents\GIT_repos\genai\PythonScripts\openai_functions.py", line 28, in run_gpt4turbo
    chat_completion = client.chat.completions.create(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\openai\_utils\_utils.py", line 271, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\openai\resources\chat\completions.py", line 648, in create
    return self._post(
           ^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\openai\_base_client.py", line 1179, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\openai\_base_client.py", line 868, in request
    return self._request(
           ^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\openai\_base_client.py", line 921, in _request
    return self._retry_request(
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\openai\_base_client.py", line 992, in _retry_request
    return self._request(
           ^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\openai\_base_client.py", line 921, in _request
    return self._retry_request(
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\openai\_base_client.py", line 992, in _retry_request
    return self._request(
           ^^^^^^^^^^^^^^
  File "C:\python_venv\venv\Lib\site-packages\openai\_base_client.py", line 931, in _request
    raise APIConnectionError(request=request) from err
openai.APIConnectionError: Connection error.

When running the same function with a slightly smaller full_prompt (44.000 tokens) it works fine and I get the expected response.

Can anyone help me resolve this issue.

Thanks in advance.

2 Likes

For me I had to turn off my VPN…

[ignore this text, just here to bypass the bot]

1 Like

Turning my VPN off was also the solution in my case

Thanks