How to retrieve the run config labels from within the flow?

View in #prefect-community on Slack

Geert-Jan_Van_den_Bogaerde @Geert-Jan_Van_den_Bogaerde: Hi all, anyone know if there is a way to retrieve the run config parameters, specifically the labels, from within a flow programmatically at flow runtime? Haven’t been able to find it in the docs.

Kevin_Kho @Kevin_Kho: There is no easy way exposed so you’d need to use the GraphQL API. Let me see if I can find an example

Geert-Jan_Van_den_Bogaerde @Geert-Jan_Van_den_Bogaerde: Thanks for the quick reply, Kevin! Actually, this seems to work after a little more digging:

@task
def get_current_flow_run_labels():
    logger = prefect.context.get('logger')
    flow_run_id = prefect.context.get("flow_run_id")
    flow_run_view = FlowRunView.from_flow_run_id(flow_run_id)
    labels = flow_run_view.labels
    logger.info(f"Labels: {labels}")
    return labels

Kevin_Kho @Kevin_Kho: Oh nice. Glad you figured it out

Geert-Jan_Van_den_Bogaerde @Geert-Jan_Van_den_Bogaerde: Funny story: it was actually Github Copilot (https://copilot.github.com) that got me on the right track, it didn’t get it 100% correct but suggested FlowRunView which got me there :slightly_smiling_face:

GitHub Copilot: GitHub Copilot · Your AI pair programmer

Kevin_Kho @Kevin_Kho: No way! That’s wild. Has it been accurate for you when working with Prefect?

Geert-Jan_Van_den_Bogaerde @Geert-Jan_Van_den_Bogaerde: More and more so. Part of that is that is clearly learns from your own codebase as well as the larger training set, and it must prioritize those somehow: it will almost always recognize patterns that I repeat a lot in my own codebase. For instance i very often use task_args = dict(name = “some friendly name”), and it will usually suggest that automatically when i call a task. Another example: we almost always do a transform task on 3 different types of dataframes in turn as 3 task calls, when I code the first two in a new flow it will without fail complete the third one for me

But other times it’s suggesting things that must come from other codebases, as I’ve just checked and we’ve never used FlowRunView before in our repo :slight_smile:
Besides the prefect specific stuff, for pandas it clearly has a lot of training data, as there it’s usually enough to just give it a comment describing what you want your function to do and it will pretty much write the whole thing, if it’s anything remotely common

Kevin_Kho @Kevin_Kho: Wow

@Anna_Geller: thanks for sharing, very cool!

1 Like