<@ULVA73B9P> when I change a flow run's state in t...
# ask-marvin
a
@Marvin when I change a flow run's state in the local hosted UI, I can select a state and a reason. Can I set a reason when I use
client.set_flow_run_state
?
m
thought for 145.4 seconds
Yes. The “Reason” in the UI maps to the state’s
message
field. When you call
client.set_flow_run_state
, pass a state that includes
message
. Examples: - Async (Prefect 3.x default client)
Copy code
from prefect.client.orchestration import PrefectClient
from prefect.states import Completed, Failed

async with PrefectClient() as client:
    await client.set_flow_run_state(
        flow_run_id,
        state=Completed(message="Manually completed for testing")
    )

    await client.set_flow_run_state(
        flow_run_id,
        state=Failed(message="Upstream dataset missing"),
        force=True,  # if you need to override
    )
- Constructing a State explicitly
Copy code
from prefect.client.schemas.objects import State, StateType
await client.set_flow_run_state(
    flow_run_id,
    State(type=StateType.COMPLETED, message="Replayed with cached results")
)
That
message
will show up in the UI as the “Reason.”