Implementation:Explodinggradients Ragas FewShotPydanticPrompt Class
| Field | Value |
|---|---|
| source | Explodinggradients_Ragas (GitHub) |
| domains | Prompts, Few_Shot |
| last_updated | 2026-02-10 00:00 GMT |
Overview
The FewShotPydanticPrompt class extends PydanticPrompt to dynamically select the most relevant Pydantic-typed examples at generation time using an embeddings-based ExampleStore.
Description
FewShotPydanticPrompt[InputModel, OutputModel] is a dataclass that wraps a PydanticPrompt with an ExampleStore for dynamic example retrieval. The ExampleStore abstract class defines get_examples and add_example interfaces operating on Pydantic BaseModel instances. InMemoryExampleStore implements this using embeddings: it serializes input models to JSON for embedding generation, stores embeddings in a list, and retrieves the top-k most similar examples above a configurable similarity threshold using NumPy-based cosine similarity. The generate_multiple override retrieves relevant examples from the store before delegating to the parent PydanticPrompt.generate_multiple. The from_pydantic_prompt class method converts an existing PydanticPrompt into a few-shot variant by transferring all examples into a new InMemoryExampleStore.
Usage
Use FewShotPydanticPrompt.from_pydantic_prompt() to convert an existing prompt, or instantiate directly with an ExampleStore. Call add_example() to grow the example pool. Each generate() call dynamically selects the best examples for the given input.
Code Reference
| Item | Detail |
|---|---|
| Source Location | src/ragas/prompt/few_shot_pydantic_prompt.py L97-155
|
| Classes | ExampleStore(ABC), InMemoryExampleStore, FewShotPydanticPrompt(PydanticPrompt, Generic[InputModel, OutputModel])
|
| Key Methods | generate_multiple(), add_example(), from_pydantic_prompt()
|
| Import | from ragas.prompt.few_shot_pydantic_prompt import FewShotPydanticPrompt
|
I/O Contract
Inputs
| Parameter | Type | Description |
|---|---|---|
example_store |
ExampleStore |
Store for managing and retrieving examples |
top_k_for_examples |
int |
Number of examples to retrieve (default 5) |
threshold_for_examples |
float |
Minimum similarity threshold (default 0.7) |
llm (generate) |
BaseRagasLLM |
Language model for generation |
data (generate) |
InputModel |
Pydantic input data for example retrieval and generation |
Outputs
| Method | Return Type | Description |
|---|---|---|
generate() |
OutputModel |
Single parsed output with dynamically chosen examples |
generate_multiple() |
List[OutputModel] |
Multiple parsed outputs |
from_pydantic_prompt() |
FewShotPydanticPrompt |
Converted prompt with embedded example store |
Usage Examples
from ragas.prompt.few_shot_pydantic_prompt import FewShotPydanticPrompt, InMemoryExampleStore
from ragas.prompt import PydanticPrompt
from pydantic import BaseModel
class QInput(BaseModel):
question: str
class QOutput(BaseModel):
answer: str
class MyPrompt(PydanticPrompt[QInput, QOutput]):
instruction = "Answer the question."
input_model = QInput
output_model = QOutput
examples = [
(QInput(question="What is 2+2?"), QOutput(answer="4")),
(QInput(question="What is H2O?"), QOutput(answer="Water")),
]
# Convert existing prompt to few-shot
few_shot = FewShotPydanticPrompt.from_pydantic_prompt(
MyPrompt(), embeddings=my_embedding_model
)
# Add more examples dynamically
few_shot.add_example(QInput(question="What is pi?"), QOutput(answer="3.14159"))
# Generate with dynamic example selection
result = await few_shot.generate(llm=my_llm, data=QInput(question="What is e?"))
print(result.answer)
Related Pages
- PydanticPrompt_Class - Parent class providing core generation logic
- DynamicFewShotPrompt_Class - Dict-based dynamic few-shot variant
- BasePrompt_Class - Root of the prompt hierarchy