Skip to main content

Module prompt

Module prompt 

Source
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

SyntaxSupportedNotes
```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):

  1. Escape it: Use \{\{literal\}\} (will be preserved literally)
  2. Put it in a code block: Variables in fenced blocks are not substituted
  3. Use a variable with literal value: Define open_brace/close_brace variables

Macros§

lazy_regex 🔒
Creates a compile-time verified regex wrapped in LazyLock.

Structs§

CodeBlockRegion
Represents a fenced code block region in content.
ExtractedVariable
Result of extracting a variable from prompt content.
PromptTemplate
A user-defined prompt template.
PromptVariable
A template variable definition.
ValidationIssue
A validation issue found in prompt content.
ValidationResult
Validation result for prompt content.

Enums§

IssueSeverity
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 required field (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.