Visitar URL original
HTTP 400 / preconditionFailed should be retried · Issue #2723 · googleapis/google-api-python-client · GitHub
Skip to content

HTTP 400 / preconditionFailed should be retried #2723

Description

@ttung

Environment details

  • OS type and version: MacOS 26
  • Python version: Python 3.12.11
  • pip version: uv
  • google-api-python-client version: n/a

Steps to reproduce

Intermittent HTTP 400 / preconditionFailed on Gmail API. A casual web search suggests that other Google APIs also issue preconditionFailed. At least in my use case, retrying with exponential backoff always works. I cannot assert that in all use cases that this is the correct behavior.

Code example

N/A

Stack trace

Activity

  1. codeXsidd commented on Mar 21, 2026

    @codeXsidd
    Image

    I have investigated and implemented a fix for this issue.

    Root Cause:
    The _should_retry_response function in googleapiclient/http.py relied on HTTP response codes to trigger retries but previously only parsed the JSON error details for 403 Forbidden statuses (specifically looking for userRateLimitExceeded and rateLimitExceeded). It did not extend this logic to 400 Bad Request errors.

    Solution:
    I updated _should_retry_response to parse the JSON content for both 403 and 400 status codes.
    If the API returns a 400 Bad Request, the client will examine the JSON error response block, and if the "reason" is either "failedPrecondition" or "preconditionFailed", the response is now flagged to be retried with exponential backoff.

    Changes made:

    • googleapiclient/http.py: Refactored the JSON parsing block in _should_retry_response to apply to http_client.BAD_REQUEST as well. Added a condition to return True for retrying when encountering a failedPrecondition reason.
    • tests/test_http.py: Added a test_retry_400_failed_precondition mock test to verify the client correctly enters the sleep/retry loop rather than failing fast when a 400 preconditions error is received.

    It handles the issue perfectly using the project's existing error-parsing conventions. Let me know if you need anything else!

  2. added 2 commits that reference this issue on Mar 21, 2026
    50a5f32
    806eab9
  3. added
    priority: p2Moderately-important priority. Fix may not be included in next release.
    type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    priority: p3Desirable enhancement or fix. May not be included in next release.
    and removed
    type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.
    on Jun 15, 2026
  4. removed
    priority: p2Moderately-important priority. Fix may not be included in next release.
    on Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

priority: p3Desirable enhancement or fix. May not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions