jusstol
04/24/2026, 3:41 PMfrom prefect_kubernetes.experimental.decorators import kubernetes decorator. the problem i face is that prefect seems to ignore the job_variables dictionary i give through the decorator. i want to set an environment variable. it works when i define it a work-pool level, but not from the decorator. show me an exemple of how to do it from the decorator.Marvin
04/24/2026, 3:41 PMMarvin
04/24/2026, 3:43 PM@kubernetes(...) decorator, but the key is that you should pass them as env=..., not as arbitrary nested Kubernetes manifest fields.
Here’s a minimal example:
python
from prefect import flow, get_run_logger
from prefect_kubernetes.experimental.decorators import kubernetes
import os
@flow
def child_flow():
logger = get_run_logger()
<http://logger.info|logger.info>(f"MY_ENV_VAR={os.getenv('MY_ENV_VAR')}")
@flow
def parent_flow():
child_flow()
@kubernetes(
work_pool="my-k8s-work-pool",
env={
"MY_ENV_VAR": "hello-from-decorator",
},
)
@flow
def kubernetes_subflow():
logger = get_run_logger()
<http://logger.info|logger.info>(f"MY_ENV_VAR={os.getenv('MY_ENV_VAR')}")
if __name__ == "__main__":
kubernetes_subflow()
A couple of important notes:
* The decorator signature is @kubernetes(work_pool=..., **job_variables), so env={...} is itself a job variable.
* For Kubernetes workers, Prefect converts that env dict into the container env list in the generated Job spec.
* This should behave similarly to setting env vars at the work-pool level, except it only applies to this decorated flow.
You can also use dotted job variables syntax like this:
python
@kubernetes(
work_pool="my-k8s-work-pool",
**{
"env.MY_ENV_VAR": "hello-from-decorator",
},
)
@flow
def kubernetes_subflow():
import os
print(os.getenv("MY_ENV_VAR"))
If your current version is ignoring this, the most likely causes are:
* you’re passing the variable under the wrong key (for example, trying to modify raw manifest fields instead of using env)
* the flow is not actually being run with that decorated infrastructure path
* you may be hitting a bug/version mismatch in prefect-kubernetes
A good known-working pattern is:
python
@kubernetes(
work_pool="my-k8s-work-pool",
image="my-image:latest",
namespace="default",
env={"MY_ENV_VAR": "hello"},
)
@flow
def my_subflow():
import os
print(os.environ["MY_ENV_VAR"])
I verified the decorator accepts **job_variables, and env is the right job variable for Kubernetes worker configuration.
If you want, I can also show you:
* a full parent flow → subflow on Kubernetes Job example
* how to inspect the rendered job configuration to confirm the env var is making it into the pod spec
* or help debug the exact job_variables dict you’re currently usingalex
04/24/2026, 4:34 PMjusstol
04/28/2026, 2:05 PM