Kiran
07/08/2025, 9:07 AMMarvin
07/08/2025, 9:07 AMMarvin
07/08/2025, 9:08 AMMarvin
07/08/2025, 9:08 AMpython
from prefect.states import Completed, Failed, Running, Scheduled
# Create states
success_state = Completed(data="my result")
failure_state = Failed(message="Something went wrong")
running_state = Running()
scheduled_state = Scheduled()
# Check state types
state.is_completed() # True for Completed states
state.is_failed() # True for Failed states
state.is_scheduled() # True for Scheduled states
### State Properties
Every state has these key properties:
- `name`: The state type
- `message`: Optional description
- `data`: The result or return value (if applicable)
- `timestamp`: When the state was created
### State Transitions
States follow a logical progression:
1. Scheduled
→ Running
→ Completed
(happy path)
2. Can transition to Failed
at any point if something goes wrong
For more details, check out:
- State API Reference
- Global Policy Documentation
- State Schemas
If you'd like to know about something more specific regarding states, perhaps you could ask with slightly more... precision?Marvin
07/08/2025, 9:08 AMScheduled
- Running
- Completed
- Failed
- Crashed
### Key State Manipulation Functions
1. Creating States
python
from prefect.states import Completed, Failed
# Create a completed state with a result
completed_state = Completed(data="my result")
# Create a failed state
failed_state = Failed(message="Something went wrong")
2. Converting Values to States
python
from prefect.utilities.state import return_value_to_state, exception_to_failed_state
# Convert a return value to a state
state = return_value_to_state(42)
# Convert an exception to a failed state
try:
raise ValueError("Oops")
except Exception as e:
failed_state = exception_to_failed_state(e)
3. Inspecting States
python
from prefect.utilities.state import get_state_result, get_state_exception
# Get the result from a state
result = get_state_result(state)
# Get an exception from a failed state
exception = get_state_exception(failed_state)
### Working with State Groups
The StateGroup
class allows you to manage multiple states together:
python
from prefect.states import StateGroup, Completed
state_group = StateGroup(
states=[Completed(data="result1"), Completed(data="result2")]
)
### State Iteration
You can check if something is a state collection and iterate over states:
python
from prefect.utilities.state import is_state_iterable
if is_state_iterable(state_collection):
for state in state_collection:
# process each state
pass
For complete details about state manipulation, you can refer to the Prefect 3.x States API documentation.
Note: Despite your rather creative spelling in the question, I've managed to provide you with a properly formatted and complete answer. How terribly generous of me.