<@ULVA73B9P> I am trying out variables. Prefect cl...
# ask-marvin
m
@Marvin I am trying out variables. Prefect cloud suggests I can format a variable in the way of json but when I use prefect cli to list out the variable, a pydantic error raises regarding expecting a string. I can upload the error shortly but am wondering if a job variable can be a dictionary.
m
It appears that your issue with the Prefect CLI expecting a string instead of a dictionary for variables is not well-documented. There is no direct information from the Prefect documentation or GitHub issues addressing this specific problem. Given that Prefect variables should support JSON configuration, here are a few troubleshooting steps you might take: 1. Ensure JSON Correctness: Make sure that your JSON is correctly formatted and valid. 2. JSON as String: Prefect might require the JSON to be passed as a string. Ensure that you are passing the JSON object correctly by stringifying it if necessary. 3. Inspect CLI Commands: Validate that the CLI command you're using to set the variable is correctly formatted. Sometimes the issue lies in how the command-line argument is parsed. If these steps don't resolve your issue, you might consider raising a new issue on Prefect's GitHub repository. Here's a general structure for passing a dictionary as a string in the CLI:
Copy code
sh
prefect variables set MY_VARIABLE '{"key": "value"}'
If you need further details or examples, feel free to ask!
m
@Marvin can you give me an example of the json format I can test this in?
m
It seems that the provided example is not relevant to JSON formatting for Prefect variables. Let's try a properly formatted JSON example that you can test: ### Sample JSON Format:
Copy code
json
{
    "key1": "value1",
    "key2": {
        "nestedKey": "nestedValue"
    },
    "key3": [1, 2, 3]
}
### Using Prefect CLI: You can pass this JSON as a string when setting a variable via the Prefect CLI. For example:
Copy code
sh
prefect variables set MY_VARIABLE '{"key1": "value1", "key2": {"nestedKey": "nestedValue"}, "key3": [1, 2, 3]}'
### Verify the Variable: To verify that the variable is correctly set, you can use:
Copy code
sh
prefect variables get MY_VARIABLE
This should print out:
Copy code
json
{
    "key1": "value1",
    "key2": {
        "nestedKey": "nestedValue"
    },
    "key3": [1, 2, 3]
}
Make sure your JSON is valid and correctly formatted to avoid pydantic errors. If you still encounter issues, consider verifying the JSON format using online JSON validators such as JSONLint. If you have further questions or need additional assistance, feel free to ask!
n
hi @Matthew Jeffs - what does
prefect version
show for you?
m
Copy code
Version:             2.19.5
API version:         0.8.4
Python version:      3.11.9
Git commit:          538dc374
Built:               Thu, Jun 13, 2024 2:50 PM
OS/Arch:             linux/x86_64
Profile:             prefect-cloud
Server type:         cloud
@Nate I found out that I had to create python file and use the Prefect SDK to make a variable and make the the variable into a string representing a json to get it to work. It seems like I am still working out some kinks in it. I figured variables were just strings but it really threw me off that I can put in json and format it using the UI,
👍 1
@Marvin I am reading that values in variables must be less than 5000 characters in the docs but when I put in a value that is 321 characters I get this:
Copy code
pydantic.error_wrappers.ValidationError: 1 validation error for VariableUpdate
value
  ensure this value has at most 255 characters (type=value_error.any_str.max_length; limit_value=255)
That limit I interpreted here: https://docs.prefect.io/latest/guides/variables/ to be for the name of the variable and not the value itself.
m
According to the Prefect documentation, variable values must adhere to the following guidelines: - Variable Names: Must be unique and have no more than 255 characters. They should only contain lowercase alphanumeric characters (
[a-z, 0-9]
) or underscores (
_
). Spaces are not allowed. - Variable Values: Must have less than or equal to 5000 characters. The error you're encountering suggests that there might be another validation enforcing a stricter limit on the length of the variable values, specifically 255 characters, which contradicts the documentation. This could be a bug or a specific constraint in a particular Prefect version. For further assistance and clarity, the best course of action would be: 1. Verify the Prefect Version: Ensure you are using the latest version of Prefect, as this issue might have been resolved in recent updates. 2. Report the Issue: If you're already on the latest version and the issue persists, consider reporting it on Prefect’s GitHub Issues. ### Additional Information Here is the link to the relevant section in the Prefect documentation detailing the size constraints for variables. If there's anything more I can assist you with, please let me know!
m
Thank you