Wait a model error's `retry_after` durably before DBOS retries a model request, via a shared `model_error_retry_after` helper

Merged
DO
18d ago
+490-8

Part of #13267, which the stacked harness PR #13283 closes.

Why

A model error's retry_after is the wait a provider asked for (Retry-After, retry-after-ms, Gemini RetryInfo). Temporal already waits it before retrying a model request activity (#12106); the other engines ignore it. This PR adds the shared piece and covers the core engines (tracked in #13267):

  • Temporal: unchanged behaviour, now via the shared helper.
  • DBOS: DBOS retries a step in-process after an asyncio.sleep that recovery doesn't remember, with no per-failure delay hook. When model_step_config allows another attempt and the model error has a retry_after, the model step now records the attempt (returns the wait instead of failing), the workflow waits it with a durable DBOS.sleep_async, and runs the step again with the attempts left, carrying on DBOS's backoff. Cap: DBOS's backoff for that attempt or 60 s, whichever is longer.
  • Prefect: no durable timer and no per-failure delay, so it's documented that retry_delay_seconds applies. No in-process sleep added.

New public surface

  • pydantic_ai.durable_exec.model_error_retry_after(error, *, maximum) -> float | None: engine-builder helper that finds a model error's retry_after in an exception chain (stopping at raise ... from None) and caps it.

User-visible behavior

 DBOSDurability(model_step_config=StepConfig(retries_allowed=True, max_attempts=3))
 # model raises ModelRateLimitError(retry_after=20)
-steps: model.request (DBOS sleeps 1 s in-process, then retries inside the step)
+steps: model.request, DBOS.sleep (20 s, durable), model.request

Interaction with #11932 (RetryOnModelErrors)

RetryOnModelErrors retries inside the agent run, so under a durable engine it waits inside the durable unit (or in workflow code), not as the engine's retry delay. This PR covers the engine's own retries of the model request's durable unit. The two stack like the other retry layers. Leave retries to the engine if you want Retry-After waited durably. No code from #11932 is touched.

Verification

  • tests/durable_exec/test_retry_after.py: helper chain walk, cap, from None.
  • tests/durable_exec/test_dbos.py: test_dbos_durability_model_error_retry_after_waits_durably (step/sleep/step journal, replay via fork), ..._counts_every_attempt, ..._continues_the_backoff, ..._caps_at_dbos_maximum_backoff, ..._outside_workflow_code_fails_as_before, test_dbos_retry_after_uses_dbos_step_defaults.
  • Existing Temporal tests cover the refactor: tests/durable_exec/temporal/test_durability.py::test_durability_model_error_retry_after_delays_next_attempt, tests/durable_exec/temporal/test_model_and_serialization.py::test_a_model_error_that_cannot_be_rebuilt_still_carries_its_retry_delay and ::test_a_non_retryable_model_error_gets_no_retry_delay.

What changes for existing users

DBOS users with model_step_config retries now wait a provider's Retry-After durably before the next model request attempt, instead of DBOS's in-process backoff; nothing else changes.

PR decisions

2026-11-12 · Per-engine Retry-After mechanisms · iter -

  • Decision: Temporal native (unchanged); DBOS: workflow-side retry loop with DBOS.sleep_async for retry_after errors; Lambda: wrap retry_strategy; Azure: orchestrator-side durable timer; Absurd/pgtask: record attempt + durable sleep_for bounded by remaining attempts; Prefect and Render: document-only, no in-process sleep
  • Why: Only Temporal and Lambda have a native per-failure delay; 'No silent in-process sleeps anywhere' (delegating agent, deciding for Douwe)
  • Source: https://github.com/pydantic/pydantic-ai/issues/13267

2026-11-12 · Public retry_after helper · iter -

  • Decision: Add a minimal public engine-builder helper to pydantic_ai.durable_exec with a keyword-only cap; Temporal uses it
  • Why: Harness engines build on the public durable_exec surface, same tier as replay_scope/record_durable_unit
  • Source: https://github.com/pydantic/pydantic-ai/issues/13267

2026-11-12 · Lambda cap rule · iter -

  • Decision: delay = min(retry_after, max(strategy's own delay for this attempt, 60s)); keep the strategy's should_retry; no new parameter
  • Why: A retry_strategy is an opaque callable with no readable maximum; never retry sooner than the user's strategy would
  • Source: https://github.com/pydantic/pydantic-ai/issues/13267

2026-11-12 · Absurd and pgtask caps · iter -

2026-11-12 · PR split · iter -

2026-11-12 · DBOS mechanism and cap · iter -

  • Decision: Model step returns a {pydantic_ai_retry_after, attempts} marker instead of failing on a retry_after error (when not its last attempt, read via DBOS.step_status); workflow code waits DBOS.sleep_async and re-runs the step via DBOS.run_step_async with the attempts left; cap = max(DBOS backoff for the attempt, 60s), same rule as Lambda
  • Why: DBOS has no per-failure delay hook and its in-step retry sleep isn't durable; returning a value records the failed attempt so replay is deterministic without depending on should_retry or exception serialization; DBOSAgent (deprecated) unchanged
  • Source: https://github.com/pydantic/pydantic-ai/issues/13267

2026-11-12 · pre-push review 1 of 3 at ac36023cbc5d516229e6c267c6d9f35a457b3607 · iter -

2026-11-12 · pre-push review 1 of 3 at ac36023cbc5d516229e6c267c6d9f35a457b3607 · iter -

2026-11-12 · pre-push review 2 of 3 at 75532f8cce · iter -

2026-11-12 · pre-push review 2 of 3 at 75532f8cce · iter -

2026-11-12 · test-only fix after review 2 · iter -

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.