8

I am using the open-ai python library. How can I enable logging of HTTP request/response/headers for all calls made to open-ai?

I am not able to find anything specific in the API docs.

3
  • 3
    Answering my own question. You can set openai.log='debug' and that will log the details. Commented Jun 23, 2023 at 19:15
  • 1
    What if you want to turn OFF logging? openai.log="error" doesn't seem to eliminate INFO logging statements from OpenAI. Commented Sep 8, 2023 at 17:18
  • To turn off INFO, instead of enabling INFO for everything, enable info only for the module you care about, i.e., don't do logging.basicConfig(level=logging.INFO, ...). Instead, omit that level param and set the level for your module by writing logging.getLogger(...).setLevel(logging.INFO) for each of module you care about. Commented Dec 27, 2023 at 13:12

5 Answers 5

7

You can enable debug logging through an environment variable:

export OPENAI_LOG=debug

Note that this also enables httpx logging. If you want to log response data, you'll need to customize the http_client (example here)

Sign up to request clarification or add additional context in comments.

Comments

5

Here's another way to set logging levels for specific loggers.

First, find the OpenAI loggers (more info on getting loggers):

import logging

loggers = [logging.getLogger(name) for name in logging.root.manager.loggerDict]
openai_loggers = [logger for logger in loggers if logger.name.startswith("openai")]
# [<Logger openai._legacy_response (WARNING)>, <Logger openai (WARNING)>, <Logger openai._response (WARNING)>, <Logger openai._base_client (DEBUG)>]

Once you know their name, you can set a custom logging level for each logger. For example:

logging.getLogger("openai._base_client").setLevel(logging.DEBUG)

1 Comment

Amazing coverage for multiple cases!
1

Per the comment from the OP, you can change the log level (at least to debug) by setting the log attribute:

import openai

openai.log = "debug"

Comments

1

I'm posting this because none of the answers here worked for me in 2025. To enable logging for OpenAI we need to do two things:

  1. Add a stream handler for the logger.
  2. Set the log level to DEBUG.

This worked for me:

from openai import OpenAI
import logging

log = logging.getLogger(OpenAI.__module__)
log.addHandler(logging.StreamHandler())
log.setLevel(logging.DEBUG)

#Do a quick test.
log.debug("Hello World")

Now you should see all requests and responses sent and received with OpenAI.

Comments

0

It might be overkill, but this provided me excessive and detailed information in logging. Not just openai calls though.

import logging
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s - %(levelname)s - %(filename)s:%(funcName)s:%(lineno)d - %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.