fix: coerce API error code/param/type to str - #3759
Open
Manny7717 wants to merge 1 commit into
Open
Conversation
APIError.code is annotated Optional[str] but construct_type is a lenient deserializer: it passes non-str values through unchanged, and the cast(Any, ...) silenced the type checker. Servers (api.openai.com under load, and OpenAI-compatible gateways) can send integer error codes, so exc.code could be an int at runtime and crash downstream consumers doing exc.code.strip(). Coerce non-None values to str in the exception constructor so the runtime value always matches the annotation. The raw value stays available on APIError.body. Fixes openai#3531.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes being requested
Fixes #3531 —
APIStatusError.codeis annotatedOptional[str]but can be anintat runtime.Problem:
APIError.__init__buildscode/param/typewith the lenient deserializer:construct_typeneither coerces nor rejects values that don't match the target type — it returns them unchanged — and thecast(Any, ...)suppresses the type checker. So when a server sends an integer error code (observed from api.openai.com under load, and routinely from OpenAI-compatible gateways),exc.codeis anintwhile the annotation promisesstr | None. Downstream code that trusts the annotation (e.g.exc.code.strip()) crashes withAttributeErrorafter the API failure already happened.Fix: coerce non-
Nonevalues tostrin the exception constructor, so the runtime value always matches the documented annotation. The raw value remains available onAPIError.bodyfor consumers that need it. Applied consistently tocode,paramandtype(all three share the same pattern).Tests: added
test_api_status_error_code_is_coerced_to_strto both the sync and async client suites (end-to-end via a mocked 400 response with"code": 404). Verified revert-proven: both new tests fail on the pre-fix code and pass with the fix. Fulltests/test_client.py: 200 passed, 2 skipped.ruff check .andmypy src/openai/_exceptions.pyclean.Note:
src/openai/_exceptions.pyis handwritten — it carries no Castiron "File generated from our OpenAPI spec" marker — so this change won't be overwritten by the generator.Additional context & links
APIStatusError.codeis typedOptional[str]but can be anintat runtime #3531