Skip to content

[Python] Feat: Compensation pattern on the Context#3620

Draft
mrkaye97 wants to merge 4 commits intomk/mq-for-status-updatesfrom
mk/python-saga
Draft

[Python] Feat: Compensation pattern on the Context#3620
mrkaye97 wants to merge 4 commits intomk/mq-for-status-updatesfrom
mk/python-saga

Conversation

@mrkaye97
Copy link
Copy Markdown
Contributor

Description

First pass at adding first-class support on the context for compensation patterns to make exception handling, especially around child spawning, nicer and easier to reason about

Basic example:

def saga_1() -> int:
    print("Performing operation 1")
    return 42

def saga_2() -> None:
    print("Performing operation 2")

    raise Exception("Something went wrong in operation 2")


@hatchet.task()
def simple(input: EmptyModel, ctx: Context) -> None:
    saga = ctx.begin_compensation_chain()

    s1 = saga.add(
        operation_fn=saga_1,
        compensation_fn=lambda: print("Compensating operation 1"),
    )

    print("s1:", s1)

    s2 = saga.add(
        operation_fn=saga_2,
        compensation_fn=lambda: print("Compensating operation 2"),
    )

    print("s2:", s2)

Which results in:

Performing operation 1
s1: 42
Performing operation 2
Compensating operation 2
Compensating operation 1
[ERROR] 🪓 -- 2026-04-14 20:01:43,928 - failed step run: simple:simple/d51f0318-5df9-47d3-aa8b-2c68982d794c
Exception: Something went wrong in operation 2
Traceback (most recent call last):
  ...
  File "/Users/matt/Documents/GitHub/hatchet/sdks/python/examples/simple/worker.py", line 21, in saga_2
    raise Exception("Something went wrong in operation 2")
Exception: Something went wrong in operation 2

Would still need to figure out how to support async, etc

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Documentation change (pure documentation change)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (non-breaking changes to code which doesn't change any behaviour)
  • CI (any automation pipeline changes)
  • Chore (changes which are not directly related to any business logic)
  • Test changes (add, refactor, improve or change a test)
  • This change requires a documentation update

What's Changed

  • Add a list of tasks or features here...

@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 15, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
hatchet-docs Ignored Ignored Preview Apr 15, 2026 0:10am

Request Review

@mrkaye97 mrkaye97 changed the base branch from main to mk/mq-for-status-updates April 15, 2026 00:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant