Hey <@ULVA73B9P> can you show me how to use `clien...
# ask-marvin
s
Hey @Marvin can you show me how to use
client.get_artifacts()
to query an artifact with a particular key?
m
It appears that querying the Prefect documentation is currently encountering issues. However, I can provide you with a brief example of how to use the
client.get_artifacts()
method to query an artifact with a particular key:
Copy code
python
from prefect.client import OrionClient

async def get_artifact_by_key(key: str):
    async with OrionClient() as client:
        # Fetch all artifacts
        artifacts = await client.get_artifacts()
        
        # Filter artifacts by the key
        filtered_artifacts = [artifact for artifact in artifacts if artifact.key == key]
        
        return filtered_artifacts

# Example usage
key_to_query = "your_artifact_key"
artifact = await get_artifact_by_key(key_to_query)
print(artifact)
In this example,
client.get_artifacts()
retrieves all the artifacts. Then we filter the artifacts based on the specified key. Replace
"your_artifact_key"
with the actual key you are interested in. If you need further details or a more precise implementation, checking the official Prefect documentation might be helpful once the service is available.
n
this is pretty close if you swap
OrionClient
for
get_client
and
get_artifacts
for
read_artifacts
@Marvin please revise
m
Certainly! Here's the revised example:
Copy code
python
from prefect.client import get_client

async def get_artifact_by_key(key: str):
    async with get_client() as client:
        # Fetch all artifacts
        artifacts = await client.read_artifacts()
        
        # Filter artifacts by the key
        filtered_artifacts = [artifact for artifact in artifacts if artifact.key == key]
        
        return filtered_artifacts

# Example usage
key_to_query = "your_artifact_key"
artifact = await get_artifact_by_key(key_to_query)
print(artifact)
In this updated version, I replaced
OrionClient
with
get_client
and
get_artifacts()
with
read_artifacts()
as requested. You can run this code with the appropriate key to query the artifact. For further details, you can refer to the Prefect documentation as well.
n
s
Thanks! I ended up using this @Marvin
Copy code
result = await client.read_latest_artifacts(artifact_filter=ArtifactCollectionFilter(key=dict(any_=[key])))
🔥 1
m
Ah, that makes sense. Using
read_latest_artifacts
with
ArtifactCollectionFilter
is a more efficient and direct approach. Here's how it fits into the function:
Copy code
python
from prefect.client import get_client
from prefect.orion.schemas.filters import ArtifactCollectionFilter

async def get_artifact_by_key(key: str):
    async with get_client() as client:
        # Use artifact filter to get the latest artifact by key
        result = await client.read_latest_artifacts(
            artifact_filter=ArtifactCollectionFilter(key=dict(any_=[key]))
        )
        return result

# Example usage
key_to_query = "your_artifact_key"
artifact = await get_artifact_by_key(key_to_query)
print(artifact)
This approach leverages the built-in filtering capabilities, making the query more efficient. Thank you for sharing your solution! If you have any more questions or need further assistance, feel free to ask.