Overview
The BasePrompt class provides the foundational abstraction for representing, templating, and formatting LLM prompts within the Guardrails framework.
Description
BasePrompt is the base class for all prompt types in Guardrails. On initialization, it accepts a source string and optionally an output schema, performs constant substitution (replacing ${gr.<constant_name>} patterns with values from the Guardrails constants registry), and applies Template.safe_substitute to inject output schema placeholders. The class exposes properties for extracting template variable names and format instructions (the portion of the prompt starting at the first Guardrails constant reference). It provides methods for making variables optional (appending default-empty syntax), escaping curly braces for safe formatting, and locating the format instructions index. The format method is left abstract for subclasses (Prompt and Instructions) to implement.
Usage
Use this class as the base for any custom prompt type in Guardrails. Typically, consumers interact with the Prompt or Instructions subclasses rather than BasePrompt directly. It is used internally when constructing prompts from RAIL specifications or when building prompts programmatically with output schema injection.
Code Reference
Source Location
- Repository: Guardrails
- File:
guardrails/prompt/base_prompt.py
Signature
class BasePrompt:
def __init__(
self,
source: str,
output_schema: Optional[str] = None,
*,
xml_output_schema: Optional[str] = None,
)
def __repr__(self) -> str
def __str__(self) -> str
@property
def variable_names(self) -> List[str]
@property
def format_instructions(self) -> str
def substitute_constants(self, text: str) -> str
def get_prompt_variables(self) -> List[str]
def format(self, **kwargs) -> "BasePrompt" # Abstract
def make_vars_optional(self)
def get_format_instructions_idx(self, text: str) -> Optional[int]
def escape(self) -> str
def _to_request(self) -> str
Import
from guardrails.prompt.base_prompt import BasePrompt
I/O Contract
__init__
| Parameter |
Type |
Description
|
source |
str |
The raw prompt template string
|
output_schema |
Optional[str] |
JSON Schema string to substitute into $output_schema placeholder
|
xml_output_schema |
Optional[str] |
XML Schema string to substitute into $xml_output_schema placeholder (keyword-only)
|
Properties
| Property |
Type |
Description
|
variable_names |
List[str] |
List of template variable names found in the prompt source
|
format_instructions |
str |
The substring of the prompt starting from the first Guardrails constant reference
|
Methods
| Method |
Parameters |
Returns |
Description
|
substitute_constants |
text: str |
str |
Replaces all ${gr.<name>} patterns with values from the constants registry
|
get_prompt_variables |
N/A |
List[str] |
Returns the list of template variable names
|
format |
**kwargs |
BasePrompt |
Abstract method; subclasses must implement to return a formatted prompt
|
make_vars_optional |
N/A |
None |
Rewrites variables from {var} to {var:} to make them optional
|
get_format_instructions_idx |
text: str |
Optional[int] |
Returns the index of the first Guardrails constant reference in the text
|
escape |
N/A |
str |
Escapes single curly braces to double curly braces for safe string formatting
|
_to_request |
N/A |
str |
Returns the prompt source string for inclusion in an API request
|
Usage Examples
from guardrails.prompt.base_prompt import BasePrompt
# BasePrompt is typically used through its subclasses (Prompt, Instructions)
# Direct usage for constant substitution and schema injection:
prompt = BasePrompt(
source="Given this schema: $output_schema\nGenerate valid JSON.",
output_schema='{"type": "object", "properties": {"name": {"type": "string"}}}'
)
print(prompt.source)
# "Given this schema: {\"type\": \"object\", ...}\nGenerate valid JSON."
# Inspect template variables
prompt_with_vars = BasePrompt(source="Hello ${name}, your task is ${task}.")
print(prompt_with_vars.variable_names)
# ["name", "task"]
# Make variables optional
prompt_with_vars.make_vars_optional()
print(prompt_with_vars.source)
# "Hello {name:}, your task is {task:}."
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.