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:Vibrantlabsai Ragas SimplePrompt

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

Overview

The Prompt class (aliased as SimplePrompt) provides a lightweight, dictionary-based prompt abstraction with string formatting, optional few-shot examples, and JSON serialization support.

Description

Prompt is a simple prompt class in Ragas that operates with plain dictionaries rather than Pydantic models. Unlike PydanticPrompt, which enforces typed input/output contracts, Prompt uses Python string formatting with named placeholders (e.g., {question}, {response}) and stores examples as tuples of plain dictionaries.

The class provides the following capabilities:

  • String formatting: The format() method substitutes keyword arguments into the instruction template using Python's str.format() mechanism and appends any registered few-shot examples.
  • Few-shot examples: Examples are stored as (input_dict, output_dict) tuples and can be added either at construction time or dynamically via add_example(). They are formatted into a human-readable "Example N: Input/Output" block when the prompt is rendered.
  • Optional response model: An optional Pydantic BaseModel can be attached for downstream validation, though the Prompt class itself does not enforce parsing or validation against the model.
  • Serialization: Prompts can be saved to JSON files (with optional gzip compression) via save() and restored via the load() classmethod. The response model schema is serialized for reference, but the model itself must be provided at load time.
  • Analytics tracking: Each call to format() fires a PromptUsageEvent for internal telemetry.

The module exports Prompt as its public API through the __all__ declaration.

Usage

Import Prompt when you need a quick, template-based prompt without the overhead of defining Pydantic input/output models. This is suitable for simple string interpolation tasks, one-off evaluations, or cases where the LLM output does not need structured parsing. For metrics or workflows that require validated, schema-conforming outputs, use PydanticPrompt instead.

Code Reference

Source Location

Signature

class Prompt:
    def __init__(
        self,
        instruction: str,
        examples: t.Optional[t.List[t.Tuple[t.Dict, t.Dict]]] = None,
        response_model: t.Optional[BaseModel] = None,
    ): ...

    def format(self, **kwargs) -> str: ...
    def add_example(self, input: t.Dict, output: t.Dict) -> None: ...
    def save(self, path: str) -> None: ...

    @classmethod
    def load(
        cls, path: str, response_model: t.Optional[BaseModel] = None
    ) -> "Prompt": ...

Import

from ragas.prompt import Prompt

I/O Contract

Inputs

Name Type Required Description
instruction str Yes A prompt template string with optional named placeholders (e.g., {question}, {context})
examples Optional[List[Tuple[Dict, Dict]]] No List of (input_dict, output_dict) example pairs for few-shot prompting
response_model Optional[BaseModel] No An optional Pydantic model for downstream response validation (not enforced by Prompt itself)

format() Parameters

Name Type Required Description
**kwargs keyword arguments Yes Named values to substitute into the instruction template placeholders

Outputs

Name Type Description
return (format) str The fully rendered prompt string with substituted values and appended examples
return (load) Prompt A new Prompt instance restored from a saved JSON file

Usage Examples

Basic Usage

from ragas.prompt import Prompt

prompt = Prompt("Answer the question: {question}")
formatted = prompt.format(question="What is 2+2?")
print(formatted)
# Output: Answer the question: What is 2+2?

With Few-Shot Examples

from ragas.prompt import Prompt

examples = [
    ({"question": "What is 1+1?"}, {"answer": "2"}),
    ({"question": "What is 3+3?"}, {"answer": "6"}),
]
prompt = Prompt(
    "Answer: {question}",
    examples=examples,
)
formatted = prompt.format(question="What is 5+5?")
print(formatted)

Adding Examples Dynamically

from ragas.prompt import Prompt

prompt = Prompt("Translate to {language}: {text}")
prompt.add_example(
    {"text": "Hello", "language": "Spanish"},
    {"translation": "Hola"},
)
formatted = prompt.format(text="Goodbye", language="French")

Saving and Loading

from ragas.prompt import Prompt

prompt = Prompt("Summarize: {text}")
prompt.save("my_prompt.json")

# Load back
loaded = Prompt.load("my_prompt.json")

# With gzip compression
prompt.save("my_prompt.json.gz")
loaded_gz = Prompt.load("my_prompt.json.gz")

With a Response Model

from pydantic import BaseModel
from ragas.prompt import Prompt


class AnswerModel(BaseModel):
    answer: str
    confidence: float


prompt = Prompt(
    "Answer the question: {question}",
    response_model=AnswerModel,
)
# Note: Prompt.save() will warn that response_model cannot be serialized.
# Re-provide it at load time:
# loaded = Prompt.load("prompt.json", response_model=AnswerModel)

Internal Details

Example Formatting

When examples are present, they are appended after the formatted instruction. Each example is rendered as:

Examples:

Example 1:
Input:
question: What is 1+1?
Output:
answer: 2

Example 2:
Input:
question: What is 3+3?
Output:
answer: 6

Serialization Format

The JSON save format includes:

  • format_version: Currently "1.0"
  • type: Always "Prompt" (used for validation on load)
  • instruction: The template string
  • examples: List of {"input": dict, "output": dict} objects
  • response_model_info: Schema metadata (class name, module, JSON Schema) if a response model is attached; null otherwise

When loading, if the saved prompt had a response model, the caller must provide a compatible model via the response_model parameter. The loader validates the provided model's schema against the saved schema and emits a warning on mismatch.

Related Pages

Page Connections

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