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

Async openai.Embedding.acreate forcibly prints INFO, whereas openai.Embedding.create prints DEBUG

Open kenn opened this issue 2 years ago • 0 comments

Describe the bug

When openai.Embedding.acreate gets called many times, it prints a lot to STDERR, like so:

INFO:openai:message='OpenAI API response' path=https://api.openai.com/v1/engines/text-embedding-ada-002/embeddings processing_ms=350 request_id=681e326f4d3241ef9427e95c9eede485 response_code=200
INFO:openai:message='OpenAI API response' path=https://api.openai.com/v1/engines/text-embedding-ada-002/embeddings processing_ms=350 request_id=681e326f4d3241ef9427e95c9eede485 response_code=200
...

and there's no way to stop it to clutter the console. That is because util.log_info is used in APIRequestor.arequest_raw whereas util.log_debug is used in APIRequestor.request_raw, and it actually prints directly to STDERR.

https://github.com/openai/openai-python/blob/main/openai/util.py#L63-L67

def log_info(message, **params):
    msg = logfmt(dict(message=message, **params))
    if _console_log_level() in ["debug", "info"]:
        print(msg, file=sys.stderr)
    logger.info(msg)

It may be intentional, and avoidable as follows, but it also suppress other logging. The different behaviors between sync/async seems like it's a bug.

logger = logging.getLogger("openai")
logger.setLevel(logging.WARN)
... API calls ...
logger.setLevel(logging.INFO)

To Reproduce

  1. Call openai.Embedding.acreate
  2. Watch the console output for STDERR

Code snippets

No response

OS

macOS

Python version

Python v3.10.10

Library version

openai-python v0.26.2

kenn avatar Mar 16 '23 07:03 kenn