Skip to content

feat: add request/response body logging at DEBUG level#3043

Open
crawfordxx wants to merge 1 commit intoopenai:mainfrom
crawfordxx:feat-log-request-response-body
Open

feat: add request/response body logging at DEBUG level#3043
crawfordxx wants to merge 1 commit intoopenai:mainfrom
crawfordxx:feat-log-request-response-body

Conversation

@crawfordxx
Copy link
Copy Markdown

Summary

Adds optional logging of HTTP request and response bodies at DEBUG level to aid debugging, addressing #1522.

  • Logs request body before sending and response body after receiving
  • Gated behind log.isEnabledFor(logging.DEBUG) to avoid performance impact when not enabled
  • Skips response body logging for streaming responses to prevent reading the stream prematurely
  • Applied to both sync (SyncAPIClient) and async (AsyncAPIClient) code paths

Usage

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

client = openai.OpenAI()
client.chat.completions.create(model="gpt-4", messages=[{"role": "user", "content": "hi"}])
# DEBUG logs will now include request/response bodies

Test plan

  • Verify request body appears in DEBUG logs when logging level is set
  • Verify response body appears in DEBUG logs for non-streaming requests
  • Verify no body logging when log level is INFO or higher
  • Verify streaming responses do not log response body
  • Verify no performance regression when DEBUG logging is disabled

Closes #1522

Add optional logging of HTTP request and response bodies to aid
debugging. The body content is only logged when the logger is set to
DEBUG level, gated behind isEnabledFor() checks to avoid any
performance impact in production. Response body logging is skipped
for streaming responses to prevent reading the stream prematurely.

Closes openai#1522
@crawfordxx crawfordxx requested a review from a team as a code owner April 1, 2026 14:59
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c6a3bf8431

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

log.debug("Sending HTTP Request: %s %s", request.method, request.url)

if log.isEnabledFor(logging.DEBUG):
if request.content:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Avoid reading request.content before send

Accessing request.content here can raise httpx.RequestNotRead for multipart or streaming request bodies (for example calls that use files=), because those requests are represented as streams until they are sent/read. Since this happens before the try block around self._client.send(...), enabling DEBUG logging can cause upload endpoints to fail immediately instead of sending the request; the async path has the same pattern.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow logging request body

1 participant