batuan
10/08/2025, 1:51 PMMarvin
10/08/2025, 1:51 PMMarvin
10/08/2025, 1:58 PMprefect.blocks.system.Secret) which is encrypted.
- Scope and naming
- Variables: Single global name per workspace (e.g., MY_VAR).
- String Block: Namespaced by type and name (e.g., string/my-string), and you can create multiple named instances.
- Access and management
- Variables:
- CLI:
prefect variable set MY_VAR "value"
prefect variable get MY_VAR
prefect variable ls
prefect variable unset MY_VAR
- Python:
from prefect.variables import Variable
Variable.set(name="MY_VAR", value="hello", overwrite=True)
value = Variable.get("MY_VAR", default="fallback")
Variable.unset("MY_VAR")
- String Block:
- Python:
from prefect.blocks.system import String
String(value="hello").save("my-string") # create/update
s = String.load("my-string")
print(s.value) # -> "hello"
- CLI (manage types/docs/UI link):
prefect block ls
prefect block inspect string/my-string
prefect block delete string/my-string
prefect block create string # opens UI link to create the block
prefect block register --help # register custom block types
- Templating (e.g., in prefect.yaml or block fields)
- Variables: {{ variables.MY_VAR }}
- String Block: {{ block('string/my-string').value }}
- Example:
storage:
repository: "<https://github.com/org/repo>"
branch: "{{ variables.DEPLOYMENT_BRANCH }}"
some_config_value: "{{ block('string/my-string').value }}"
- Data types
- Variables: Typically used as strings (API accepts a “strict variable value”); keep usage simple and non-sensitive.
- String Block: Plain string only. For structured data, use the JSON block instead of String.
- Versioning and history
- Neither Variables nor String blocks give you automatic version history of values. If you need versioning, create new names (e.g., string/my-string-v2) or manage via code.
- RBAC/permissions
- Both are workspace objects and respect Prefect Cloud RBAC (different permissions categories for Variables vs Blocks).
Recommended usage
- Use Variables for simple, non-sensitive configuration that may change frequently (URLs, feature flags, git branches).
- Use the Secret block for sensitive values.
- Use the JSON block for structured configuration.
- Use the String block when you specifically want to model the value as a Block and reference it via block templating or compose with other Blocks.
Docs and references
- Variables: Variables (concepts)
- Blocks: Blocks (concepts)
- Templating helpers: Templating API
- CLI block commands: prefect block CLI
If you share your use case (simple flag vs secret vs structured config), I can suggest the most fitting pattern and a ready-to-use snippet.Nate
10/08/2025, 2:23 PM