Skip to content

fix(errors): guard APIError._get_code on non-dict response bodies - #3475

Open
Roxen-AS wants to merge 2 commits into
googleapis:mainfrom
Roxen-AS:fix/api-error-non-dict-code
Open

Roxen-AS wants to merge 2 commits into
googleapis:mainfrom
Roxen-AS:fix/api-error-non-dict-code

Conversation

@Roxen-AS

@Roxen-AS Roxen-AS commented Dec 5, 2026

Copy link
Copy Markdown

Guarded APIError._get_code against non-dict response bodies (such as websocket close reasons, integers, or multi-entry list payloads) and non-dict 'error' fields so constructing APIError with a falsy code returns code=None instead of raising AttributeError, matching _get_status and _get_message.

Fixes #3472

Signed-off-by: Roxen-AS <akash.arjun12@gmail.com>

@Sun-Wukhan Sun-Wukhan 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.

This fixes #3472, which I filed, and the shape of the fix is better than a plain try around the old expression. Two points worth recording:

  1. The hasattr(error, 'get') check preserves a case the old code got wrong even for dict bodies. The old expression evaluated response_json.get('error', {}).get('code', None) eagerly as the default argument, so {'code': 400, 'error': None} raised AttributeError even though the top-level code was present. This version returns 400 there, and the added test_constructor_falsy_code_dict_with_top_level_code_and_null_error pins exactly that case. The duplicate PR #3474 takes the plain try approach and still returns None for that body, losing the 400, so this PR is the one I would keep of the two.

  2. Precedence is unchanged on the normal paths: a top-level code still wins over error.code, and a body of only {'error': {'code': 400}} still yields 400 through the nested lookup.

Two small test suggestions, neither blocking:

  • The nested-only case {'error': {'code': 400}} with a falsy constructor code is the path this refactor rewires most, and the new tests only reach a nested code inside a list body, where the guard returns None before any nested lookup happens. One direct dict case would pin that the nested lookup still works.
  • #3474's tests also parametrize the ClientError and ServerError subclasses. The constructor path is shared, so this is belt and braces, but one subclass case would close the loop on the issue report, which named all three classes.

Signed-off-by: Roxen-AS <akash.arjun12@gmail.com>
@Roxen-AS

Roxen-AS commented Dec 6, 2026

Copy link
Copy Markdown
Author

Thanks for the review @Sun-Wukhan! Added both test cases to google/genai/tests/errors/test_api_error.py:

  • Parametrized APIError, ClientError, and ServerError over the non-dict and {'error': None} payloads.
  • Added test_constructor_falsy_code_nested_only_dict covering falsy code (None, 0) with {'error': {'code': 400}}.

Verified locally (pytest google/genai/tests/errors/: 36 passed, mypy google/genai/errors.py: 0 issues).

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

2 participants