<@ULVA73B9P> Is it possible to access any informat...
# ask-marvin
o
@Marvin Is it possible to access any information about a deployment run (such as the created name etc) inside of the prefect flow code? eg to output/log to a different service
m
thought for 296.7 seconds
Yes — inside your flow (and tasks) you can access run/deployment metadata to forward to other systems. In Prefect 3.x, you have two primary options: 1) Via prefect.runtime (simple, works anywhere in the run) - Flow run info: -
prefect.runtime.flow_run.id
-
prefect.runtime.flow_run.name
- Deployment info (only when the flow is running from a deployment): -
prefect.runtime.deployment.id
-
prefect.runtime.deployment.name
- Inside tasks, you can also use
prefect.runtime.task_run.*
2) Via get_run_context() (richer model + parameters) -
get_run_context().flow_run
returns the full FlowRun model (id, name, tags, deployment_id, etc.) -
get_run_context().parameters
gives the runtime parameters dict Example: send run details to an external webhook
Copy code
from prefect import flow, get_run_logger, runtime
from prefect.context import get_run_context
import os, requests

@flow
def my_flow(x: int = 1):
    logger = get_run_logger()
    ctx = get_run_context()

    payload = {
        "flow_run_id": runtime.flow_run.id,
        "flow_run_name": runtime.flow_run.name,
        "deployment_name": getattr(runtime.deployment, "name", None),  # None when not running from a deployment
        "parameters": ctx.parameters,
        "tags": list(ctx.flow_run.tags or []),
    }

    <http://logger.info|logger.info>(f"Forwarding run metadata: {payload}")
    if "WEBHOOK_URL" in os.environ:
        <http://requests.post|requests.post>(os.environ["WEBHOOK_URL"], json=payload)

    return x + 1
If you need work pool/queue or other deployment details, you can look them up with the Prefect client via the deployment_id in the context:
Copy code
from prefect import flow
from prefect.context import get_run_context
from prefect.client.orchestration import get_client

@flow
async def my_async_flow():
    ctx = get_run_context()
    if ctx.flow_run.deployment_id:
        async with get_client() as client:
            deployment = await client.read_deployment(ctx.flow_run.deployment_id)
            work_pool = deployment.work_pool_name
            work_queue = deployment.work_queue_name
            # send or log these values as needed
Notes: - When you run a flow directly (not via a deployment),
prefect.runtime.deployment
won’t be set and
ctx.flow_run.deployment_id
will be None. - Use
get_run_context()
when you need parameters, tags, or the full FlowRun model; use
prefect.runtime.*
for quick access to IDs/names. If you can share which specific fields you want to push (e.g., run URL, work pool/queue, parameters, etc.), I can tailor the snippet exactly.