<@ULVA73B9P> Is it possible to update same artifac...
# ask-marvin
r
@Marvin Is it possible to update same artifact several times during flow ?
m
thought for 107.5 seconds
Short answer: it depends on the artifact type. - Progress artifacts: Yes, you can update the same artifact in place multiple times during a flow.
Copy code
from prefect import flow
  from prefect.artifacts import create_progress_artifact, update_progress_artifact

  @flow
  def run():
      prog_id = create_progress_artifact(progress=0.0, key="my-progress", description="Processing")
      for i in range(1, 6):
          # ... do work ...
          update_progress_artifact(artifact_id=prog_id, progress=i * 20.0)
- Markdown, link, table, image artifacts: There’s no in-place update. Create a new artifact with the same key; Prefect will version them and mark the newest as “latest” in the UI.
Copy code
from prefect import flow
  from prefect.artifacts import create_markdown_artifact

  @flow
  def run():
      create_markdown_artifact(key="daily-report", markdown="# Report v1")
      # later in the same flow
      create_markdown_artifact(key="daily-report", markdown="# Report v2")  # new version; same key
Notes: - Using the same
key
groups versions and ensures the UI shows the latest; history is preserved. - Async variants exist (
acreate_*
,
aupdate_progress_artifact
) if you’re writing async flows.
r
@Marvin I need updates for markdown artifact, but I do not need history, only last version. How can I do it ?
m
thought for 196.6 seconds
Yes. For a “latest-only” markdown artifact, create it once, then update it in place by PATCHing the same artifact ID. This replaces the content without creating new versions. Key points: - Markdown artifact data is just the raw markdown string. - Use PrefectClient.update_artifact with ArtifactUpdate(data=...) to patch it. - If you don’t have the ID, look up the latest by key first. Examples 1) Create once, then update in place (sync)
Copy code
from prefect.artifacts import create_markdown_artifact, Artifact
from prefect.client.orchestration import get_client
from prefect.client.schemas.actions import ArtifactUpdate

# Create once (returns the artifact UUID)
artifact_id = create_markdown_artifact(key="my-report", markdown="# Initial", description="Latest report")

# Update in place (no history/version created)
client = get_client(sync_client=True)
client.update_artifact(
    artifact_id=artifact_id,
    artifact=ArtifactUpdate(data="# Updated content", description="Latest report")
)
2) Update by key (if you don’t have the ID yet) — async flow example
Copy code
from prefect import flow
from prefect.artifacts import Artifact, MarkdownArtifact
from prefect.client.orchestration import get_client
from prefect.client.schemas.actions import ArtifactUpdate

@flow
async def upsert_markdown(md: str):
    async with get_client() as client:
        latest = await Artifact.aget(key="my-report")
        if latest:
            await client.update_artifact(
                artifact_id=latest.id,
                artifact=ArtifactUpdate(data=md, description="Latest report"),
            )
        else:
            await MarkdownArtifact(key="my-report", markdown=md, description="Latest report").acreate()
If you previously created multiple versions with the same key and want to keep only one, you can delete the older ones via client.delete_artifact(old_id).