feat: add request/response body logging at DEBUG level#3043
feat: add request/response body logging at DEBUG level#3043crawfordxx wants to merge 1 commit intoopenai:mainfrom
Conversation
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
There was a problem hiding this comment.
💡 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: |
There was a problem hiding this comment.
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 👍 / 👎.
Summary
Adds optional logging of HTTP request and response bodies at DEBUG level to aid debugging, addressing #1522.
log.isEnabledFor(logging.DEBUG)to avoid performance impact when not enabledSyncAPIClient) and async (AsyncAPIClient) code pathsUsage
Test plan
Closes #1522