Hi all, I'm overhauling our current Prefect 2.19....
# ask-community
r
Hi all, I'm overhauling our current Prefect 2.19.1 to Prefect 3.1.0. Because our codebase dynamically generates a lot of deployments we use Prefect Python and not Prefect YAML to manage everything. Surprisingly the Python code has really stayed mostly the same between versions, which is great. Unsurprisingly some of our side scripts we use to manage the Cloud are now broken. One in particular we use to clear backlogs of Late runs is giving me fits. This code used to work
Copy code
import asyncio

from prefect.states import State
from prefect.client import get_client

MAX_RUNS_TO_DELETE = 5000
PREFECT_OFFSET_MAX = 200


async def remove_all_flows():
    client = get_client()
    for i in range(0, MAX_RUNS_TO_DELETE, PREFECT_OFFSET_MAX):
        flow_runs = await client.read_flow_runs(offset=i)
        for flow_run in flow_runs:
            if flow_run.state_name == "Late":
                flow_id = flow_run.id
                print("deleting", flow_id)
                await client.set_flow_run_state(flow_run_id=flow_id, state=State(type="CANCELLED"))


asyncio.run(remove_all_flows())
But now returns an error
Copy code
pydantic.errors.PydanticUserError: `StateCreate` is not fully defined; you should define all referenced types, then call `StateCreate.model_rebuild()`.
No amount of defining State parameters (or passing directly a StateCreate object) fixes the issue. I'm increasingly convinced the old code is now a dead end. Could the Prefect team advise on what the idiomatic way to cancel a Late flow run would be under Prefect 3.1+?
c
Hi Robert - this may be a bug; I'm investigating
r
Great thanks!
I'm seeing the same error in a separate script that runs
Copy code
await client.create_flow_run_from_deployment(deployment_id=deployment_id, parameters=parameters)
Where
deployment_id
is a string ID and
parameters
is a dict of parameters in various formats
In case this helps
c
ok so this is tricky - there is some weird circular dependencies lurking in the codebase that this is triggering; there's a simple workaround for now which is to run this basic import _first_:
Copy code
from prefect import flow
it doesn't really matter what you import from here but importing from the main module ensures that whatever is happening is avoided. It'll take some more time to find a proper solution but hopefully this unblocks you for now!
actually you could import the
State
object directly from here and it works:
Copy code
from prefect import State
r
veeeeery weird but I'll take it!
😅 1
y
Thanks @Chris White, stumbled into that as well today with the latest prefect v3 version (3.1.3) Changing the import orders indeed fixed that.
🙌 2
c
Thank you @Chris White. Also noting that I am seeing the same issue with Prefect 3.1.2 and changing the order works
c
yup that's expected the issue has not been fixed yet unfortunately; we're currently doing a big push on type completeness that should resolve this but we won't know until the project is complete
thank you 1
👍 1
174 Views