Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/openai/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,10 @@ def request(

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 👍 / 👎.

log.debug("Request body: %s", request.content.decode("utf-8", errors="replace"))

response = None
try:
response = self._client.send(
Expand Down Expand Up @@ -1046,6 +1050,10 @@ def request(
)
log.debug("request_id: %s", response.headers.get("x-request-id"))

if log.isEnabledFor(logging.DEBUG):
if not stream and not self._should_stream_response_body(request=request):
log.debug("Response body: %s", response.text)

try:
response.raise_for_status()
except httpx.HTTPStatusError as err: # thrown on 4xx and 5xx status code
Expand Down Expand Up @@ -1599,6 +1607,10 @@ async def request(

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

if log.isEnabledFor(logging.DEBUG):
if request.content:
log.debug("Request body: %s", request.content.decode("utf-8", errors="replace"))

response = None
try:
response = await self._client.send(
Expand Down Expand Up @@ -1645,6 +1657,10 @@ async def request(
)
log.debug("request_id: %s", response.headers.get("x-request-id"))

if log.isEnabledFor(logging.DEBUG):
if not stream and not self._should_stream_response_body(request=request):
log.debug("Response body: %s", response.text)

try:
response.raise_for_status()
except httpx.HTTPStatusError as err: # thrown on 4xx and 5xx status code
Expand Down