Hi, I can't make transaction idempotency work. It ...
# ask-community
t
Hi, I can't make transaction idempotency work. It seems like
txn.is_committed()
always returns false even though I can see a record written with the expected key in the storage directory. I tried the example from the docs:
Copy code
from prefect import task, flow
from prefect.transactions import transaction


@task
def download_data():
    """Imagine this downloads some data from an API"""
    return "some data"


@task
def write_data(data: str):
    """This writes the data to a file"""
    with open("data.txt", "w") as f:
        f.write(data)


@flow(log_prints=True)
def pipeline():
    with transaction(key="download-and-write-data") as txn:
        if txn.is_committed():
            print("Data file has already been written. Exiting early.")
            return
        data = download_data()
        write_data(data)


if __name__ == "__main__":
    pipeline()
I don't get the expected print statement and the download/write tasks are executed.
n
hi @Theom - I think there was a bug at fault here, which I hopefully fixed in this PR (on macOS) you can try the fix by copying your example to your clipboard and running
Copy code
pbpaste | uv run --with git+<https://github.com/PrefectHQ/prefect.git@refs/pull/17541/head> -
this has been merged! should be released later today https://github.com/PrefectHQ/prefect/pull/17541
t
Thanks for the lightning fast reaction! I'm not on macOS though, I'll wait for the release where this PR will be included.
n
the only macos part was the pbpaste, you should be able to do
pip install git+<https://github.com/PrefectHQ/prefect.git@refs/pull/17541/head>
and then your script but sounds good! thanks for the follow up
t
Thank you, will try this or the next dev release. In fact I had another transaction-related question here. Is there a way to assign a result to a transaction and then retrieve it in subsequent runs in case it's already committed?
n
would you be willing to make a discussion about this? just more discoverable that way and easier to link to PRs
t
Sure!
n
thank you!