Expand description
Prompt template models.
Provides data structures for user-defined prompt templates with variable substitution.
§Code Block Detection Edge Cases
Variable extraction automatically skips {{variable}} patterns inside fenced code blocks
to avoid capturing documentation examples. This section documents edge cases and behaviors.
§Supported Code Block Syntaxes
| Syntax | Supported | Notes |
|---|---|---|
```language ... ``` | ✓ | Standard fenced code block |
``` ... ``` | ✓ | Code block without language |
~~~ ... ~~~ | ✓ | Tilde fenced code block |
| Indented code (4 spaces) | ✗ | Only fenced blocks detected |
§Edge Cases
§1. Unclosed Code Blocks
Input: triple-backtick rust, let x = "{{var}}";, no closing triple-backtick
Behavior: Unclosed blocks are not detected, so variables inside ARE extracted. This is intentional - malformed content shouldn’t silently exclude variables.
§2. Nested Code Blocks (within markdown)
Input: Outer tilde block containing inner backtick block with {{inner_var}}
Behavior: Both tilde and backtick blocks are detected. Variables inside either syntax are excluded. Nested blocks are handled correctly.
§3. Variables at Block Boundaries
Input: {{before}} immediately before triple-backtick, {{after}} immediately after
Behavior: Both {{before}} and {{after}} are extracted. Only content
strictly between the opening and closing triple-backticks is excluded.
§4. Inline Code (single backticks)
Input: Use {{var}} syntax for variables. (single backticks around var)
Behavior: Single backticks DO NOT exclude variables. Only triple-backtick
fenced blocks are detected. {{var}} IS extracted.
§5. Empty Code Blocks
Input: Empty triple-backtick block
Behavior: Empty blocks are detected but contain no variables to exclude.
§Workarounds
If you need a {{variable}} pattern in your actual prompt output (not as a variable):
- Escape it: Use
\{\{literal\}\}(will be preserved literally) - Put it in a code block: Variables in fenced blocks are not substituted
- Use a variable with literal value: Define
open_brace/close_bracevariables
Macros§
- lazy_
regex 🔒 - Creates a compile-time verified regex wrapped in
LazyLock.
Structs§
- Code
Block Region - Represents a fenced code block region in content.
- Extracted
Variable - Result of extracting a variable from prompt content.
- Prompt
Template - A user-defined prompt template.
- Prompt
Variable - A template variable definition.
- Validation
Issue - A validation issue found in prompt content.
- Validation
Result - Validation result for prompt content.
Enums§
- Issue
Severity - Severity level for validation issues.
Constants§
- MAX_
VARIABLE_ VALUE_ LENGTH - Maximum length for variable values (64KB).
- RESERVED_
PREFIXES 🔒 - Reserved variable name prefixes that cannot be used.
Statics§
- CODE_
BLOCK_ 🔒BACKTICK_ PATTERN - Regex pattern for detecting fenced code blocks (triple backticks with optional language identifier).
Matches:
followed by optional language, then content, then - CODE_
BLOCK_ 🔒TILDE_ PATTERN - Regex pattern for detecting tilde fenced code blocks. Matches: ~~~ followed by optional language, then content, then ~~~
- VALIDATION_
PATTERN 🔒 - Regex pattern for detecting any content between
{{and}}. - VARIABLE_
PATTERN 🔒 - Regex pattern for extracting template variables:
{{variable_name}}. Supports dots for context template auto-variables like{{memory.id}}.
Functions§
- default_
required 🔒 - Default value for
requiredfield (true). - detect_
code_ blocks - Detects fenced code blocks in content.
- extract_
variables - Extracts variables from prompt content.
- extract_
variables_ 🔒with_ exclusions - Extracts variables from prompt content, excluding those inside code blocks.
- is_
in_ exclusion - Checks if a byte position falls within any exclusion region.
- is_
reserved_ variable_ name - Checks if a variable name uses a reserved prefix.
- sanitize_
variable_ value - Sanitizes a variable value to prevent template injection attacks.
- substitute_
variables - Substitutes variables in prompt content.
- validate_
prompt_ content - Validates prompt content for common issues.