<@ULVA73B9P> if a function from a deployment (runn...
# ask-marvin
s
@Marvin if a function from a deployment (running as a local process worker (?)) returns a string, and I trigger a deployment flow run that calls that function, how can I get that string return result back?
m
âś… thought for 177.4 seconds
n
short answer: must have
persist_result=True
on the flow you trigger.
run_deployment(...).state.result()
from the place you trigger to retrieve the actual value
👍 1
m
Great question. Short answer: you can get that returned string back as long as the flow’s result is persisted. The worker type (local process, etc.) doesn’t change the pattern. In Prefect 3.x: - Results are not persisted by default across processes - To retrieve a return value from a deployment-triggered run, enable result persistence on the flow (or globally), then read the final state’s result Here are the two common patterns. 1) Trigger via Python and wait for the result - Ensure the flow persists its result:
Copy code
from prefect import flow

@flow(name="echo", persist_result=True)
def echo(msg: str) -> str:
    return msg
- Trigger the deployment and fetch the result:
Copy code
from prefect.deployments import run_deployment

flow_run = run_deployment(
    name="echo/prod",            # "<flow name>/<deployment name>"
    parameters={"msg": "hi"},
    timeout=None                 # wait until the run finishes
)

value = flow_run.state.result()  # -> "hi"
print(value)
2) If you only have a flow_run_id later
Copy code
from prefect.client.orchestration import get_client

client = get_client(sync_client=True)
flow_run = client.read_flow_run(flow_run_id)
value = flow_run.state.result()   # -> your string
Notes: - If you don’t enable
persist_result=True
(or set
PREFECT_RESULTS_PERSIST_BY_DEFAULT=true
), calling
.result()
on a state from another process will raise a “missing result” style error because the value was never written to result storage. - If you’d prefer something UI-visible or JSON-only, consider emitting an artifact (e.g., a string or markdown artifact) from your flow and reading it via the API. If you can share how you’re triggering the deployment (Python
run_deployment
, CLI/UI, or an automation) and whether you can modify the flow code, I can tailor the exact snippet for your setup.
n
good job marvin
marvin 1
s
Thanks! @Marvin, following up - if I have two devices on separate networks and no other built-in ways of sharing filesystems, then I must use an S3 bucket or similar to enable result persistence - is that correct?
m
âś… thought for 119.1 seconds
Short answer: yes—if the two machines can’t see the same filesystem, you need a shared, network-accessible result store for
state.result()
to work across devices. S3 (or similar, like GCS/Azure Blob) is the most common choice. Details and options: - Object storage (recommended): S3, GCS, Azure Blob via their Prefect blocks. Both machines just need outbound access to that service. Configure your flow with
persist_result=True
and a
result_storage
block. - Other remote filesystems: Prefect’s
RemoteFileSystem
and
SMB
blocks can work if both devices can reach the endpoint (e.g., SFTP/WebDAV/SMB share), but that still requires a reachable network path. - Local filesystems only work if the reader and writer are the same machine (or truly shared FS); otherwise,
state.result()
won’t be able to fetch the value. Lightweight alternative for strings: - If you only need to pass back a small string or JSON, you can emit an artifact (e.g., markdown or table). Artifacts are stored in the Prefect API (Cloud/Server), so no external storage is needed as long as both devices can reach the Prefect API. This avoids S3 entirely for small textual results. Example with S3:
Copy code
from prefect import flow
from prefect_aws.s3 import S3Bucket

# one-time setup
s3 = S3Bucket(bucket_name="my-results-bucket", basepath="prefect-results/")
s3.save("results-s3", overwrite=True)

@flow(persist_result=True, result_storage="s3-bucket/results-s3")
def echo(msg: str) -> str:
    return msg
Then when you trigger the deployment from another machine and call
flow_run.state.result()
, Prefect will use the saved block to fetch from S3. You only need a Prefect API key to read the block config; you don’t need to hardcode AWS creds in the caller. Artifact alternative (for small strings):
Copy code
from prefect import flow
from prefect.artifacts import create_markdown_artifact

@flow
def echo(msg: str):
    create_markdown_artifact(
        key="echo-output",
        markdown=f"Result: {msg}",
        description="Echo result"
    )
You can later read this artifact via the Prefect API without configuring external storage. If you want, tell me which storage you prefer (S3, GCS, Azure, SFTP/SMB), and I’ll share a drop-in config snippet for your setup.