Re-raise model errors from Temporal model activities with their original type in workflow code

Merged
DO
2mo ago
+499-5

Under TemporalDurability, a model request runs in an activity, so a ModelAPIError it raises reached workflow code as Temporal's ActivityError. As a result, workflow-side code that handles model errors never saw one: on_model_request_error hooks, the Fallback capability in #9348, and the RetryOnModelErrors capability planned in #9334. This PR rebuilds the original error on the workflow side.

[!WARNING]
Compatibility impact: Under TemporalDurability, workflow code that catches a model failure as temporalio.exceptions.ActivityError around agent.run() no longer catches it. A failed model request now raises the original ModelAPIError (for example ModelHTTPError), with the ActivityError as its __cause__. A workflow that lets the error escape now fails with that error's type instead of ActivityError.

Why this can ship in a minor release: The ActivityError was an engine artifact. Pydantic AI documents model errors as ModelAPIError, which is what DBOS and Prefect, the in-process engines, already raise, so this brings Temporal in line with them.

Migration: Catch ModelAPIError (or ModelHTTPError) instead of ActivityError. The ActivityError is still available as the error's __cause__.

What changes

  • In a model activity (request, streamed request, message compaction, suspended-response cancellation), a ModelAPIError is raised as an ApplicationError whose details carry the error's class and its pickled fields, taken from __reduce__. The ApplicationError's type is the class name, which is what Temporal used for the raw exception, so activity retry policies and non_retryable_error_types behave exactly as before.
  • In the workflow, an ActivityError caused by such an ApplicationError is re-raised as the rebuilt error, from the ActivityError.
  • The error is encoded from the pickling protocol rather than a per-class table, so new subclasses cross without changes here, provided they define __reduce__. That includes the #9339 error categories, which are planned to be picklable module-level classes.
  • Only Pydantic AI's own classes are rebuilt. On the workflow side, the class is looked up among the ModelAPIError subclasses already loaded, so details from the wire never decide what gets imported. An application-defined subclass, or an error whose fields can't be encoded or rebuilt, still fails as an ActivityError. A field value that isn't JSON-serializable, such as an unusual response body, crosses as its string form.
  • Non-model errors are unchanged.
  • The deprecated TemporalAgent wrapper path keeps its current behavior.

Unrelated test change

The elif in test_logfire_plugin's tracer-type check in tests/durable_exec/temporal/test_model_and_serialization.py is now marked # pragma: lax no cover, like the lines inside it. CI on this branch needed it: the coverage job failed on that line alone. Which arm of the check runs depends on the installed logfire.

DBOS and Prefect

Both run model steps and tasks in-process and already raise the original exception. New tests pin this: an on_model_request_error hook in a DBOS workflow and in a Prefect flow receives ModelHTTPError with its fields.

Tests

  • Temporal workflow tests:
    • a non-streamed request whose on_model_request_error hook receives ModelHTTPError with status_code, retry_after and body;
    • a streamed request whose error reaches the workflow as ModelHTTPError;
    • a non-model error that still reaches the hook as ActivityError.
  • Unit tests for the encoding:
    • ModelHTTPError and ModelAPIError round trips, including headers and suggested_model_id;
    • a body that isn't JSON, and a body that can't be encoded;
    • the ApplicationError type;
    • application-defined subclasses and classes that can't be rebuilt;
    • malformed details;
    • activity failures without an ApplicationError.

Checklist

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

šŸ¤– Generated with Claude Code