How to enable HTTP request logging in OpenAI Python SDK

I’m working with the OpenAI Python SDK and need to debug some API calls. I want to see all the HTTP requests and responses that are being sent to OpenAI’s servers, including the headers and response data.

I’ve looked through the documentation but couldn’t find any clear examples of how to enable this kind of logging. Does anyone know the proper way to set up logging so I can monitor all the network traffic when making calls to the OpenAI API?

Any help would be appreciated since I need to troubleshoot some issues with my API calls.

You can also use the SDK’s built-in logging instead of enabling full debug mode on httpx. Just be selective:

import logging
import openai

# Set up logging for just OpenAI requests
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("openai")
logger.setLevel(logging.DEBUG)

# Create handler to see the output
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)

This gives you cleaner output than dumping everything from httpx. You’ll still see request details minus all the extra noise.

I also use a simple wrapper when debugging specific calls:

def debug_openai_call(func, *args, **kwargs):
    print(f"Making call to {func.__name__}")
    try:
        result = func(*args, **kwargs)
        print(f"Success: {len(str(result))} chars returned")
        return result
    except Exception as e:
        print(f"Error: {e}")
        raise

Helps me track which API calls are failing without drowning in HTTP logs.

I had the same issue when debugging the OpenAI SDK. The SDK runs on httpx, so you can enable HTTP logging to see what’s happening. Just add this before your API calls:

import logging
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("httpx").setLevel(logging.DEBUG)

This dumps all the HTTP requests and responses to your console. Warning though - it gets really verbose and will show your API key in the headers. Don’t use this in production or you’ll leak your key. It’s great for debugging request formatting and rate limit issues.

Another option: use HTTP debugging tools outside the SDK. I just run export HTTPX_LOG_LEVEL=trace before my script and it shows everything without touching the code. Perfect when you don’t want to mess with your actual app just to debug.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.