<@ULVA73B9P>, I'm running a Prefect server and a w...
# ask-marvin
m
@Marvin, I'm running a Prefect server and a worker in a docker container. I'm able to create a deployment and run it from a python script. the script looks like this:
from prefect.deployments import run_deployment
from prefect.client.schemas.objects import State
from prefect.client.orchestration import get_client
import dotenv
dotenv.load_dotenv() # loads prefect api url from .env
if __name__ == "__main__":
state: State = run_deployment(_name_="hello_flow/hello_deploy", _parameters_={"name": "Masa"})
print("result", type(state), type(state.state))
print ("state", state)
How do I get what is returned by the flow?
m
thought for 126.9 seconds
Great question. In Prefect 3.x,
run_deployment(...)
returns a
FlowRun
object (not a
State
). To get the Python value your flow returned, resolve the final state’s result. Example:
Copy code
from prefect.deployments import run_deployment
from prefect.states import get_state_result

if __name__ == "__main__":
    flow_run = run_deployment(
        name="hello_flow/hello_deploy",
        parameters={"name": "Masa"},
    )
    print("final state:", flow_run.state.type, flow_run.state.name)

    result = get_state_result(flow_run.state)  # raises if the run failed
    print("flow result:", result)
Notes: - If you want to avoid raising on failures, use
get_state_result(flow_run.state, raise_on_failure=False)
and check
flow_run.state.type
. - Make sure your flow’s return value is serializable by your configured result serializer (JSON by default). If not, configure a different serializer. Docs: - run_deployment: API - get_state_result: API - Results overview: Concepts