How to Use Variable.get in Airflow: Simple Guide
Use
Variable.get in Airflow to retrieve stored variables by their key name. It allows you to provide a default value if the variable is missing and can return the value as a string or parsed JSON.Syntax
The Variable.get method has this syntax:
key: The name of the variable to fetch.default_var: Optional value returned if the variable does not exist.deserialize_json: Optional boolean to parse the value as JSON if set toTrue.
python
Variable.get(key, default_var=None, deserialize_json=False)
Example
This example shows how to get a variable named my_var with a default fallback and JSON parsing:
python
from airflow.models import Variable # Get a simple string variable my_var = Variable.get('my_var', default_var='default_value') print(f"Value of my_var: {my_var}") # Get a JSON variable and parse it my_json_var = Variable.get('my_json_var', default_var='{}', deserialize_json=True) print(f"Parsed JSON variable: {my_json_var}")
Output
Value of my_var: default_value
Parsed JSON variable: {}
Common Pitfalls
Common mistakes include:
- Not providing
default_varwhich causes an error if the variable is missing. - Forgetting to set
deserialize_json=Truewhen expecting a JSON object, resulting in a string instead of a dictionary. - Using incorrect variable keys that do not exist in Airflow's Variable store.
python
from airflow.models import Variable # Wrong: No default_var, will raise KeyError if 'missing_var' does not exist # missing_var = Variable.get('missing_var') # Right: Provide default_var to avoid errors missing_var = Variable.get('missing_var', default_var='fallback') # Wrong: Forgetting to deserialize JSON json_var = Variable.get('json_var') # returns string # Right: Deserialize JSON to get dict json_var = Variable.get('json_var', deserialize_json=True)
Quick Reference
Remember these tips when using Variable.get:
- Always use
default_varto avoid errors if the variable is missing. - Use
deserialize_json=Trueto get Python objects from JSON strings. - Variable values are stored as strings in Airflow's backend.
Key Takeaways
Use Variable.get with the variable key to retrieve stored values in Airflow.
Always provide default_var to prevent errors if the variable is missing.
Set deserialize_json=True to automatically parse JSON strings into Python objects.
Variable values are stored as strings, so parsing is needed for complex data types.
Incorrect keys or missing defaults cause runtime errors.