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.
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)
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)
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"
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:
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.
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')
openai.log='debug'and that will log the details.