Skip to content

fix: coerce exception code to string or None - #3611

Open
Rajeev91691 wants to merge 1 commit into
openai:mainfrom
Rajeev91691:fix/exception-code-coercion
Open

fix: coerce exception code to string or None#3611
Rajeev91691 wants to merge 1 commit into
openai:mainfrom
Rajeev91691:fix/exception-code-coercion

Conversation

@Rajeev91691

Copy link
Copy Markdown

Coerce error code inside APIError body to str | None to prevent validation errors with construct_type when the API returns integer codes (e.g. 400). Added unit test in tests/test_client.py.

@Rajeev91691
Rajeev91691 requested a review from a team as a code owner August 13, 2026 18:34

@ting-hong-shieh ting-hong-shieh left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewed b00a48a against #3531. The focused regression passes, and the normalization keeps the public str | None contract intact. One reproducible formatting failure remains below. No API call or external service was used.

Validation snapshot:

  • Focused pytest: 1 passed, 176 deselected.
  • Ruff 0.14.7 check: passed.
  • Ruff 0.14.7 format check: failed because tests/test_client.py would be reformatted.
  • git diff --check: failed at tests/test_client.py:2971.

Comment thread tests/test_client.py
# Test None code
err = APIError("message", request, body={"code": None})
assert err.code is None

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please remove this extra blank line at EOF. Using the repository-pinned Ruff 0.14.7, ruff format --check src/openai/_exceptions.py tests/test_client.py reports that this file would be reformatted; git diff --check independently reports tests/test_client.py:2971: new blank line at EOF. The focused regression itself passes (1 passed, 176 deselected).

@CacinieP

Copy link
Copy Markdown

Confirmed the remaining formatting failure — it's a single extra blank line at end of file, introduced by the new test:

$ ruff format --check src tests   # ruff 0.14.7, as in CI
Would reformat: tests/test_client.py

$ git diff --check
tests/test_client.py:2971: new blank line at EOF.

tests/test_client.py on this branch ends with assert err.code is None\n\n (two trailing newlines) while every other file ends with exactly one. Deleting the final blank line clears both the ruff format check and the git diff --check failure — the rest of the diff (src and tests) is otherwise clean under ruff format --check and ruff check.

One-liner fix:

printf '%s\n' "$(cat tests/test_client.py)" > tests/test_client.py

or just remove the last empty line in your editor and push.

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.

3 participants