Using prompts in chat completions
The Prompt Registry lets you store, version, and manage prompts centrally in Studio. This guide shows how to retrieve a stored prompt and use it as the system message in a chat completion.
Basic usage
Fetch the prompt by name or ID, extract its content, and pass it as the system message.
import os
from mistralai.client import Mistral
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
prompt = client.beta.prompts.get(prompt_id="coding-assistant")
system_content = prompt.definition.content
response = client.chat.complete(
model="mistral-large-latest",
messages=[
{"role": "system", "content": system_content},
{"role": "user", "content": "Help me debug this function."},
],
)
print(response.choices[0].message.content)Template variables
Prompts can contain {{variable}} placeholders. Replace them before passing the content to the completion.
For example, a prompt stored as:
You are a coding assistant. You specialise in {{language}}.Can be used like this:
prompt = client.beta.prompts.get(prompt_id="coding-assistant")
system_content = prompt.definition.content.replace("{{language}}", "Python")Pinning a version
By default, get returns the latest version of a prompt. Pin a specific version or alias to prevent production behavior from changing when a prompt is updated.
# Pin by version number
prompt = client.beta.prompts.get(prompt_id="coding-assistant", version=3)
# Pin by alias
prompt = client.beta.prompts.get(prompt_id="coding-assistant", alias="production")Use named aliases like production or staging to decouple your code from version numbers. You can update which version an alias points to from Studio without changing your application.