<@ULVA73B9P> can i pull data about prefect assets ...
# ask-marvin
g
@Marvin can i pull data about prefect assets using the prefect client? can you show me an example?
m
thought for 51.7 seconds
Yes, you can retrieve data about Prefect assets using the PrefectClient! In Prefect 3.x, you can use the client to read and manage artifacts, which are Prefect's way of storing and tracking data assets. Here's a practical example:
Copy code
python
from prefect import get_client

async def get_artifact_data():
    # Create an async client
    async with get_client() as client:
        # Retrieve artifacts - you can filter by various criteria
        artifacts = await client.read_artifacts(
            limit=10,  # Optional: limit the number of results
            # You can filter by:
            # - key
            # - flow_run_id
            # - task_run_id
            # - type
        )
        
        # Get a specific artifact by ID
        specific_artifact = await client.read_artifact("your-artifact-id")
        
        # Get the latest version of artifacts with a specific key
        latest_artifacts = await client.read_artifact_collections(
            key="your-artifact-key"
        )

# Run the async function
import asyncio
asyncio.run(get_artifact_data())
You can also create new artifacts using the client:
Copy code
python
async def create_artifact():
    async with get_client() as client:
        # Create a new artifact
        new_artifact = await client.create_artifact(
            key="my-data",
            data={"some": "data"},  # Your data here
            type="json"  # Specify the type of data
        )
The client supports several operations for artifacts: - `create_artifact()`: Create a new artifact - `read_artifacts()`: Query multiple artifacts with filters - `read_artifact()`: Get a specific artifact by ID - `read_artifact_collections()`: Get the latest versions of artifacts - `update_artifact()`: Update an existing artifact's metadata Each artifact can store various types of data, including: - JSON data - Markdown content - URLs - File paths - Custom data types The artifacts system is particularly useful for: - Storing and tracking model metrics - Saving data quality reports - Maintaining links to generated visualizations - Tracking any other data assets produced by your flows Remember that artifacts are immutable once created - each new version creates a new artifact, allowing you to maintain a history of your data assets. Would you like me to provide more specific examples for any particular use case or artifact operation?
n
hmm ill have to enlighten marvin here
Copy code
uv run <https://gist.githubusercontent.com/zzstoatzz/c8a4ad709876c44cefd40e65ac983485/raw/5dcdd854799b5f42558514b9a6870fda221aa735/asset_manager.py> list
there will be more official things like this in the future
g
ok awesome! thank you @Nate, this is perfect and just to confirm- if I have multiple flows that write data to the same asset, a postgres table for example, will the lineage only show a connection to that table from the last flow that was run? So the upstream assets from other flows won't be connected to the shared downstream asset.
n
yes that's how it works today