Title: Issue · GitHub
Open Graph Title: Issue · googleapis/python-api-core
X Title: Issue · googleapis/python-api-core
Description: This library has moved to https://github.com/googleapis/google-cloud-python/tree/main/packages/google-api-core - Issue · googleapis/python-api-core
Open Graph Description: This library has moved to https://github.com/googleapis/google-cloud-python/tree/main/packages/google-api-core - Issue · googleapis/python-api-core
X Description: This library has moved to https://github.com/googleapis/google-cloud-python/tree/main/packages/google-api-core - Issue · googleapis/python-api-core
Opengraph URL: https://github.com/googleapis/python-api-core
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Exception chain lost when retry mechanism re-raises non-retryable errors","articleBody":"#### Environment details\n\n - OS type and version: macOS 26.1\n - Python version: 3.12.12\n - pip version: 25.3\n - `google-api-core` version: HEAD = [f8bf6f9610f3e0e7580f223794c3906513e1fa73](https://github.com/googleapis/python-api-core/commit/f8bf6f9610f3e0e7580f223794c3906513e1fa73)\n\n#### Description\n\nThe retry mechanism clears the `__cause__` attribute when re-raising non-retryable exceptions, breaking explicit exception chaining created with `raise ... from ...`.\n\n#### Steps to reproduce\n\n 1. Use the `@retry_async.AsyncRetry` decorator\n 2. Raise a non-retryable exception with a cause\n 3. Catch the exception and try to read or log its cause\n\n#### Code example\n\n```python\nimport asyncio\nfrom google.api_core import retry_async, exceptions\n\n@retry_async.AsyncRetry(predicate=retry_async.if_exception_type(exceptions.InternalServerError))\nasync def example():\n try:\n raise exceptions.Unauthenticated(\"Invalid credentials\")\n except exceptions.Unauthenticated as exc:\n raise ValueError(\"Access denied\") from exc # Explicit chaining\n\nasync def main():\n # After going through retry:\n try:\n await example()\n except ValueError as e:\n print(e.__cause__) # Expected: \"401 Invalid credentials\"; Actual: None\n\nif __name__ == \"__main__\":\n asyncio.run(main())\n```\n\nSee https://github.com/googleapis/python-api-core/pull/879/changes for a unit test that fails with the current code and passes with the proposed fix.\n\n#### Root Cause\n\nIn [retry_base.py:168](https://github.com/googleapis/python-api-core/blob/f8bf6f9610f3e0e7580f223794c3906513e1fa73/google/api_core/retry/retry_base.py#L168), `_default_exception_factory()` returns `(exc_list[-1], None)`, which causes raise `final_exc` from `None` on line 214, explicitly clearing `__cause__`.\n\n#### Proposed fix\n\nPreserve the exception's __cause__ attribute:\n\n```python\n# Before\nreturn exc_list[-1], None\n\n# After\nfinal_exc = exc_list[-1]\ncause = getattr(final_exc, '__cause__', None)\nreturn final_exc, cause\n```\n\nThis maintains exception chains for better debugging and meets developer expectations for exception handling.\n\n#### Draft pull request\n\nhttps://github.com/googleapis/python-api-core/pull/879","author":{"url":"https://github.com/fercor-cisco","@type":"Person","name":"fercor-cisco"},"datePublished":"2025-12-20T01:30:16.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/880/python-api-core/issues/880"}
| route-pattern | /:user_id/:repository/issues/:id(.:format) |
| route-controller | issues |
| route-action | show |
| fetch-nonce | v2:0d24dda6-2762-fa9f-0db6-e6fe8f38e118 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 91D4:1BDC7:10361EA:16A2F73:6A4DF214 |
| html-safe-nonce | 31217f0ed35b2bc24def04ae7b4a619e810bb5074d8ced6acaa7ff49db1ec9c8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MUQ0OjFCREM3OjEwMzYxRUE6MTZBMkY3Mzo2QTRERjIxNCIsInZpc2l0b3JfaWQiOiI4OTIzMDgyOTEwNDQyODQ0NjkyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 3f7839116ceeb02b2db57b7b0ce01f0ff3df37c3da4e74c6f28d61827103bebb |
| hovercard-subject-tag | repository:226992456 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/googleapis/python-api-core/issues/880 |
| twitter:image | https://opengraph.githubassets.com/3ffccb136b2931a1a317ea19b17623cccd16bd4d2b36ea24bc2be65889f865e9/googleapis/python-api-core |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3ffccb136b2931a1a317ea19b17623cccd16bd4d2b36ea24bc2be65889f865e9/googleapis/python-api-core |
| og:image:alt | This library has moved to https://github.com/googleapis/google-cloud-python/tree/main/packages/google-api-core - Issue · googleapis/python-api-core |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5818716c93c6a2925b815402541a32814e43a7b1261c322b0c2df75224289566 |
| turbo-cache-control | no-cache |
| go-import | github.com/googleapis/python-api-core git https://github.com/googleapis/python-api-core.git |
| octolytics-dimension-user_id | 16785467 |
| octolytics-dimension-user_login | googleapis |
| octolytics-dimension-repository_id | 226992456 |
| octolytics-dimension-repository_nwo | googleapis/python-api-core |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 226992456 |
| octolytics-dimension-repository_network_root_nwo | googleapis/python-api-core |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f4bb89367ca678f057d79b1abc45d6675b1bd5b2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width