Joseph Wibowo
11/24/2025, 12:19 AMMarvin
11/24/2025, 12:19 AMMarvin
11/24/2025, 12:22 AMimport os
from prefect.context import get_run_context, MissingContextError
from prefect.artifacts import create_link_artifact
def create_link_artifact_if_in_context(*, link: str, key: str):
# Optional test toggle
if os.getenv("DISABLE_PREFECT_ARTIFACTS") == "1":
return None
try:
get_run_context()
except MissingContextError:
return None
return create_link_artifact(link=link, key=key)
In tests, set DISABLE_PREFECT_ARTIFACTS=1 or just rely on the context check.
2) Monkeypatch artifact functions to no-ops in pytest
If you want to prevent any artifact code from running in tests without touching production code:
# conftest.py
import pytest
import prefect.artifacts as pa
def _noop(*args, **kwargs):
return None
async def _anoop(*args, **kwargs):
return None
@pytest.fixture(autouse=True)
def disable_prefect_artifacts(monkeypatch):
# Patch all artifact helpers to no-ops
names = [
"create_link_artifact", "acreate_link_artifact",
"create_markdown_artifact", "acreate_markdown_artifact",
"create_table_artifact", "acreate_table_artifact",
"create_progress_artifact", "acreate_progress_artifact",
"update_progress_artifact", "aupdate_progress_artifact",
"create_image_artifact", "acreate_image_artifact",
]
for name in names:
if hasattr(pa, name):
monkeypatch.setattr(pa, name, _anoop if name.startswith("a") else _noop)
This completely prevents artifact creation during tests.
If you only need to silence the warning
3) Filter the specific warning in pytest config
# pytest.ini or pyproject.toml [tool.pytest.ini_options]
filterwarnings =
ignore:Artifact creation outside of a flow or task run is deprecated:FutureWarning
4) Wrap the call in tests with pytest.warns
When your test intentionally triggers out-of-context creation:
import pytest
from prefect.artifacts import create_link_artifact
def test_artifact_outside_context():
with pytest.warns(FutureWarning):
create_link_artifact(link="<https://example.com>", key="k")
Notes
- There is no Prefect setting/env var that globally disables artifact creation. You’ll need to guard, patch, or filter as above.
- Best practice in Prefect 3.x is to only create artifacts inside flows/tasks.
Refs:
- Artifacts concept: Artifacts (Prefect docs)
- Run context API: get_run_context