Skip to content

fix(errors): guard APIError._get_code against non-dict response bodies - #3474

Open
dsugurtuna wants to merge 1 commit into
googleapis:mainfrom
dsugurtuna:fix/api-error-get-code-non-dict
Open

dsugurtuna wants to merge 1 commit into
googleapis:mainfrom
dsugurtuna:fix/api-error-get-code-non-dict

Conversation

@dsugurtuna

@dsugurtuna dsugurtuna commented Dec 5, 2026 •

Copy link
Copy Markdown

Fixes #3472

APIError._get_status and APIError._get_message both catch AttributeError and return None when response_json (or nested response_json['error']) does not have .get. APIError._get_code was missing that guard, so constructing APIError, ClientError, or ServerError with a falsy code (None or 0) and a non-dict response_json (such as a string, int, list, or None) raised AttributeError instead of leaving code=None.

This wraps APIError._get_code in try ... except AttributeError: return None in google/genai/errors.py to match _get_status and _get_message, and adds 18 parametrized regression cases in google/genai/tests/errors/test_api_error.py.

Checks:

  • pytest google/genai/tests/errors/: 36 passed (the 18 new cases failed with AttributeError on origin/main).
  • mypy google/genai/ passed with no issues.
  • pyink --check on test_api_error.py and ruff on changed lines passed.

Catch AttributeError in APIError._get_code and return None, matching _get_status and _get_message so constructing APIError with a falsy code and a non-dict response_json (or non-dict nested error) does not raise AttributeError.

Fixes googleapis#3472

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant