Hi there I'm trying to query the Prefect Cloud AP...
# ask-community
d
Hi there I'm trying to query the Prefect Cloud API run history to find the FAILED tasks in a set of flow runs, and I'm having trouble working out how to filter for the tasks which ended in FAILED state. Filtering by the flow run IDs is fine, but that only returns a maximum of 200 tasks. There are a lot more tasks than that in each flow run, so I want to only return those tasks which ended in FAILED state. Could anyone help me figure out what I need to do to get this work, please? I've looked at the documentation and the source code, and I can see some simple examples, but I haven't been able to figure out how to adapt those to do what I need to here. This is a cut-down version of the code I have so far.
Copy code
import asyncio

import prefect.client.schemas.filters as filters
from prefect.client.orchestration import get_client
from prefect.server.schemas.states import StateType

run_ids = [  ... a list of the flow run IDs I'm interested in ... ]

client = get_client()

tasks = asyncio.run(
    client.read_task_runs(
        flow_run_filter=filters.FlowRunFilter(id=filters.FlowRunFilterId(any_=run_ids)),   # <-- This works by itself
        task_run_filter=filters.TaskRunFilterState(type=filters.TaskRunFilterStateType(any_=[StateType.FAILED])), # <-- this doesn't work
    )
)
This fails with:
Copy code
prefect.exceptions.PrefectHTTPStatusError: Client error '422 Unprocessable Entity' for url '<https://api.prefect.cloud/api/accounts/8d8b3f47-6199-40be-98a9-1bc1b3056ee4/workspaces/71539e7c-8525-4c61-842e-90aba3248e79/task_runs/filter>'
Response: {'exception_message': 'Invalid request received.', 'exception_detail': [{'loc': ['body', 'task_runs', 'type'], 'msg': 'extra fields not permitted', 'type': 'value_error.extra'}], 'request_body': {'flows': None, 'flow_runs': {'id': {'any_': ['7924bcd8-8dea-46a0-a6f4-f47fc84002dd', 'ad2d8cd7-8302-4bf2-b434-5f923ded1ec3', '239fc509-f4b1-4b20-a37d-4a66c4d6ef37', '4e5fa333-ecf6-4a9b-bc7c-1572b8c1c4b9', '2d995267-d507-48c1-a2f4-d1b4ef60bbec']}}, 'task_runs': {'type': {'any_': ['FAILED']}, 'name': None}, 'deployments': None, 'sort': None, 'limit': None, 'offset': 0}}
I've tried a few variants for the
task_run_filter
but I always get more of less the same error.