Stop Temporal from retrying subclasses of non-retryable errors, like `ContentFilterError` or a `UserError` subclass

Merged
DO
1mo ago
+213-28

No issue: found while auditing how errors are retried under durable execution.

TemporalDurability adds the framework's non-retryable errors (UserError, PydanticUserError, UnexpectedModelBehavior, FallbackExceptionGroup and the workspace errors) to every activity's retry policy, by class name. Temporal's non_retryable_error_types matches the exact class name, though. So a subclass, like ContentFilterError or IncompleteToolCall (both UnexpectedModelBehavior) or an application's own UserError subclass, was retried as often as the policy allows. With Temporal's default policy, that's without limit.

This came up in practice. Two capabilities already work around it by turning ContentFilterError into ModelRetry, "which keeps a durable engine from retrying the tool activity against an error class its non-retryable list doesn't name" (capabilities/image_generation.py, common_tools/image_generation.py). A user tool that runs a subagent which hits a refusal, or a model like OpenAIDecisionsModel that raises ContentFilterError itself, had no such workaround.

What changed

  • Activities. Every activity TemporalDurability registers now raises an error that's an instance of one of those classes, but not of the class itself, as a non-retryable ApplicationError. It keeps the error's class name and message, which is what Temporal reported for it before, and has the error as its cause. An error of a listed class is raised unchanged, so nothing changes for errors the policy already names.
  • Child workflows. Tools that run as child workflows mark the same errors non-retryable on the ApplicationError they already raise.
  • Single source. The list of non-retryable classes is now one tuple, and with_non_retryable_errors derives the policy's names from it. The names and their order are unchanged.
  • Docs. temporal.md says that subclasses aren't retried either.

Behavior change

An activity or tool child workflow that raises such a subclass now fails on its first attempt, instead of being retried until the retry policy runs out (never, by default). That is what the docs already promised for UnexpectedModelBehavior and UserError.

Not covered

  • Deprecated TemporalAgent. Its own model and toolset activities (TemporalModel, temporalize_*_toolset) aren't changed. They also don't carry model errors across the boundary, which TemporalDurability does, so I've left that deprecated path alone.
  • Other engines. Prefect already matches with isinstance, so subclasses are covered there. DBOS and the Harness engines (Absurd, pgtask) have no non-retryable classification at all, so this isn't the same bug there.

Tests

  • Activities (test_durability.py): each error is raised from both a model request activity and a tool activity, with a 3-attempt policy: UnexpectedModelBehavior itself (unchanged), ContentFilterError, IncompleteToolCall, and an app-defined UserError subclass. Before the fix, the subclasses ran 3 times; now each runs once and keeps its own ApplicationError type.
  • Child workflows (test_child_workflows.py): ContentFilterError from a child workflow tool. Its child workflow now has 1 attempt (3 before).

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.

🤖 Generated with Claude Code