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:Explodinggradients Ragas PydanticPrompt Class

From Leeroopedia


Field Value
source Explodinggradients_Ragas (GitHub)
domains Prompts, Framework
last_updated 2026-02-10 00:00 GMT

Overview

The PydanticPrompt class provides structured, type-safe prompting with Pydantic input/output models, supporting generation, output parsing with retries, language adaptation, and JSON serialization.

Description

PydanticPrompt[InputModel, OutputModel] is a generic class that extends BasePrompt. It defines class-level attributes for input_model, output_model, instruction, and examples. The to_string method assembles a complete prompt string including the instruction, JSON schema for the output model, formatted examples, and serialized input data. The generate and generate_multiple methods handle three LLM backends: standard BaseRagasLLM, InstructorBaseRagasLLM, and legacy LangChain LLMs. Output parsing is handled by RagasOutputParser with configurable retry logic via FixOutputFormat. The adapt method translates prompt examples to a target language using the TranslateStatements prompt. The class provides save/load methods for JSON persistence with hash-based integrity verification.

Usage

Subclass PydanticPrompt with concrete Pydantic models for input and output, set the instruction and optionally examples, then call generate() with an LLM and input data. Override process_input and process_output for custom pre/post-processing.

Code Reference

Item Detail
Source Location src/ragas/prompt/pydantic_prompt.py L82-627
Class Signature class PydanticPrompt(BasePrompt, Generic[InputModel, OutputModel])
Key Methods generate(), generate_multiple(), adapt(), to_string(), save(), load()
Import from ragas.prompt import PydanticPrompt

I/O Contract

Inputs (generate)

Parameter Type Description
llm Union[BaseRagasLLM, InstructorBaseRagasLLM, BaseLanguageModel] Language model instance
data InputModel Pydantic model instance with input data
temperature Optional[float] Generation temperature
stop Optional[List[str]] Stop sequences
callbacks Optional[Callbacks] Callback handlers
retries_left int Number of retry attempts for invalid output (default 3)

Outputs

Method Return Type Description
generate() OutputModel Single parsed output model instance
generate_multiple() List[OutputModel] List of n parsed output model instances
adapt() PydanticPrompt New prompt with translated examples
to_string() str Complete prompt string with schema and examples

Usage Examples

from pydantic import BaseModel
from ragas.prompt import PydanticPrompt

class QuestionInput(BaseModel):
    context: str
    question: str

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

class QAPrompt(PydanticPrompt[QuestionInput, AnswerOutput]):
    instruction = "Answer the question based on the context."
    input_model = QuestionInput
    output_model = AnswerOutput
    examples = [
        (
            QuestionInput(context="The sky is blue.", question="What color is the sky?"),
            AnswerOutput(answer="Blue", confidence=0.95),
        )
    ]

prompt = QAPrompt()
result = await prompt.generate(
    llm=my_llm,
    data=QuestionInput(context="Water boils at 100C.", question="At what temperature does water boil?"),
)
print(result.answer)       # "100 degrees Celsius"
print(result.confidence)   # 0.92

Related Pages

Page Connections

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