Skip to content

Conversation

@marcel-rbro
Copy link
Contributor

@marcel-rbro marcel-rbro commented Jan 13, 2026

Note

Refines guidance on Actor state persistence.

  • Recommends using Actor.useState for automatic state save/restore during migrations
  • Clarifies manual persistence via Actor.on() migrating event and retrieval with Actor.getValue/Actor.get_value, noting Python usage
  • Editorial updates in state_persistence.md; no code changes

Written by Cursor Bugbot for commit c9753ae. Configure here.

@marcel-rbro marcel-rbro requested a review from TC-MO as a code owner January 13, 2026 11:57
@github-actions github-actions bot added the t-docs Issues owned by technical writing team. label Jan 13, 2026
@apify-service-account
Copy link

Preview for this PR was built for commit c9753ae and is ready at https://pr-2176.preview.docs.apify.com!

The simplest way to handle state persistence is with the [`Actor.useState`](/sdk/js/reference/class/Actor#useState) method, which automatically saves and retrieves your state during migrations.

This is done using the `Actor.on()` method and the `migrating` event.
For more control or when using Python, you can manually handle state persistence using the `Actor.on()` method and the `migrating` event.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What control? More than what? And why we move straight to Python from the control bit what is the connection?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to the PR: Hmm, the Python thing is kinda bummer. Seems we have it in Crawlee but not SDK. We should add it there @Pijukatel

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the issue apify/apify-sdk-python#735

But we have to think this through, given the recent (and maybe upcoming) changes to the use state
apify/crawlee#3309
apify/crawlee-python#1669

Should Actor.use_state point to a different state than Crawler.use_state unless explicitly set up to share the same state? If we by default separate use_state of two different crawlers, then it will be different from actor as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is determined by the KV record key which is one of the parameters of useState.

If users use this and Crawler.useState, I think those should be 2 separate instances with 2 different default keys. But there isn't much use-case for this and if users combine a lot of these, they are asking for trouble.

The simplest way to handle state persistence is with the [`Actor.useState`](/sdk/js/reference/class/Actor#useState) method, which automatically saves and retrieves your state during migrations.

This is done using the `Actor.on()` method and the `migrating` event.
For more control or when using Python, you can manually handle state persistence using the `Actor.on()` method and the `migrating` event.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to the PR: Hmm, the Python thing is kinda bummer. Seems we have it in Crawlee but not SDK. We should add it there @Pijukatel

For more control or when using Python, you can manually handle state persistence using the `Actor.on()` method and the `migrating` event.

- The `migrating` event is triggered just before a migration occurs, allowing you to save your state.
- To retrieve previously saved state, you can use the [`Actor.getValue`](/sdk/js/reference/class/Actor#getValue)/[`Actor.get_value`](/sdk/python/reference/class/Actor#get_value) methods.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is kinda bad to recommend many different ways to do this. How about:

  1. Let's wait for Python team to add use_state to SDK if it is reasonably easy
  2. Recommend it as a primary method of handling state, we don't even need to mention getValue then.
  3. Show useState in the first code example
  4. Keep the "manual" usage for the 2nd example with the reboot (I want this to be automatically in the SDK too but that won't happen soon)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let's wait for the Python team. I've subscribed to the issue, will finalize this once the Python issue is resolved.

@apify-service-account
Copy link

Preview for this PR was built for commit 8416e81d and is ready at https://pr-2176.preview.docs.apify.com!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

t-docs Issues owned by technical writing team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants