Hi, I’m trying to use the Advanced Run Configurati...
# ask-community
h
Hi, I’m trying to use the Advanced Run Configuration settings in Prefect Cloud to create a key:value Context that I can then refer to in my code - but using
Copy code
prefect.context['my_run_parameter']
in my Python code results in the following runtime error:
Copy code
Failed to load and execute Flow's environment: KeyError('my_run_parameter',)
I’ve tried lots of variations on this syntax e.g.
Copy code
prefect.context.get('my_run_parameter')
but nothing seems to work. Any advice/assistance you can provide would be gratefully received, thanks!
a
@Huw Ringer it should be:
Copy code
prefect.context['parameters'].get("your_parameter_name")
The “parameters” in the context is a dictionary of parameter names as keys and parameter values as values
h
Ah! Thanks so much Anna - will give that a try!
Unfortunately, after trying the above am now getting the error:
Failed to load and execute Flow's environment: KeyError('parameters',)
For context, here’s what my Prefect Cloud ‘Advanced Run Configuration’ section looks like:
a
Can you try a simple flow first and confirm whether it’s working in a simple flow? Here is an example you could use:
Copy code
from prefect import Flow, Parameter, task
import prefect


@task(log_stdout=True)
def hello_world(user_input: str):
    print(prefect.context["parameters"].get("user_input"))
    print(f"hello {user_input}")


with Flow("test-flow") as flow:
    param = Parameter("user_input", default="world")
    hw = hello_world(param)

if __name__ == "__main__":
    flow.run()
e.g. this should say Hello to you 🙂
Copy code
if __name__ == "__main__":
    flow.run(parameters=dict(user_input="Huw"))
h
Running through Prefect Cloud?
a
yup
maybe the quotation marks cause the issue for you with “Value”? unlikely, but…
h
Your example runs fine on my local agent, but not on Prefect Cloud (I’d need to flow.register it, check it into Github, set flow.storage and flow.run_config parameters; etc.)….
[2021-11-16 12:55:27+0000] INFO - prefect.FlowRunner | Beginning Flow run for 'test-flow'
[2021-11-16 12:55:27+0000] INFO - prefect.TaskRunner | Task 'user_input': Starting task run...
[2021-11-16 12:55:27+0000] INFO - prefect.TaskRunner | Task 'user_input': Finished task run for task with final state: 'Success'
[2021-11-16 12:55:27+0000] INFO - prefect.TaskRunner | Task 'hello_world': Starting task run...
[2021-11-16 12:55:27+0000] INFO - prefect.TaskRunner | world
[2021-11-16 12:55:27+0000] INFO - prefect.TaskRunner | hello world
[2021-11-16 12:55:27+0000] INFO - prefect.TaskRunner | Task 'hello_world': Finished task run for task with final state: 'Success'
[2021-11-16 12:55:27+0000] INFO - prefect.FlowRunner | Flow run SUCCESS: all reference tasks succeeded
Although it works when run programmatically on a local agent, note I’m not invoking my flow using code (i.e. flow.run) - I’m invoking it specifically by clicking on the Run button in Prefect Cloud, then clicking on Show Advanced Run Configuration, then entering the key:value pair in the Context box (not user input), and it’s this Context dictionary value that I seem unable to somehow fetch via the API
Removing the quotes from “Value” didn’t make any difference incidentally
a
@Huw Ringer so this basic example didn’t work for you in Prefect Cloud? I’m confident that registering this simple example will work when running through Advanced Run configuration in the UI. Perhaps there is some issue with the registration process or setting the run configuration. Could you show how you approached this so that we can go step by step and inspect what is the issue?
so the steps you would need: • create flow file e.g. testflow.py
Copy code
from prefect import Flow, Parameter, task
import prefect


@task(log_stdout=True)
def hello_world(user_input: str):
    print(prefect.context["parameters"].get("user_input"))
    print(f"hello {user_input}")


with Flow("test-flow") as flow:
    param = Parameter("user_input", default="world")
    hw = hello_world(param)
then register:
Copy code
prefect register --project YOUR_PROJECT -p testflow.py
then the above command returns a flow uuid you can use to run the flow:
Copy code
prefect run --id UUID-FROM-OUTPUT-ABOVE --param user_input=Huw --watch
👀 1