<@ULVA73B9P> In Prefect 3, I have an automation th...
# ask-marvin
n
@Marvin In Prefect 3, I have an automation that triggers a webhook block. I know that in the webhook template body I can put various metadata of the flow run. Can I put a specific job variable present on the run?
m
Error: unhandled errors in a TaskGroup (1 sub-exception)
Sorry, I encountered an error while processing your request. Please try again.
n
@Marvin In Prefect 3, I have an automation that triggers a webhook block. I know that in the webhook template body I can put various metadata of the flow run. Can I put a specific job variable present on the run?
m
thought for 148.4 seconds
Short answer: Yes—with a caveat. - In Prefect 3 automations, webhook templates can access the flow run object, including any job variables set on that run. You can reference them with Jinja like:
Copy code
{
  "my_var": "{{ flow_run.job_variables.my_var }}"
}
or safely:
Copy code
{
  "my_var": "{{ flow_run.job_variables.get('my_var') }}"
}
- Important: this gives you only the job variables that were attached to the flow run itself. It does not automatically include defaults from the deployment or work pool, nor the final “resolved” variables the worker computed at run time. If the value might come from other layers, you can use fallbacks in the template:
Copy code
{
  "image": "{{ flow_run.job_variables.get('image') 
              or deployment.job_variables.get('image') 
              or work_pool.base_job_template.variables.get('image') }}"
}
If you truly need the fully resolved job variables used by the worker, they aren’t exposed to automations. Common workarounds: - Emit a custom event from within your flow (after the worker has resolved variables) that includes the values you need, then trigger the webhook off that event. - Have your webhook receiver query Prefect for the flow run plus deployment/work pool and reconstruct the value with the same precedence (run → deployment → work pool). - Push the needed value into the flow run’s job variables when creating the run so it’s directly available as
flow_run.job_variables.<key>
. If you share the exact key you want (and your worker type), I can help you craft the precise template snippet.
n
@Marvin can you provide a complete example of an automation that triggers a webhook, in whose body is present a job variable from the flow run?
m
thought for 243.7 seconds
Absolutely—here’s a complete, end-to-end example you can copy/paste. It creates an automation that triggers a webhook and includes a job variable from the flow run in the webhook body. Step 0 — Create a Webhook block and get its ID - In Python:
Copy code
from prefect.blocks.webhook import Webhook

webhook = Webhook(
    url="<https://webhook.site/your-unique-url>",
    method="POST",
    headers={"Content-Type": "application/json"}
)
webhook.save("notify-completions", overwrite=True)
- Get the block’s UUID (block_document_id):
Copy code
prefect block inspect webhook/notify-completions
Copy the “ID” value from the output. Step 1 — Define the automation (YAML) Replace BLOCK_DOCUMENT_ID_HERE with the UUID from Step 0. Save as webhook-auto.yaml:
Copy code
name: "Flow Run Completion Webhook"
description: "Send a webhook on flow-run completion including a job variable from the run"
enabled: true

trigger:
  type: event
  posture: Reactive
  expect:
    - prefect.flow-run.Completed
  threshold: 1
  within: 0

actions:
  - type: call-webhook
    block_document_id: "BLOCK_DOCUMENT_ID_HERE"
    payload: |
      {
        "automation": "{{ automation.name }}",
        "event_id": "{{ event.id }}",
        "flow_run_id": "{{ flow_run.id }}",
        "flow_run_name": "{{ flow_run.name }}",
        "state": "{{ flow_run.state.name }}",
        "my_var_from_job_variables": "{{ flow_run.job_variables.get('my_var') }}",
        "all_job_variables": {{ flow_run.job_variables | tojson if flow_run.job_variables else 'null' }},
        "ui_url": "{{ flow_run | ui_url }}"
      }
Notes: -
{{ flow_run.job_variables.get('my_var') }}
is the job variable reference. -
tojson
safely serializes the entire
job_variables
dict if you want to include everything. Step 2 — Create the automation
Copy code
prefect automations create --from-file webhook-auto.yaml
Step 3 — Trigger a flow run with a job variable You can attach a job variable when creating a run. For strings, pass valid JSON (so quote the value). Example:
Copy code
prefect deployment run <FLOW_NAME>/<DEPLOYMENT_NAME> \
  -jv my_var='"hello from CI"' \
  --watch
When the run completes, your automation will fire and the webhook body will include: - `my_var_from_job_variables`: hello from CI - `all_job_variables`: {"my_var": "hello from CI"} (plus anything else present) Tips: - If you prefer a different key, just change
my_var
in both the
-jv
flag and the template. - The job variables available here are those set on the flow run itself (not the final “resolved” variables after worker merging). If you need resolved values, let me know your worker type and I can suggest a pattern to emit them from within the flow.