fix: coerce APIStatusError.code to str to match Optional[str] annotation#3536
Open
Varshith-Kali wants to merge 1 commit into
Open
fix: coerce APIStatusError.code to str to match Optional[str] annotation#3536Varshith-Kali wants to merge 1 commit into
Varshith-Kali wants to merge 1 commit into
Conversation
The lenient construct_type(type_=Optional[str], ...) passes through non-str values (int, bool, etc.) unchanged, while the cast(Any, ...) silences the type checker. When an API response contains a JSON number for error.code, exc.code is an int at runtime despite the Optional[str] annotation — causing downstream AttributeError on .strip() as reported in openai#3531. Fix: replace the construct_type+cast pattern with an explicit str() coercion guarded by a None check. Existing string codes pass through unchanged; None stays None; everything else becomes its str repr. 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.
Summary
Fixes #3531 —
APIStatusError.codeis annotatedOptional[str]butconstruct_type(type_=Optional[str], value=...)passes through non-str values unchanged (int,bool, etc.), and thecast(Any, ...)silences the type checker. Downstream code that trusts the annotation and callsexc.code.strip()crashes withAttributeError.Fix
Replace the
construct_type+castpattern with an explicitstr()coercion guarded by aNonecheck:Nonecode values (int, bool, float, etc.) →str(value)None→None(unchanged)str()as identity (unchanged)Testing
Verified all six coercion cases pass:
{"code": 404}→"404"{"code": "rate_limit"}→"rate_limit"{"message": "nope"}→None{"code": true}→"True"{"code": null}→NoneNone