<@ULVA73B9P> How to return data from flow, if I wi...
# ask-marvin
r
@Marvin How to return data from flow, if I will run it through run_deployment. Show me example
m
🔄 Thinking... this may take a while
Yes — in Prefect 3.x,
run_deployment()
returns a flow run object, and you get the flow’s returned data from its final state. Important: if you want to read the returned value reliably, enable result persistence on the flow. Example:
Copy code
python
from prefect import flow
from prefect.deployments import run_deployment

@flow(persist_result=True)
def my_flow(x: int):
    return {"input": x, "output": x * 2}

if __name__ == "__main__":
    flow_run = run_deployment(
        name="my-flow/my-deployment",
        parameters={"x": 21},
    )

    # run_deployment returns a FlowRun, not the result directly
    result = flow_run.state.result()
    print(result)  # {"input": 21, "output": 42}
If you want to be a bit safer and inspect the state first:
Copy code
python
from prefect import flow
from prefect.deployments import run_deployment

@flow(persist_result=True)
def my_flow(x: int):
    return x * 2

flow_run = run_deployment(
    name="my-flow/my-deployment",
    parameters={"x": 10},
)

if flow_run.state.is_completed():
    result = flow_run.state.result()
    print(f"Result: {result}")
elif flow_run.state.is_failed():
    error = flow_run.state.result(raise_on_failure=False)
    print(f"Flow failed: {error}")
else:
    print(f"Flow is in state: {flow_run.state.type}")
Notes: -
run_deployment(...)
→ returns
FlowRun
- actual flow return value →
flow_run.state.result()
- use
@flow(persist_result=True)
so the result is stored and can be retrieved If you want, I can also show: - an async example - how to do this when the deployment is triggered and checked later - how to configure remote result storage for Docker/Kubernetes workers
r
@Marvin I got : ValueError: Path C:\home\adam\.prefect\storage\02e836ca02d1447d81919e5012356c8b does not exist. when I call flow_run.state.result(). Prefect worker is running from wsl on my Windows machine,. Prefect client is running directly in windows. Does it meen that flow_run.state.result() will always try read result from local storage ? What if worke will be on another host ?
m
✅ thought for 82.0 seconds
Yes — that’s exactly what’s happening.
flow_run.state.result()
does not always read from local storage in principle, but in your current setup it is trying to read from a local filesystem result store, and that local path only exists in the worker environment. Because your worker runs in WSL and your client runs in Windows: - worker writes result to something like
/home/adam/.prefect/storage/...
inside WSL - Windows client tries to resolve it as
C:\home\adam\.prefect\storage\...
- that path does not exist on Windows - so
flow_run.state.result()
fails So yes: local result storage is not safe across different environments/hosts. What this means generally - If worker and caller are in the same environment, local storage can work - If worker is on another host, container, VM, Kubernetes pod, WSL, etc., local storage usually will not work - For cross-host result retrieval, use shared/remote result storage Recommended fix Use a result storage backend both the worker and the client can access, for example: - S3 - GCS - Azure Blob - SMB / network share Then
flow_run.state.result()
can fetch the result from that shared backend. Example with explicit result storage on the flow:
Copy code
python
from prefect import flow

@flow(
    persist_result=True,
    result_storage="s3-bucket/my-shared-results"
)
def my_flow(x: int):
    return {"value": x * 2}
Then from your Windows client:
Copy code
python
from prefect.deployments import run_deployment

flow_run = run_deployment(
    name="my-flow/my-deployment",
    parameters={"x": 21},
)

result = flow_run.state.result()
print(result)
That works as long as both environments can access the same S3 bucket/block configuration. Why local storage fails on another host Local filesystem storage stores results on the machine that executed the flow. If the reader is on a different machine, that path is meaningless unless it points to a shared mount visible at the same location. So this is not just a Windows/WSL issue — the same problem happens with: - local laptop client + remote VM worker - local client + Docker worker - local client + Kubernetes worker Good rule of thumb - local filesystem result storage → only for single-machine setups - remote/shared storage → for any real deployment setup If you want, I can show you a full working example for: - S3 result storage - shared network folder / SMB - or a minimal local workaround for Windows + WSL on the same machine