openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

Error with proxy use

Open olegoleg1111121 opened this issue 2 years ago • 3 comments

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • [X] This is an issue with the Python library

Describe the bug

from openai import AsyncOpenAI
import httpx
import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
client = AsyncOpenAI(
     # This is the default and can be omitted
    api_key="...",
    base_url='https://api.openai.com/v1',
    timeout=20,
    http_client=httpx.Client(verify=False,
        proxies={"all://":"http://..."},
    ),
 )

After that, I make an asynchronous request to the chat.completions method

DEBUG:openai._base_client:Request options: {'method': 'post', 'url': '/chat/completions', 'files': None, 'json_data': {'messages': [{'role': 'system', 'content': '.......'}, {'role': 'user', 'content': 'fef'}], 'model': 'gpt-4', 'max_tokens': 4096}}
DEBUG:httpcore.connection:connect_tcp.started host='......' port=.....local_address=None timeout=20 socket_options=None
DEBUG:httpcore.connection:connect_tcp.complete return_value=<httpcore._backends.sync.SyncStream object at 0x000001D831AD70D0>
DEBUG:httpcore.http11:send_request_headers.started request=<Request [b'CONNECT']>
DEBUG:httpcore.http11:send_request_headers.complete
DEBUG:httpcore.http11:send_request_body.started request=<Request [b'CONNECT']>
DEBUG:httpcore.http11:send_request_body.complete
DEBUG:httpcore.http11:receive_response_headers.started request=<Request [b'CONNECT']>
DEBUG:httpcore.http11:receive_response_headers.complete return_value=(b'HTTP/1.1', 200, b'Connection Established', [])
DEBUG:httpcore.proxy:start_tls.started ssl_context=<ssl.SSLContext object at 0x000001D82F847940> server_hostname='api.openai.com' timeout=20
DEBUG:httpcore.proxy:start_tls.complete return_value=<httpcore._backends.sync.SyncStream object at 0x000001D831AD7100>
DEBUG:httpcore.http11:send_request_headers.started request=<Request [b'POST']>
DEBUG:httpcore.http11:send_request_headers.complete
DEBUG:httpcore.http11:send_request_body.started request=<Request [b'POST']>
DEBUG:httpcore.http11:send_request_body.complete
DEBUG:httpcore.http11:receive_response_headers.started request=<Request [b'POST']>
DEBUG:httpcore.http11:receive_response_headers.complete return_value=(b'HTTP/1.1', 200, b'OK', [(b'Date', b'Sat, 02 Mar 2024 08:57:45 GMT'), (b'Content-Type', b'application/json'), (b'Transfer-Encoding', b'chunked'), (b'Connection', b'keep-alive'), (b'access-control-allow-origin', b'*'), (b'Cache-Control', b'no-cache, must-revalidate'), (b'openai-model', b'gpt-4-0613'), (b'openai-organization', b'gergergergegr'), (b'openai-processing-ms', b'1174'), (b'openai-version', b'2020-10-01'), (b'strict-transport-security', b'max-age=15724800; includeSubDomains'), (b'x-ratelimit-limit-requests', b'5000'), (b'x-ratelimit-limit-tokens', b'40000'), (b'x-ratelimit-remaining-requests', b'4999'), (b'x-ratelimit-remaining-tokens', b'35807'), (b'x-ratelimit-reset-requests', b'12ms'), (b'x-ratelimit-reset-tokens', b'6.289s'), (b'x-request-id', b'......'), (b'CF-Cache-Status', b'DYNAMIC'), (b'Set-Cookie', b'__cf_bm=.....; path=/; expires=Sat, 02-Mar-24 09:27:45 GMT; domain=.api.openai.com; HttpOnly; Secure; SameSite=None'), (b'Set-Cookie', b'_cfuvid=.....; path=/; domain=.api.openai.com; HttpOnly; Secure; SameSite=None'), (b'Server', b'cloudflare'), (b'CF-RAY', b'85e02e54ecf9cb27-DUS'), (b'Content-Encoding', b'br'), (b'alt-svc', b'h3=":443"; ma=86400')])
INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
DEBUG:httpcore.http11:receive_response_body.started request=<Request [b'POST']>
DEBUG:httpcore.http11:receive_response_body.complete
DEBUG:httpcore.http11:response_closed.started
DEBUG:httpcore.http11:response_closed.complete
DEBUG:openai._base_client:Encountered Exception
Traceback (most recent call last):
  File "C:\Users\oleg\AppData\Local\Programs\Python\Python39\lib\site-packages\openai\_base_client.py", line 1457, in _request
    resp = await self._client.send(
TypeError: object Response can't be used in 'await' expression

To Reproduce

  1. Use AsyncOpenAI
  2. Use proxy in client.AsyncOpenAI
  3. use await client.chat.completions

Code snippets

No response

OS

Windows 10

Python version

Python 3.9.4

Library version

openai 1.13.1

olegoleg1111121 avatar Mar 02 '24 09:03 olegoleg1111121

Thanks for reporting, we'll take a look.

Could you share an example of how you're making the request?

rattrayalex avatar Mar 02 '24 22:03 rattrayalex

@olegoleg1111121 this is because you're passing the synchronous https client to the asynchronous openai client, changing your code to httpx.AsyncClient will fix your error.

We could have a better error message here though.

RobertCraigie avatar Mar 03 '24 09:03 RobertCraigie

@olegoleg1111121 this is because you're passing the synchronous https client to the asynchronous openai client, changing your code to httpx.AsyncClient will fix your error.

We could have a better error message here though.

This solved the problem, thanks!

olegoleg1111121 avatar Mar 03 '24 09:03 olegoleg1111121