Implementation:Explodinggradients Ragas BasePrompt Class
| Field | Value |
|---|---|
| source | Explodinggradients_Ragas (GitHub) |
| domains | Prompts, Framework |
| last_updated | 2026-02-10 00:00 GMT |
Overview
The base prompt module defines the abstract BasePrompt class and lightweight I/O models (StringIO, BoolIO, StringPrompt) that form the foundation of the Ragas prompt hierarchy.
Description
BasePrompt is an abstract base class requiring subclasses to implement generate (single completion) and generate_multiple (multiple completions) async methods. It provides save and load methods for JSON-based prompt persistence with Ragas version tracking. The class stores a name (auto-derived from the class name via camel-to-snake conversion), language, and an optional original_hash for integrity checking. StringIO and BoolIO are simple Pydantic models wrapping a text string and a boolean respectively. StringPrompt is a concrete subclass of BasePrompt that sends raw text to an LLM via StringPromptValue and returns plain string output, suitable for simple text-in/text-out use cases.
Usage
Use StringPrompt directly for simple string-based LLM generation. Subclass BasePrompt to create custom prompt types with structured input/output handling. Use StringIO and BoolIO as I/O models in PydanticPrompt subclasses.
Code Reference
| Item | Detail |
|---|---|
| Source Location | src/ragas/prompt/base.py L23-227
|
| Classes | BasePrompt(ABC), StringIO(BaseModel), BoolIO(BaseModel), StringPrompt(BasePrompt)
|
| BasePrompt Signature | class BasePrompt(ABC) with abstract methods generate() and generate_multiple()
|
| Import | from ragas.prompt.base import BasePrompt, StringIO, StringPrompt
|
I/O Contract
Inputs (StringPrompt.generate)
| Parameter | Type | Description |
|---|---|---|
llm |
BaseRagasLLM |
The language model for text generation |
data |
str |
Raw text input to send to the LLM |
temperature |
Optional[float] |
Temperature for generation randomness |
stop |
Optional[List[str]] |
Stop sequences to end generation |
callbacks |
Callbacks |
Callback handlers for the generation process |
Outputs
| Method | Return Type | Description |
|---|---|---|
StringPrompt.generate() |
str |
Single generated text string |
StringPrompt.generate_multiple() |
List[str] |
List of n generated text strings |
BasePrompt.save() |
None |
Writes JSON file with prompt metadata |
BasePrompt.load() |
BasePrompt |
Restored prompt instance from JSON |
Usage Examples
from ragas.prompt.base import StringPrompt, StringIO, BoolIO
# Using StringPrompt for simple text generation
prompt = StringPrompt()
result = await prompt.generate(
llm=my_llm,
data="Summarize the following text: ..."
)
print(result) # Generated summary text
# Multiple generations
results = await prompt.generate_multiple(
llm=my_llm,
data="Give me a creative title for this article: ...",
n=3,
temperature=0.9,
)
print(results) # ['Title 1', 'Title 2', 'Title 3']
# Using StringIO as a Pydantic I/O model
io = StringIO(text="hello world")
print(io.text) # 'hello world'
Related Pages
- PydanticPrompt_Class - Structured prompt subclass of BasePrompt
- SimplePrompt_Class - Independent simple prompt class
- ImageTextPrompt_Class - Multi-modal prompt extending PydanticPrompt