Launch Week 02 wrapped — explore all five launches

Pull Prompts

Learn how to test and use prompts in your LLM app

Overview

You can pull a prompt version from Confident AI like how you would pull a dataset. It works by:

  • Providing Confident AI with the alias and optionally version of the prompt you wish to retrieve
  • Confident AI will provide the non-interpolated version of the prompt
  • You will then interpolate the variables in code

You should pull prompts once and save it in memory instead of pulling it everytime you need to use it.

Pull Prompt By Version

  1. Pull prompt with alias

    Pull your prompt version by providing the alias you've defined:

    from deepeval.prompt import Prompt
    
    prompt = Prompt(alias="YOUR-PROMPT-ALIAS")
    prompt.pull(version="latest")
  2. Interpolate variables

    Now that you have your prompt template, interpolate any dynamic variables you may have defined in your prompt version.

    interpolated_prompt = prompt.interpolate(name="Joe")

    For example, if this is your prompt version:

    {
      "role": "system",
      "content": "You are a helpful assistant called {{ name }}. Speak normally like a human."
    }

    And your interpolation type is {{ variable }}, interpolating the name (e.g. "Joe") would give you this prompt that is ready for use:

    {
      "role": "system",
      "content": "You are a helpful assistant called Joe. Speak normally like a human."
    }

    And if you don't have any variables, you must still use the interpolate() method to create a copy of your prompt template to be used in your LLM application.

  3. Use interpolated prompt

    By now you should have an interpolated prompt version, for example:

    {
      "role": "system",
      "content": "You are a helpful assistant called Joe. Speak normally like a human."
    }

    Which you can use to generate text from your LLM provider of choice. Here are some examples with OpenAI:

    main.py
    from deepeval.prompt import Prompt
    from openai import OpenAI
    
    prompt = Prompt(alias="YOUR-PROMPT-ALIAS")
    prompt.pull()
    interpolated_prompt = prompt.interpolate() # interpolate prompt
    
    response = OpenAI().chat.completions.create(
        model="gpt-4o-mini",
        messages=interpolated_prompt
    )
    
    print(response.choices[0].message.content)

You can also fetch all the versions associated with a prompt as shown below:

from deepeval.prompt import Prompt

prompt = Prompt(alias="YOUR-PROMPT-ALIAS")
commits = prompt._get_versions()

Pull Prompts By Label

You can also pull specific versions of prompts using the label you assign to the versions on the platform.

from deepeval.prompt import Prompt

prompt = Prompt(alias="YOUR-PROMPT-ALIAS")
prompt.pull(label="staging")

You must manually label each prompt version before pulling it. Click here to learn how to do so.

Pull Prompts By Commit

You can also pull specific snapshots of prompts using it's alias and the commit hash.

from deepeval.prompt import Prompt

prompt = Prompt(alias="YOUR-PROMPT-ALIAS")
prompt.pull()

You can also fetch all the commits associated with a prompt as shown below:

from deepeval.prompt import Prompt

prompt = Prompt(alias="YOUR-PROMPT-ALIAS")
commits = prompt._get_commits()

Pull Prompts By Branch

You can also pull specific snapshots of prompts using it's alias and the branch name.

from deepeval.prompt import Prompt

prompt = Prompt(alias="YOUR-PROMPT-ALIAS")
prompt.pull(branch="MY-BRANCH-NAME")

Branch Operations

You can also perform branch operations such as creating a new branch, updating a branch name, deleting a branch and listing all branches.

from deepeval.prompt import Prompt

prompt = Prompt(alias="YOUR-PROMPT-ALIAS")
prompt.get_branches()
import { Prompt } from "deepeval";

const prompt = new Prompt({ alias: "YOUR-PROMPT-ALIAS" });
await prompt.getBranches();
  curl https://api.confident-ai.com/v1/prompts/PROMPT-ALIAS/branches \
    -H "CONFIDENT_API_KEY: <PROJECT-API-KEY>"

How Are Prompts Pulled?

Confident AI automatically caches prompts on the client side to minimize API call latency and ensure prompt availability, which is especially useful in production environments.

sequenceDiagram
    participant App as Your Application
    participant DeepEval as DeepEval
    participant Cache as DeepEval Cache
    participant API as Confident AI API

    App->>DeepEval: Pull Prompt
    DeepEval->>Cache: Check cache
    Cache-->>DeepEval: Cached Prompt Data
    DeepEval-->>App: Prompt Data
    Note over Cache,API: Refetching (every 60s)
    Cache->>API: GET /v1/prompts
    API-->>Cache: Update Prompt Cache

Customize refresh rate

By default, the cache is refetched every 60 seconds, where DeepEval will automatically update the cached prompt with the up-to-date version from Confident AI. This can be overridden by setting the refresh parameter to a different value. Fetching is done asynchronously, so it will not block your application.

main.py
from deepeval.prompt import Prompt

prompt = Prompt(alias="YOUR-PROMPT-ALIAS")
prompt.pull(refresh=60)
interpolated_prompt = prompt.interpolate(name="Joe")

Configure cache

To disable caching, you can set refresh=0. This will force an API call every time you pull the prompt, which is particularly useful for development and testing.

main.py
from deepeval.prompt import Prompt

prompt = Prompt(alias="YOUR-PROMPT-ALIAS")
prompt.pull(refresh=0)
interpolated_prompt = prompt.interpolate(name="Joe")

Advanced Usage

As you learnt in earlier sections, prompts have the additional option to not just version text/messages but also model settings (provider, name, parameters), output type, and tools.

Using model settings

After pulling a prompt, you can access any model settings that were configured for the prompt version via the model_settings property. Model settings include the provider, model name, and model parameters (temperature, max_tokens, etc.).

main.py
from deepeval.prompt import Prompt
from openai import OpenAI

prompt = Prompt(alias="YOUR-PROMPT-ALIAS")
prompt.pull()
interpolated_prompt = prompt.interpolate()
settings = prompt.model_settings

# Use model settings (provider, name, parameters) in your OpenAI call
response = OpenAI().chat.completions.create(
    model=settings.name,
    messages=interpolated_prompt,
    temperature=settings.temperature,
    max_tokens=settings.max_tokens,
    top_p=settings.top_p,
    frequency_penalty=settings.frequency_penalty,
    presence_penalty=settings.presence_penalty,
    stop=settings.stop_sequence,
)

print(response.choices[0].message.content)

Using output type

After pulling a prompt, you can access the output configuration via the output_type and output_schema properties. This is useful when you want to enforce structured outputs from your LLM.

main.py
from deepeval.prompt import Prompt
from deepeval.prompt.api import OutputType
from openai import OpenAI

prompt = Prompt(alias="YOUR-PROMPT-ALIAS")
prompt.pull()
interpolated_prompt = prompt.interpolate()

# Build response_format based on output type
response_format = None
if prompt.output_type == OutputType.JSON:
    response_format = {"type": "json_object"}
elif prompt.output_type == OutputType.SCHEMA:
    response_format = {
        "type": "json_schema",
        "json_schema": prompt.output_schema
    }

# Use output type in your OpenAI call
response = OpenAI().chat.completions.create(
    model="gpt-4o",
    messages=interpolated_prompt,
    response_format=response_format,
)

print(response.choices[0].message.content)

The available output types are:

  • TEXT - Plain text output (default)
  • JSON - JSON formatted output (maps to {"type": "json_object"})
  • SCHEMA - Structured output validated against a schema (maps to {"type": "json_schema", ...})

Using tools

After pulling a prompt, you can access any tools that were defined in the prompt version via the tools property. Each tool contains:

  • name: The name of the tool
  • description: A description of what the tool does
  • input_schema: The JSON schema defining the tool's input parameters
  • mode: The tool mode (ALLOW_ADDITIONAL, NO_ADDITIONAL, or STRICT)
main.py
from deepeval.prompt import Prompt
from openai import OpenAI

prompt = Prompt(alias="YOUR-PROMPT-ALIAS")
prompt.pull()
interpolated_prompt = prompt.interpolate()

# Convert prompt tools to OpenAI format
openai_tools = [
    {
        "type": "function",
        "function": {
            "name": tool.name,
            "description": tool.description,
            "parameters": tool.input_schema,
            "strict": tool.mode == "STRICT",
        },
    }
    for tool in prompt.tools
]

# Use tools in your OpenAI call
response = OpenAI().chat.completions.create(
    model="gpt-4o",
    messages=interpolated_prompt,
    tools=openai_tools,
)

print(response.choices[0].message)

Using images

For prompts containing images, here's how you would parse it and pass it for use in your MLLM of choice:

The deepeval python SDK offers a utility method called convert_to_multi_modal_array. This method is useful for converting a string containing images in the [DEEPEVAL:IMAGE:url] format into a list of strings and MLLMImage items.

from deepeval.prompt import Prompt
from deepeval.utils import convert_to_multi_modal_array

prompt = Prompt(alias="YOUR-PROMPT-ALIAS")
prompt.pull()

multimodal_array = convert_to_multi_modal_array(prompt.text)

The multimodal_array here is a list containing strings and MLLMImages, you can loop over this list to construct a messages array with images to pass to your MLLM. Here's an example showing how to construct messages array for openai:

messages = []
for element in multimodal_array:
    if isinstance(element, str):
        messages.append({"type": "text", "text": element})
    elif isinstance(element, MLLMImage):
      if element.url:
          messages.append(
              {
                  "type": "image_url",
                  "image_url": {"url": element.url},
              }
          )

Prompt Association

You can and should definitely associate prompt versions with test runs and traced data for Confident AI to let you know which version of your prompt performs best.

Evals

You can associate a prompt with your evals to get detailed insights on how each prompt and their versions are performing. It works by:

  • Pulling prompt via the Evals API
  • Logging prompts as a hyperparameter during evaluation

Simply add the pulled prompt instance as a free-form key-value pair to the hyperparameters argument in the evaluate() function

evaluate(
    ...
    hyperparameters={
        "Model": "YOUR-MODEL",
        "Prompt": prompt,
    },
)

This will automatically attribute the prompt used during this test run, which will allow you get detailed insights in the Confident AI platform.

Tracing

Associating prompts with LLM traces and spans is a great way to determine which prompts performs best in production.

  1. Setup tracing

    Attach the @observe decorator to functions/methods that make up your agent, and specify type llm for your LLM-calling functions.

    main.py
    from deepeval.tracing import observe
    
    @observe(type="llm", model="gpt-4.1")
    def your_llm_component():
        ...
  2. Pull and interpolate prompt

    Pull and interpolate the prompt version to use it for LLM generation.

    main.py
    from deepeval.tracing import observe
    from deepeval.prompt import Prompt
    from openai import OpenAI
    
    @observe(type="llm", model="gpt-4.1")
    def your_llm_component():
        prompt = Prompt(alias="YOUR-PROMPT-ALIAS")
        prompt.pull()
        interpolated_prompt = prompt.interpolate(name="Joe")
        response = OpenAI().chat.completions.create(model="gpt-4o-mini", messages=interpolated_prompt)
        return response.choices[0].message.content
  3. Execute your function

    Then simply provide the prompt to the update_llm_span function.

    main.py
    from deepeval.tracing import observe, update_llm_span
    from deepeval.prompt import Prompt
    from openai import OpenAI
    
    @observe(type="llm", model="gpt-4.1")
    def your_llm_component():
        prompt = Prompt(alias="YOUR-PROMPT-ALIAS")
        prompt.pull()
        interpolated_prompt = prompt.interpolate(name="Joe")
        response = OpenAI().chat.completions.create(model="gpt-4o-mini", messages=interpolated_prompt)
        update_llm_span(prompt=prompt)
        return response.choices[0].message.content

    This will automatically attribute the prompt used to the LLM span.

Switching Projects

You can pull and manage your prompts in any project by configuring a CONFIDENT_API_KEY.

  • For default usage, set CONFIDENT_API_KEY as an environment variable.
  • To target a specific project, pass a confident_api_key directly when creating the Prompt object.
from deepeval.prompt import Prompt, PromptMessage

prompt = Prompt(
  alias="YOUR-PROMPT-ALIAS",
  confident_api_key="confident_us...",
)

When both are provided, the confident_api_key passed to Prompt always takes precedence over the environment variable.

Next Steps

Now that you can pull prompts into your app, learn how to push them programmatically or run evaluations.

Managing prompts in production?Prompt versioning, A/B testing, and rollbacks for production teamsBook a demo
Built byConfident AI