<@ULVA73B9P> what is difference between prefect va...
# ask-marvin
b
@Marvin what is difference between prefect variables and prefect string block ?
m
thought for 453.6 seconds
Great question! Here’s a quick, clear comparison in Prefect 3.x: - Purpose - Prefect Variables: Workspace-level key/value settings for non-sensitive configuration that you want to change without redeploying. Best for simple flags, URLs, branch names, etc. - String Block: A named Block instance that stores a string. It lives in the Blocks system (alongside JSON, Secret, etc.) and is referenced by block type/name. Use when you want to manage values as Blocks or compose them with other block-based configs. - Security - Variables: Not encrypted. Do not store secrets. - String Block: Also not encrypted. For secrets, use the Secret block (
prefect.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:
Copy code
prefect variable set MY_VAR "value"
      prefect variable get MY_VAR
      prefect variable ls
      prefect variable unset MY_VAR
- Python:
Copy code
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:
Copy code
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):
Copy code
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:
Copy code
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.
n
variables replace the need for string blocks. secret blocks are useful for secrets as they are encrypted, but in all cases where you want a string block, you should probably just use a variable
🙏 1