<@ULVA73B9P> can you write a quick python function...
# ask-marvin
j
@Marvin can you write a quick python function to return the date of a deployment, given the deployment name?
m
thought for 55.4 seconds
Yep — if by “date of a deployment” you mean its creation date or last updated date, you can fetch the deployment by its full name and return one of those fields. In Prefect 3.x, the name format is:
"<flow_name>/<deployment_name>"
Quick example:
Copy code
python
from prefect.client.orchestration import get_client

def get_deployment_created_date(deployment_full_name: str):
    with get_client(sync_client=True) as client:
        deployment = client.read_deployment_by_name(deployment_full_name)
        return deployment.created
Usage:
Copy code
python
date = get_deployment_created_date("my-flow/my-deployment")
print(date)
If you want the last updated date instead:
Copy code
python
from prefect.client.orchestration import get_client

def get_deployment_updated_date(deployment_full_name: str):
    with get_client(sync_client=True) as client:
        deployment = client.read_deployment_by_name(deployment_full_name)
        return deployment.updated
If you want, I can also give you: - an async version - a version with error handling for “deployment not found” - a version that accepts just the deployment name and searches for matches
j
@Marvin add error handling, tell me where in the docs the return value of read_deployment_by_name is defined, and what is the significance of "deployment_full_name" vs just a deployment name?
m
thought for 114.7 seconds
Absolutely. Here’s a version with basic error handling:
Copy code
python
from prefect.client.orchestration import get_client
from prefect.exceptions import ObjectNotFound

def get_deployment_created_date(deployment_full_name: str):
    """
    Return the created timestamp for a deployment.

    Expected format:
        '<flow_name>/<deployment_name>'
    """
    try:
        with get_client(sync_client=True) as client:
            deployment = client.read_deployment_by_name(deployment_full_name)
            return deployment.created
    except ValueError as exc:
        raise ValueError(
            "Invalid deployment name format. Expected '<flow_name>/<deployment_name>'."
        ) from exc
    except ObjectNotFound as exc:
        raise LookupError(
            f"Deployment not found: {deployment_full_name}"
        ) from exc
If you want the updated timestamp instead:
Copy code
python
from prefect.client.orchestration import get_client
from prefect.exceptions import ObjectNotFound

def get_deployment_updated_date(deployment_full_name: str):
    """
    Return the updated timestamp for a deployment.

    Expected format:
        '<flow_name>/<deployment_name>'
    """
    try:
        with get_client(sync_client=True) as client:
            deployment = client.read_deployment_by_name(deployment_full_name)
            return deployment.updated
    except ValueError as exc:
        raise ValueError(
            "Invalid deployment name format. Expected '<flow_name>/<deployment_name>'."
        ) from exc
    except ObjectNotFound as exc:
        raise LookupError(
            f"Deployment not found: {deployment_full_name}"
        ) from exc
Where is the return value of
read_deployment_by_name
defined?
The method returns a
DeploymentResponse
object. You can see that in: - the API reference for the client: https://docs.prefect.io/v3/api-ref/python/prefect-client-orchestration-__init__/ - the schema docs for `DeploymentResponse`: https://docs.prefect.io/v3/api-ref/python/prefect-client-schemas-responses/ The actual returned model is defined in the
prefect.client.schemas.responses
module as
DeploymentResponse
, which includes fields like: -
id
-
name
-
created
-
updated
-
flow_id
-
work_pool_name
-
parameters
-
schedules
What’s the significance of
deployment_full_name
vs just deployment name?
This is important:
read_deployment_by_name(...)
does not accept only the deployment’s short name. It expects the full name in this format:
Copy code
python
"<flow_name>/<deployment_name>"
So this is valid:
Copy code
python
"my-flow/my-deployment"
And this is not valid:
Copy code
python
"my-deployment"
Why? Because deployment names are looked up in the context of a flow. The client splits the string on
/
into: -
flow_name
-
deployment_name
If you pass only
my-deployment
, Prefect can’t determine which flow it belongs to, and the method raises a
ValueError
. So
deployment_full_name
is just a clearer variable name than
deployment_name
, because it reminds you that the function expects the combined
flow/deployment
identifier, not the short deployment name alone. If you want, I can also give you a helper that accepts
flow_name
and
deployment_name
as separate arguments instead of requiring the combined string.