Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Guardrails ai Guardrails Instructions

From Leeroopedia
Knowledge Sources
Domains Prompt Engineering, LLM Integration
Last Updated 2026-02-14 00:00 GMT

Overview

The Instructions class represents secondary LLM instructions (e.g., system prompts) that extend BasePrompt with formatting and equality semantics.

Description

Instructions is a subclass of BasePrompt designed to represent secondary instructions passed to an LLM. Different models use these differently; for example, chat models typically receive instructions via the system prompt. The class overrides __repr__ to display a truncated representation prefixed with "Instructions(", implements __eq__ for source-based equality comparison, and provides a concrete format method that substitutes only the keyword arguments whose names appear as template variables in the source string. The format method returns a new Instructions instance with the substituted content.

Usage

Use this class when constructing system-level or secondary instructions for LLM calls within the Guardrails framework. It is typically instantiated from RAIL specifications or programmatically when building guard configurations that include separate system instructions.

Code Reference

Source Location

  • Repository: Guardrails
  • File: guardrails/prompt/instructions.py

Signature

class Instructions(BasePrompt):
    def __repr__(self) -> str
    def __eq__(self, __value: object) -> bool
    def format(self, **kwargs) -> "Instructions"

Import

from guardrails.prompt.instructions import Instructions
# or
from guardrails.prompt import Instructions

I/O Contract

__init__ (inherited from BasePrompt)

Parameter Type Description
source str The raw instructions 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 template; only keys matching template variables are used
Returns Type Description
Formatted instructions Instructions A new Instructions 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 an Instructions instance with the same source string

Usage Examples

from guardrails.prompt.instructions import Instructions

# Create instructions with template variables
instructions = Instructions(
    source="You are a helpful assistant. Your task is to ${task}. "
           "Respond in ${language}."
)

# Format with keyword arguments
formatted = instructions.format(task="extract entities", language="English")
print(formatted.source)
# "You are a helpful assistant. Your task is extract entities. Respond in English."

# Equality check
instr_a = Instructions(source="Be concise.")
instr_b = Instructions(source="Be concise.")
assert instr_a == instr_b  # True

# Representation
print(repr(instructions))
# Instructions(You are a helpful assistant. Your task is to ...)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment