André Lynum
06/10/2026, 12:05 PMMarvin
06/10/2026, 12:05 PMMarvin
06/10/2026, 12:07 PMprefect.runtime.
python
from prefect import flow
from prefect.runtime import deployment, flow_run
@flow
def my_flow():
print("deployment id:", deployment.id)
print("deployment name:", deployment.name)
print("deployment version:", deployment.version)
print("deployment parameters:", deployment.parameters)
print("flow run id:", flow_run.id)
print("flow run name:", flow_run.name)
print("flow run tags:", flow_run.tags)
print("flow parameters:", flow_run.parameters)
What you can access easily
- deployment.id
- deployment.name
- deployment.version
- deployment.parameters
- deployment.flow_run_id
And from `flow_run`:
- flow_run.id
- flow_run.name
- flow_run.flow_name
- flow_run.flow_version
- flow_run.tags
- flow_run.parameters
- flow_run.job_variables
If you need lower-level metadata like work pool / work queue, use the flow run context:
python
from prefect import flow
from prefect.context import FlowRunContext
@flow
def my_flow():
ctx = FlowRunContext.get()
if not ctx:
return
fr = ctx.flow_run
print("deployment_id:", fr.deployment_id)
print("work_pool_name:", fr.work_pool_name)
print("work_queue_name:", fr.work_queue_name)
print("job_variables:", fr.job_variables)
A few caveats
- If the flow is run ad hoc (for example python my_flow.py) instead of from a deployment, deployment fields are usually None or {}.
- deployment.tags are not exposed directly via prefect.runtime.deployment.
If you need deployment fields not exposed at runtime, query the API:
python
from prefect import flow
from prefect.context import FlowRunContext
from prefect.client.orchestration import get_client
@flow
async def my_flow():
ctx = FlowRunContext.get()
if not ctx or not ctx.flow_run.deployment_id:
print("Not running from a deployment")
return
async with get_client() as client:
dep = await client.read_deployment(ctx.flow_run.deployment_id)
print("deployment tags:", dep.tags)
print("description:", dep.description)
Rule of thumb
- Use prefect.runtime for common metadata
- Use FlowRunContext.get() for work pool / queue details
- Use the client if you need full deployment object data
If you want, I can give you a copy-paste helper function like get_current_deployment_info() that returns all of this in one dict.André Lynum
06/10/2026, 12:10 PMMarvin
06/10/2026, 12:10 PMMarvin
06/10/2026, 12:10 PMdeployment.branch field inside a flow run. If you're deploying from Git with flow.from_source(...), the branch/ref used for the pull is part of the source configuration, but it is not a standard runtime field you can always read from prefect.runtime.deployment.
So in practice, the reliable options are:
Best options
1. Pass the branch as a deployment parameter
2. Set it in a variable/env var/job variable
3. Query the deployment definition and inspect its source/pull configuration if you need to infer it
Recommended pattern: parameter
python
from prefect import flow, get_run_logger
@flow
def my_flow(git_branch: str = "main"):
logger = get_run_logger()
<http://logger.info|logger.info>(f"Deployment branch: {git_branch}")
Then set git_branch in the deployment so every run logs it.
Alternative: job variable / env var
If your worker job config sets something like GIT_BRANCH, you can log it:
python
import os
from prefect import flow, get_run_logger
@flow
def my_flow():
logger = get_run_logger()
<http://logger.info|logger.info>(f"Deployment branch: {os.getenv('GIT_BRANCH', 'unknown')}")
Can you inspect the deployment from inside the flow?
Yes, sometimes. You can query the deployment object from the API, but whether the branch is easy to extract depends on how the deployment source was configured.
python
from prefect import flow, get_run_logger
from prefect.context import FlowRunContext
from prefect.client.orchestration import get_client
@flow
async def my_flow():
logger = get_run_logger()
ctx = FlowRunContext.get()
if not ctx or not ctx.flow_run.deployment_id:
<http://logger.info|logger.info>("No deployment id found; probably an ad-hoc run")
return
async with get_client() as client:
deployment = await client.read_deployment(ctx.flow_run.deployment_id)
<http://logger.info|logger.info>(f"Deployment name: {deployment.name}")
<http://logger.info|logger.info>(f"Deployment: {deployment}")
But I wouldn't recommend depending on parsing that object for branch info unless you've confirmed your deployment shape and tested it.
Bottom line
- Yes, you can log the branch during a flow run
- No, there is not a guaranteed built-in runtime field like deployment.branch
- The most reliable solution is to explicitly pass/store the branch in the deployment configuration
If you want, I can show you the cleanest way to do this for:
- flow.from_source(...).deploy(...)
- prefect deploy
- GitHub Actions-based deploymentsAndré Lynum
06/10/2026, 12:12 PMMarvin
06/10/2026, 12:12 PMMarvin
06/10/2026, 12:12 PMA when it points at branch feature-x
- later update deployment A to point at main
…the run UI will usually show the current deployment metadata, not a guaranteed immutable historical branch field for that old run.
What is preserved for the run
You can reliably keep branch history only if you store it as run-time data, for example:
- a flow parameter like git_branch
- a tag
- a logged message/artifact
- a custom variable in job config copied into the run
Then the run’s logs / parameters / artifacts remain associated with that specific run even if the deployment changes later.
Best practice
If branch provenance matters, explicitly record it at run time.
For example:
python
from prefect import flow, get_run_logger
@flow
def my_flow(git_branch: str = "unknown"):
logger = get_run_logger()
<http://logger.info|logger.info>(f"Git branch for this run: {git_branch}")
Then every run keeps its own branch value in:
- flow run parameters
- logs
You could also create an artifact if you want it more visible in the UI.
Short answer
- Current deployment branch after edits? Yes, you can inspect the deployment as it exists now.
- Historical branch actually used by an older run after the deployment was changed? Not reliably in the UI unless you recorded it on the run itself.
If you want, I can show you the best way to persist branch info per run so it’s visible in Prefect Cloud even after deployments are updated.André Lynum
06/10/2026, 12:32 PMMarvin
06/10/2026, 12:32 PMMarvin
06/10/2026, 12:35 PMyaml
branch: "{{ prefect.variables.deployment_branch }}"
then Prefect Cloud will typically show the template string itself in the deployment config/UI, not the resolved branch value.
Why?
Because for pull/source config like branch, Prefect keeps the unresolved template in the deployment definition and resolves it at runtime when the worker executes the pull step.
So the behavior is:
- Deployment UI/config: shows {{ prefect.variables.deployment_branch }}
- Actual run: worker resolves prefect.variables.deployment_branch at run time and uses that value for the clone/pull
That design is what allows you to change the variable later without redeploying.
Important implication
Because the branch is resolved at run time:
- the deployment UI may not tell you which concrete branch a past run used
- if the variable changes later, older runs won't automatically show the old resolved branch unless you log or persist it yourself
Best practice if you care about auditability
Log the resolved branch during the run, or persist it as an artifact/parameter. Otherwise the UI will just keep showing the template.
So short answer: yes, this is right.
If you want, I can show you a pattern to record the resolved branch on every flow run so it's visible later in Cloud.