Skip to content

Commit 121799e

Browse files
author
giulio-leone
committed
fix: apply code review suggestions
Apply bot code suggestions from PR review.
1 parent 24438b0 commit 121799e

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

src/openai/_exceptions.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,18 @@ class ContentFormatError(OpenAIError):
169169

170170
raw_content: str
171171
"""The raw content string returned by the API that failed to parse."""
172+
# Avoid including the full raw_content in the exception message to prevent
173+
# leaking large or sensitive responses into logs/tracebacks. A truncated
174+
# preview is included instead; the full content is available via the
175+
# `raw_content` attribute for debugging purposes.
176+
preview = raw_content
177+
max_preview_length = 200
178+
if len(preview) > max_preview_length:
179+
preview = preview[:max_preview_length] + "... [truncated]"
172180

173-
expected_response_format: str | None
174-
"""Best-effort name of the expected response format type."""
181+
super().__init__(
182+
f"Could not parse response content as the response did not match the expected format: {error}. "
183+
f"Raw content preview: {preview!r}"
175184

176185
error: Exception
177186
"""The underlying parsing exception."""

src/openai/lib/_parsing/_completions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ def _parse_content(response_format: type[ResponseFormatT], content: str) -> Resp
251251

252252
return pydantic.TypeAdapter(response_format).validate_json(content)
253253
except (pydantic.ValidationError, json.JSONDecodeError) as exc:
254-
raise ContentFormatError(raw_content=content, error=exc, response_format=response_format) from exc
254+
expected_type = getattr(response_format, "__qualname__", getattr(response_format, "__name__", repr(response_format)))
255+
raise ContentFormatError(raw_content=content, error=exc, expected_type=expected_type) from exc
255256

256257
raise TypeError(f"Unable to automatically parse response format type {response_format}")
257258

0 commit comments

Comments
 (0)