Skip to content

Retry should have a public way to access the predicate #15026

Open

Description

@tswast

Is your feature request related to a problem? Please describe.

In BigQuery, there are some operations like "start a query" that can actually take multiple API requests. It's usually just jobs.insert, but if there's a conflict, then we also make calls to jobs.get. In addition, these have different predicates, such as wanting to retry jobs.get on 404 in this context but not others.

Describe the solution you'd like

Ideally, I could take a user-provided generic retry and tweak if for the given scenario. For example:

get_job_retry = retry.with_predicate(
                        lambda exc: isinstance(exc, core_exceptions.NotFound)
                        # Reference the original retry to avoid recursion.
                        or retry._predicate(exc)
)

or

def do_query_predicate(exc) -> bool:
            if isinstance(exc, core_exceptions.RetryError):
                exc = exc.cause

            # Per https://github.com/googleapis/python-bigquery/issues/2134, sometimes
            # we get a 404 error. In this case, if we get this far, assume that the job
            # doesn't actually exist and try again. We can't add 404 to the default
            # job_retry because that happens for errors like "this table does not
            # exist", which probably won't resolve with a retry.
            if isinstance(exc, core_exceptions.NotFound):
                message = exc.message
                # Don't try to retry table/dataset not found, just job not found.
                # The URL contains jobs, so use whitespace to disambiguate.
                return message is not None and " job" in message.lower()

            # Reference the original job_retry to avoid recursion.
            return job_retry._predicate(exc)
)

Describe alternatives you've considered

  1. Just as I already have two retry objects in the parameters for the query() API, I could update it to have two more for these specific situations.
  2. We could also add a method with_additional_predicate that adds an or clause + this function to the previous predicate.

Additional context

googleapis/python-bigquery#2135 is an issue where I will likely add more "retry" parameters for finer-grained control, but would have preferred taking a user's retry object and amending the predicate.

Activity

  1. added
    type: feature request
    priority: p2
    on Feb 21, 2025
  2. removed their assignment
    on Feb 21, 2025
  3. vchudnov-g commented on Feb 21, 2025

    @vchudnov-g
    Contributor

    OK, we will consider a clean way to achieve this.

  4. parthea commented on Jan 12, 2026

    @parthea
    Contributor

    Transferring to google-cloud-python as the code for python-api-core will be migrated there in 1-3 weeks.

  5. Rajatendu1 commented on Nov 18, 2026

    @Rajatendu1

    Would a focused PR exposing a read-only predicate property on _BaseRetry be welcome here? That would let callers compose their existing predicate with a context-specific check through with_predicate(), without changing retry defaults or adding a second composition API.

    On current main (8d69ab5c), the property is still absent, and the shared base covers unary and streaming retries in both sync and async code. The existing 33 retry-base tests pass locally on Python 3.13. The proposed scope would include tests for all four retry classes, preservation through with_* copies, and a documented composition example. No implementation has been started.

    I see this is assigned to @tswast, so I wanted to check whether outside help is still useful before duplicating anyone's work. If the additional-predicate method is preferred instead, it would be useful to settle that API first.

    Source investigation and this scope proposal used AI assistance.

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

Metadata

Metadata

Assignees

Labels

priority: p2type: feature request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions