📍 Join the 1st DeepEval meetup in SF, this Friday 5pm PT. RSVP on Luma →
💥 Introducing JevEval: Jev-as-a-Judge for LLM evaluation. Read the post →
Concepts

Prompts

deepeval lets you evaluate prompts by associating them with test runs. A Prompt in deepeval contains the prompt template and model parameters used for generation. By linking a Prompt to a test run, you can attribute metric scores to specific prompts, enabling metrics-driven prompt selection and optimization for your LLM application.

Quick summary

You can evaluate prompts end-to-end. End-to-end testing is useful when you want to evaluate the prompt's impact on the entire LLM application, since metric scores in end-to-end tests are calculated on the final output.

Evaluating Prompts

End-to-End

You can evaluate prompts end-to-end by running the evaluate function in Python or assert_test in CI/CD pipelines.

To evaluate a prompt during end-to-end evaluation, pass your test cases and metrics to the evaluate function, and include the prompt object in the hyperparameters dictionary with any string key.

main.py
from somewhere import your_llm_app
from deepeval.metrics import AnswerRelevancyMetric
from deepeval.prompt import Prompt, PromptMessage
from deepeval.test_case import LLMTestCase
from deepeval import evaluate

prompt = Prompt(
    alias="First Prompt",
    messages_template=[PromptMessage(role="system", content="You are a helpful assistant.")]
)

input = "What is the capital of France?"
actual_output = your_llm_app(input, prompt.messages_template)

evaluate(
    test_cases=[LLMTestCase(input=input, actual_output=actual_output)],
    metrics=[AnswerRelevancyMetric()],
    hyperparameters={"prompt": prompt}
)

To evaluate a prompt during end-to-end evaluation in CI/CD pipelines, use the assert_test function with your test cases and metrics, and include the prompt object in the hyperparameters dictionary.

main.py
import pytest

from somewhere import your_llm_app
from deepeval.metrics import AnswerRelevancyMetric
from deepeval.prompt import Prompt, PromptMessage
from deepeval.test_case import LLMTestCase
from deepeval import assert_test

prompt = Prompt(
    alias="First Prompt",
    messages_template=[PromptMessage(role="system", content="You are a helpful assistant.")]
)

def test_llm_app():
    input = "What is the capital of France?"
    actual_output = your_llm_app(input, prompt.messages_template)
    test_case = LLMTestCase(input=input, actual_output=actual_output)
    assert_test(test_case=test_case, metrics=[AnswerRelevancyMetric()])

@deepeval.log_hyperparameters()
def hyperparameters():
    return {"prompt": prompt}
✅ If successful, you should see a confirmation log like the one below in your CLI.
✓ Prompts Logged

╭─ Message Prompt (v00.00.20) ──────────────────────────────╮
│                                                           │
│  type: messages                                           │
│  output_type: OutputType.SCHEMA                           │
│  interpolation_type: PromptInterpolationType.FSTRING      │
│                                                           │
│  Model Settings:                                          │
│    – provider: OPEN_AI                                    │
│    – name: gpt-4o                                         │
│    – temperature: 0.7                                     │
│    – max_tokens: None                                     │
│    – top_p: None                                          │
│    – frequency_penalty: None                              │
│    – presence_penalty: None                               │
│    – stop_sequence: None                                  │
│    – reasoning_effort: None                               │
│    – verbosity: LOW                                       │
│                                                           │
╰───────────────────────────────────────────────────────────╯

Based on the metric scores, you can iterate on different prompts to identify the highest-performing version and optimize your LLM application accordingly.

Arena

You can also evaluate prompts side-by-side using ArenaGEval to pick the best-performing prompt for your given criteria. Simply include the prompts in the hyperparameters field of each Contestant.

main.py
from deepeval.test_case import ArenaTestCase, LLMTestCase, SingleTurnParams, Contestant
from deepeval.metrics import ArenaGEval
from deepeval.prompt import Prompt
from deepeval import compare

prompt_1 = Prompt(alias="First Prompt", text_template="You are a helpful assistant.")
prompt_2 = Prompt(alias="Second Prompt", text_template="You are a helpful assistant.")

test_case = ArenaTestCase(
    contestants=[
        Contestant(
            name="Version 1",
            hyperparameters={"prompt": prompt_1},
            test_case=LLMTestCase(input='Who wrote the novel "1984"?', actual_output="George Orwell"),
        ),
        Contestant(
            name="Version 2",
            hyperparameters={"prompt": prompt_2},
            test_case=LLMTestCase(input='Who wrote the novel "1984"?', actual_output='"1984" was written by George Orwell.'),
        ),
    ]
)

arena_geval = ArenaGEval(
    name="Friendly",
    criteria="Choose the winner of the more friendly contestant based on the input and actual output",
    evaluation_params=[
        SingleTurnParams.INPUT,
        SingleTurnParams.ACTUAL_OUTPUT,
    ]
)

compare(test_cases=[test_case], metric=arena_geval)

Creating Prompts

Loading Prompts

main.py
from deepeval.prompt import Prompt

prompt = Prompt(alias="First Prompt")
prompt.pull(version="00.00.01")

When loading prompts from .json files, the file name is automatically taken as the alias, if unspecified.

main.py
from deepeval.prompt import Prompt

prompt = Prompt()
prompt.load(file_path="example.json")
Click to see example.json
example.json
{
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant."
    }
  ]
}

When loading prompts from .txt files, the file name is automatically taken as the alias, if unspecified.

main.py
from deepeval.prompt import Prompt

prompt = Prompt()
prompt.load(file_path="example.txt")
Click to see example.txt
example.txt
You are a helpful assistant.

From Scratch

You can create a prompt in code by instantiating a Prompt object with an alias. Supply either a list of messages for a message-based prompt, or a text string for a text-based prompt.

main.py
from deepeval.prompt import Prompt, PromptMessage

prompt = Prompt(
    alias="First Prompt",
    messages_template=[PromptMessage(role="system", content="You are helpful assistant.")]
)
main.py
from deepeval.prompt import Prompt

prompt = Prompt(
    alias="First Prompt",
    text_template="You are helpful assistant."
)

Additional Attributes

In addition to prompt templates, you can associate model and output settings with a Prompt.

Model Settings

Model settings include the model provider and name, as well as generation parameters such as temperature:

main.py
from deepeval.prompt import Prompt, ModelSettings, ModelProvider

model_settings=ModelSettings(
    provider=ModelProvider.OPEN_AI,
    name="gpt-3.5-turbo",
    max_tokens=100,
    temperature=0.7
)
prompt = Prompt(..., model_settings=model_settings)

You can configure the following nine model settings for a prompt:

  • provider: An ModelProvider enum specifying the model provider to use for generation.
  • name: The string specifying the model name to use for generation.
  • temperature: A float between 0.0 and 2.0 specifying the randomness of the generated response.
  • top_p: A float between 0.0 and 1.0 specifying the nucleus sampling parameter.
  • frequency_penalty: A float between -2.0 and 2.0 specifying the frequency penalty.
  • presence_penalty: A float between -2.0 and 2.0 specifying the presence penalty.
  • max_tokens: An integer specifying the maximum number of tokens to generate.
  • verbosity: A Verbosity enum specifying the response detail level.
  • reasoning_effort: An ReasoningEffort enum specifying the thinking depth for reasoning models.
  • stop_sequences: A list of strings specifying custom stop tokens.

Output Settings

The output settings include the output type and optionally the output schema, if the output type is OutputType.SCHEMA.

main.py
from deepeval.prompt import OutputType
from pydantic import BaseModel
...

class Output(BaseModel):
    name: str
    age: int
    city: str

prompt = Prompt(..., output_type=OutputType.SCHEMA, output_schema=Output)

There are TWO output settings you can associate with a prompt:

  • output_type: The string specifying the model to use for generation.
  • output_schema: The schema of type BaseModel of the output, if output_type is OutputType.SCHEMA.

Tools

The tools in a prompt are used to specify the tools your agent has access to, all tools are identified using their name and hence must be unique.

from deepeval.prompt import Prompt, Tool
from deepeval.prompt.api import ToolMode
from pydantic import BaseModel

class ToolInputSchema(BaseModel):
    result: str
    confidence: float

prompt = Prompt(alias="YOUR-PROMPT-ALIAS")
tool = Tool(
    name="ExploreTool",
    description="Tool used for browsing the internet",
    mode=ToolMode.STRICT,
    structured_schema=ToolInputSchema,
)

prompt.push(
    text="This is a prompt with a tool",
    tools=[tool]
)

# You can also update an existing tool by using the new tool in the push / update method:
tool2 = Tool(
    name="ExploreTool", # Must have the same name to update a tool
    description="Tool used for browsing the internet",
    mode=ToolMode.ALLOW_ADDITIONAL,
    structured_schema=ToolInputSchema,
)

prompt.update(
    tools=[tool2]
)

FAQs

How do I attribute metric scores to a specific prompt?
Pass your Prompt object into the hyperparameters dictionary of evaluate() for end-to-end evals. deepeval then ties each test run's scores to that prompt so you can pick the best one.
Can I evaluate multiple prompts at once?
Yes. You can log multiple prompts under different keys, e.g. hyperparameters={"prompt_1": prompt_1, "prompt_2": prompt_2}, which is common when your app chains several prompts.
Where can prompts be loaded from?
You can build a Prompt from scratch in code, load one from a local .json or .txt file, or pull() a specific version from Confident AI. You must call load() or pull() before using the prompt in an eval.
Can a prompt store more than just the template text?
Yes. A Prompt can carry model settings (like temperature and model name), output settings, and a list of Tools your agent has access to, so the full generation configuration is versioned alongside the template.

On this page