Retry Policy#
The retry API is built from three families of callables modeled after tenacity:
- Retry conditions decide whether an exception should be retried.
- Wait strategies decide how long to sleep before the next attempt.
- Stop conditions decide when retries should stop.
You can compose them into a policy with retry_policy(retry=..., wait=..., stop=...).
Retry conditions support | and &, wait strategies support +, and stop
conditions support | and &.
Quick Example#
from workflows.retry_policy import (
retry_policy,
retry_if_exception_message,
retry_if_exception_type,
stop_after_attempt,
stop_before_delay,
wait_fixed,
wait_random,
)
policy = retry_policy(
retry=retry_if_exception_type((TimeoutError, ConnectionError))
| retry_if_exception_message(match="rate limit|temporarily unavailable"),
wait=wait_fixed(1) + wait_random(0, 1),
stop=stop_after_attempt(5) | stop_before_delay(30),
)
Policy Constructor#
RetryPolicy
#
Bases: Protocol
Structural interface for step retry policies.
Any object with a compatible next method satisfies this protocol,
including policies built with retry_policy(), ConstantDelayRetryPolicy,
ExponentialBackoffRetryPolicy, and user-defined policies.
Most users do not implement this protocol directly. Instead, construct a
policy with retry_policy(retry=..., wait=..., stop=...) and combine
retry conditions, wait strategies, and stop conditions with the operators
supported by this module.
Examples:
from workflows.retry_policy import (
retry_policy,
retry_if_exception_type,
stop_after_attempt,
wait_exponential,
)
policy = retry_policy(
retry=retry_if_exception_type((TimeoutError, ConnectionError)),
wait=wait_exponential(multiplier=1, exp_base=2, max=30),
stop=stop_after_attempt(5),
)
See Also
Source code in workflows/retry_policy.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
next
#
next(elapsed_time: float, attempts: int, error: Exception, *, seed: int | None = None) -> float | None
Decide if another retry should occur and the delay before it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
elapsed_time
|
float
|
Seconds since the first failure. |
required |
attempts
|
int
|
Number of failures so far, starting at 1 for the first failure. |
required |
error
|
Exception
|
The last exception encountered. |
required |
seed
|
int | None
|
Optional RNG seed for deterministic jitter (DBOS replay). |
None
|
Returns:
| Type | Description |
|---|---|
float | None
|
Seconds to wait before retrying, or |
Source code in workflows/retry_policy.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
retry_policy
#
retry_policy(retry: RetryCondition | None = None, wait: WaitStrategy = wait_fixed(5), stop: StopCondition = stop_after_attempt(3)) -> RetryPolicy
Construct a composable retry policy from retry, wait, and stop components.
This is the primary way to create retry policies. Combine retry conditions,
wait strategies, and stop conditions using operators (|, &, +)
or the named combinators.
Examples:
from workflows.retry_policy import (
retry_policy,
retry_if_exception_type,
stop_after_attempt,
wait_exponential,
)
policy = retry_policy(
retry=retry_if_exception_type((TimeoutError, ConnectionError)),
wait=wait_exponential(multiplier=1, exp_base=2, max=30),
stop=stop_after_attempt(5),
)
With no arguments, retry_policy() retries all exceptions up to 3
attempts with a 5-second fixed delay between each.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
retry
|
RetryCondition | None
|
Predicate that decides whether an exception is retryable.
When |
None
|
wait
|
WaitStrategy
|
Strategy that computes the delay before the next attempt.
Defaults to |
wait_fixed(5)
|
stop
|
StopCondition
|
Predicate that decides when to give up.
Defaults to |
stop_after_attempt(3)
|
Returns:
| Type | Description |
|---|---|
RetryPolicy
|
A |
Source code in workflows/retry_policy.py
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 | |
Retry Introspection#
Types exposed to step bodies via Context.retry_info() and to @catch_error
handlers via StepFailedEvent.exception.
RetryInfo
dataclass
#
Snapshot of the currently-executing step's retry state.
Returned by Context.retry_info(). On the first attempt retry_number
is 0, elapsed_seconds is 0.0, and both last_exception and
last_failed_at are None. On subsequent retries they describe the
most recent prior failure.
Attributes:
| Name | Type | Description |
|---|
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
retry_number
|
str
|
|
required |
elapsed_seconds
|
str
|
|
required |
last_exception
|
str
|
|
required |
last_failed_at
|
str
|
|
required |
Source code in workflows/retry_policy.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
Retry Conditions#
Modeled after tenacity retry functions.
retry_if_exception
#
Bases: _RetryConditionBase
Retry when the raised exception satisfies a custom predicate.
Use this when your retry decision depends on exception details that are not covered by the built-in helpers.
Examples:
retry_if_exception(lambda error: "rate limit" in str(error).lower())
Source code in workflows/retry_policy.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
retry_if_exception_type
#
Bases: retry_if_exception
Retry only when the exception is an instance of one of the given types.
This is the most common retry predicate for transient network and provider failures.
Examples:
retry_if_exception_type((TimeoutError, ConnectionError))
Source code in workflows/retry_policy.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | |
retry_if_not_exception_type
#
Bases: retry_if_exception
Retry unless the exception is an instance of one of the given types.
This is useful when most failures are retryable except for a small set of known permanent errors.
Examples:
retry_if_not_exception_type((ValueError, PermissionError))
Source code in workflows/retry_policy.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
retry_unless_exception_type
#
Bases: retry_if_not_exception_type
Retry unless the exception is an instance of one of the given types.
Tenacity-style alias for retry_if_not_exception_type.
Examples:
retry_unless_exception_type(AuthenticationError)
Source code in workflows/retry_policy.py
256 257 258 259 260 261 262 263 264 265 266 267 268 | |
retry_if_exception_message
#
Bases: _RetryConditionBase
Retry when the exception message matches an exact string or regex pattern.
Pass either message for an exact string match or match for a regular
expression. Passing both is an error.
Examples:
retry_if_exception_message(message="please retry")
retry_if_exception_message(match=r"HTTP 5\d\d")
Source code in workflows/retry_policy.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
retry_if_not_exception_message
#
Bases: retry_if_exception_message
Retry when the exception message does not match the given string or regex.
This is useful when a provider uses specific messages to signal permanent failures that should stop retries.
Examples:
retry_if_not_exception_message(match="invalid_api_key|permission denied")
Source code in workflows/retry_policy.py
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | |
retry_if_exception_cause_type
#
Bases: _RetryConditionBase
Retry when any exception in the __cause__ chain matches the given type.
Only explicit exception chaining (raise X from Y) is followed. Implicit
chaining via __context__ is not inspected, matching tenacity's
behavior. If you need to match implicitly chained exceptions, use
retry_if_exception with a custom predicate that walks __context__.
Examples:
retry_if_exception_cause_type(ConnectionError)
Source code in workflows/retry_policy.py
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | |
retry_any
#
Bases: _RetryConditionBase
Retry if any of the provided retry predicates match.
Equivalent to combining retry predicates with |.
Examples:
retry_any(
retry_if_exception_type(ConnectionError),
retry_if_exception_message(match="rate limit"),
)
Source code in workflows/retry_policy.py
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | |
retry_all
#
Bases: _RetryConditionBase
Retry if all of the provided retry predicates match.
Equivalent to combining retry predicates with &.
Examples:
retry_all(
retry_if_exception_type(RuntimeError),
retry_if_exception_message(match="temporary"),
)
Source code in workflows/retry_policy.py
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 | |
retry_always
#
Bases: _RetryConditionBase
Retry condition that always retries.
This is mainly useful when you want to be explicit in a composed policy.
Examples:
retry_policy(retry=retry_always(), stop=stop_after_attempt(3))
Source code in workflows/retry_policy.py
403 404 405 406 407 408 409 410 411 412 413 414 415 416 | |
retry_never
#
Bases: _RetryConditionBase
Retry condition that never retries.
This can be useful in tests or to disable one branch of a composed retry expression.
Examples:
retry_never() | retry_if_exception_type(ConnectionError)
Source code in workflows/retry_policy.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | |
Wait Strategies#
Modeled after tenacity wait functions.
wait_fixed
#
Bases: _WaitStrategyBase
Wait a fixed number of seconds between attempts.
Examples:
wait_fixed(5)
Source code in workflows/retry_policy.py
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | |
wait_none
#
Bases: wait_fixed
Wait strategy that does not delay retries.
Examples:
wait_none()
Source code in workflows/retry_policy.py
453 454 455 456 457 458 459 460 461 462 463 464 | |
wait_exponential
#
Bases: _WaitStrategyBase
Wait with exponentially increasing delays, clamped between min and max.
The delay for retry n is multiplier * exp_base**n before clamping,
with n=0 for the first retry.
Examples:
wait_exponential(multiplier=1, exp_base=2, max=60)
Source code in workflows/retry_policy.py
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 | |
wait_incrementing
#
Bases: _WaitStrategyBase
Wait an incrementally larger amount after each attempt.
The delay is start for the first retry and increases by increment
on each subsequent retry, capped by max and never going below zero.
Examples:
wait_incrementing(start=1, increment=2, max=10)
Source code in workflows/retry_policy.py
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 | |
wait_random
#
Bases: _WaitStrategyBase
Wait a random duration uniformly sampled from [min, max].
When the workflow runtime provides a seed, the sampled value is
deterministic across replayed runs.
Examples:
wait_random(min=0.5, max=1.5)
Source code in workflows/retry_policy.py
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 | |
wait_exponential_jitter
#
Bases: _WaitStrategyBase
Exponential backoff with additive random jitter.
The deterministic base delay grows exponentially and a random value in
[0, jitter] is added on top.
Examples:
wait_exponential_jitter(initial=1, exp_base=2, max=60, jitter=1)
Source code in workflows/retry_policy.py
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | |
wait_random_exponential
#
Bases: _WaitStrategyBase
Exponential backoff with full jitter.
A random delay is sampled between min and the exponential upper bound
for the current attempt.
Examples:
wait_random_exponential(multiplier=1, exp_base=2, max=60)
Source code in workflows/retry_policy.py
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 | |
wait_chain
#
Bases: _WaitStrategyBase
Use a different wait strategy for each retry in order, starting with the first.
After the provided strategies are exhausted, the last strategy is reused for all subsequent retries.
Examples:
wait_chain(wait_fixed(1), wait_fixed(2), wait_fixed(5))
Source code in workflows/retry_policy.py
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 | |
wait_combine
#
Bases: _WaitStrategyBase
Combine multiple wait strategies by summing their delays.
Equivalent to combining waits with +.
Examples:
wait_combine(wait_fixed(1), wait_random(0, 1))
Source code in workflows/retry_policy.py
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 | |
wait_full_jitter
#
wait_full_jitter(multiplier: int | float = 1.0, exp_base: int | float = 2.0, max: time_unit_type = 60.0, min: time_unit_type = 0.0) -> wait_random_exponential
Alias for wait_random_exponential.
Examples:
wait_full_jitter(multiplier=1, exp_base=2, max=60)
Source code in workflows/retry_policy.py
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 | |
Stop Conditions#
Modeled after tenacity stop functions.
stop_after_attempt
#
Bases: _StopConditionBase
Stop after a fixed number of attempts.
Examples:
stop_after_attempt(5)
Source code in workflows/retry_policy.py
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 | |
stop_after_delay
#
Bases: _StopConditionBase
Stop after a maximum elapsed time in seconds.
Examples:
stop_after_delay(30)
Source code in workflows/retry_policy.py
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 | |
stop_before_delay
#
Bases: _StopConditionBase
Stop if the next sleep would move the retry past the configured limit.
Unlike stop_after_delay, this condition considers the upcoming_sleep
value produced by the wait strategy.
Examples:
stop_before_delay(30)
Source code in workflows/retry_policy.py
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 | |
stop_any
#
Bases: _StopConditionBase
Stop if any of the provided stop predicates match.
Equivalent to combining stop conditions with |.
Examples:
stop_any(stop_after_attempt(5), stop_after_delay(30))
Source code in workflows/retry_policy.py
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 | |
stop_all
#
Bases: _StopConditionBase
Stop if all of the provided stop predicates match.
Equivalent to combining stop conditions with &.
Examples:
stop_all(stop_after_attempt(5), stop_after_delay(30))
Source code in workflows/retry_policy.py
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 | |
stop_never
#
Bases: _StopConditionBase
Stop condition that never stops.
This is typically paired with a retry predicate or workflow timeout that provides the real upper bound.
Examples:
stop_never()
Source code in workflows/retry_policy.py
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 | |
Deprecated Constructors#
The following helpers predate the composable API and are kept for
backwards compatibility. Prefer retry_policy(...) with explicit retry,
wait, and stop arguments.
ConstantDelayRetryPolicy
#
ConstantDelayRetryPolicy(maximum_attempts: int = 3, delay: float = 5) -> RetryPolicy
Retry at a fixed interval up to a maximum number of attempts.
Deprecated: use retry_policy(wait=wait_fixed(delay), stop=stop_after_attempt(n)) instead.
Examples:
ConstantDelayRetryPolicy(delay=5, maximum_attempts=10)
Source code in workflows/retry_policy.py
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 | |
ExponentialBackoffRetryPolicy
#
ExponentialBackoffRetryPolicy(maximum_attempts: int = 5, initial_delay: float = 1.0, multiplier: float = 2.0, max_delay: float = 60.0, jitter: bool = True) -> RetryPolicy
Retry with exponentially increasing delays, optional jitter, and a cap.
Deprecated: use retry_policy(wait=wait_exponential(...), stop=stop_after_attempt(n)) instead.
For jitter, use wait_random_exponential or wait_exponential_jitter.
Examples:
ExponentialBackoffRetryPolicy(
initial_delay=1,
multiplier=2,
max_delay=30,
maximum_attempts=5,
jitter=True,
)
Source code in workflows/retry_policy.py
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 | |