You're encountering a common issue in Temporal workflows: when an activity times out or fails and the workflow fails, cleanup or final state updates may not execute if they're placed after the point of failure — especially if exceptions aren't properly caught.
In your current code:
- You have a
try block wrapping most of the activities.
- You catch
ApplicationError and set the status to FAILED.
- But if an activity times out, it raises a
TimeoutError, which is not caught by except ApplicationError.
So if extract_venture_from_dynamic_sections (or any intermediate activity) times out, the exception will be a TimeoutError, your except ApplicationError block won't trigger, and the final update_agentic_extraction_status for FAILED is never reached.
✅ Solution: Use except Exception or catch TimeoutError
To ensure that your report status is always updated — even on timeouts — you need to catch all possible exceptions, including TimeoutError.
Here’s how to fix your workflow:
from temporalio.exceptions import TimeoutError
from temporalio.common import RetryPolicy
from datetime import timedelta
@workflow.defn
class ExtractDataFromDynamicSections:
@workflow.run
async def run(self, payload: ExtractDataFromDynamicSectionsPayload) -> None:
activity_options = {
"start_to_close_timeout": timedelta(minutes=20),
"retry_policy": default_retry_policy,
}
await workflow.execute_activity(
reset_report,
args=[payload.report_id],
**activity_options,
)
await workflow.execute_activity(
update_agentic_extraction_status,
args=[payload.report_id, Report.AgenticExtractionStatus.RUNNING],
**activity_options,
)
try:
await workflow.execute_activity(
extract_venture_from_dynamic_sections,
args=[payload.report_id],
**activity_options,
)
await workflow.execute_activity(
extract_people_from_dynamic_sections,
args=[payload.report_id],
**activity_options,
)
await workflow.execute_activity(
extract_report_details_from_dynamic_sections,
args=[payload.report_id],
**activity_options,
)
await workflow.execute_activity(
extract_skill_matrix_from_dynamic_sections,
args=[payload.report_id],
**activity_options,
)
# Only set SUCCESS if all activities succeed
await workflow.execute_activity(
update_agentic_extraction_status,
args=[payload.report_id, Report.AgenticExtractionStatus.SUCCESS],
**activity_options,
)
except (ApplicationError, TimeoutError, Exception): # Catch relevant errors
await workflow.execute_activity(
update_agentic_extraction_status,
args=[payload.report_id, Report.AgenticExtractionStatus.FAILED],
**activity_options,
)
raise # Re-raise to mark workflow as failed