Gautam Menon
11/19/2025, 3:01 PM@task(
cache_key_fn=lambda _, kw: f"{kw['url']}",
cache_expiration=timedelta(days=30),
)
this leads to the following error :
18:58:44.513 | WARNING | prefect.tasks - Both `cache_policy` and `cache_key_fn` are set on task <prefect.tasks.Task object at 0x169d0ff20>. `cache_key_fn` will be used.
suggest an appropriate fix to avoid such warning logs
I am using prefect version 3.6.1Marvin
11/19/2025, 3:01 PMMarvin
11/19/2025, 3:12 PMcache_policy and cache_key_fn are set on the task. In your case you didn’t pass cache_policy explicitly, so it’s almost certainly being injected by your global settings (profile or environment), e.g. PREFECT_TASKS_DEFAULT_NO_CACHE=true or PREFECT_TASKS_DISABLE_CACHING=true. When Prefect sees a non-NotSet cache_policy and your cache_key_fn, it logs the warning and uses the cache_key_fn.
Two clean ways to remove the warning:
Option A (recommended): Use a cache policy only (no cache_key_fn)
- This is the current preferred API and avoids the conflict entirely.
from datetime import timedelta
from prefect import task
from prefect.cache_policies import CachePolicy
@task(
cache_policy=CachePolicy.from_cache_key_fn(lambda _, kw: kw["url"]),
cache_expiration=timedelta(days=30),
)
def my_task(...):
...
If your url is part of the task inputs, you could also use input-based caching:
from datetime import timedelta
from prefect import task
from prefect.cache_policies import INPUTS
@task(cache_policy=INPUTS, cache_expiration=timedelta(days=30))
def my_task(url: str, ...):
...
Option B: Keep cache_key_fn, but stop injecting a default cache_policy
- Check if a global setting is setting a cache policy:
prefect config view --hide-defaults --show-sources
- If you see either of these set, turn them off (or remove the env vars):
prefect config set PREFECT_TASKS_DEFAULT_NO_CACHE=false
prefect config set PREFECT_TASKS_DISABLE_CACHING=false
With no global policy being injected, your original cache_key_fn + cache_expiration will not warn.
Note: The warning is benign—Prefect will prefer `cache_key_fn`—but using one of the options above will silence it.Gautam Menon
11/19/2025, 3:31 PMMarvin
11/19/2025, 3:31 PMMarvin
11/19/2025, 3:31 PM