Overview
The Prompt class represents the primary LLM prompt text, extending BasePrompt with concrete formatting and equality semantics.
Description
Prompt is a subclass of BasePrompt that represents the primary instructions passed to an LLM. It provides a concrete format method that uses Template.safe_substitute to replace template variables with provided keyword arguments, filtering to only variables that exist in the prompt source. The method returns a new Prompt instance containing the formatted text. The class also implements __eq__ for source-based equality comparison between Prompt instances.
Usage
Use this class when constructing the primary user-facing prompt for LLM calls within the Guardrails framework. It is the standard prompt class used in Guard configurations and is instantiated from RAIL specifications or programmatically when defining guard input prompts.
Code Reference
Source Location
- Repository: Guardrails
- File:
guardrails/prompt/prompt.py
Signature
class Prompt(BasePrompt):
def __eq__(self, __value: object) -> bool
def format(self, **kwargs) -> "Prompt"
Import
from guardrails.prompt.prompt import Prompt
# or
from guardrails.prompt import Prompt
I/O Contract
__init__ (inherited from BasePrompt)
| 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)
|
format
| Parameter |
Type |
Description
|
**kwargs |
Any |
Keyword arguments to substitute into the prompt template; only keys matching template variables are used
|
| Returns |
Type |
Description
|
| Formatted prompt |
Prompt |
A new Prompt instance with template variables substituted
|
__eq__
| Parameter |
Type |
Description
|
__value |
object |
The object to compare against
|
| Returns |
Type |
Description
|
| Equality result |
bool |
True if __value is a Prompt instance with the same source string
|
Usage Examples
from guardrails.prompt import Prompt
# Create a prompt with template variables
prompt = Prompt(source="Translate the following ${source_lang} text to ${target_lang}: ${text}")
# Format the prompt with specific values
formatted = prompt.format(
source_lang="English",
target_lang="French",
text="Hello, world!"
)
print(str(formatted))
# "Translate the following English text to French: Hello, world!"
# Equality comparison
prompt_a = Prompt(source="Generate a summary.")
prompt_b = Prompt(source="Generate a summary.")
assert prompt_a == prompt_b # True
# Extra kwargs are safely ignored
formatted2 = prompt.format(
source_lang="English",
target_lang="Spanish",
text="Good morning",
unused_param="ignored"
)
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.