Prompts
The Opik TypeScript SDK provides comprehensive prompt management functionality for versioning, storing, and formatting your LLM prompt templates. Prompts in Opik are versioned automatically, allowing you to track changes over time while seamlessly integrating with your codebase.
Introduction
Opik supports two types of prompts:
- Text Prompts: Simple string templates for single-turn interactions
- Chat Prompts: Structured message-based templates for conversational AI with support for multimodal content (text, images, videos)
Each prompt:
- Has a unique name and auto-generated versions
- Supports Mustache or Jinja2 template syntax
- Tracks metadata, tags, and change descriptions
- Maintains complete version history
- Enables version comparison and rollback
Getting Started
This section covers text prompts. For chat prompts with structured messages, see the Chat Prompts section.
Creating Your First Prompt
Create a text prompt with the createPrompt method:
Retrieving Prompts
Get prompts by name or specific version:
Formatting Prompts
Opik supports two powerful template engines:
- Mustache - Simple, logic-less templates with
{{variable}}placeholders (default) - Jinja2 - Advanced templating with control flow using
{% %}blocks and{{ }}variables
The format() method works on both Prompt and PromptVersion instances:
Template Syntax Examples:
Core Operations
Creating and Updating
Understanding Version Creation
The createPrompt method intelligently handles versioning based on content changes:
New version is created when:
- Template content (
prompt) changes - Metadata changes (deep equality check)
- Template type (
type) changes
No new version (returns existing) when:
- Template, metadata, and type are all identical
- Only
tagsordescriptiondiffer
Version creation is triggered by changes to template content,
metadata, or type. Changes to tags, name or description alone do
not create new versions - use updateProperties() for those.
Updating Prompt Properties
Update prompt metadata without creating new versions:
Updating properties (name, description, tags) does not create a new
version. Only changes to the template content, metadata, or type
trigger version creation.
Retrieving and Searching
Searching with Opik Query Language
Search and filter prompts using Opik Query Language (OQL) - a powerful SQL-like syntax for finding exactly the prompts you need.
Supported Fields:
Search Examples:
Deleting Prompts
Version Management
Understanding Versions
Version Metadata and Properties
Access comprehensive version information:
Viewing Version History
Working with Versions
Getting Specific Versions
Comparing Versions
Restoring Previous Versions
Advanced Usage
Integration with Tracing
Prompts work seamlessly with Opik’s tracing functionality:
Best Practices
Store Prompts in Code
Keep your prompts versioned alongside your code:
Use Meaningful Version Descriptions
Tag Your Prompts
Chat Prompts
Chat prompts are structured message-based templates designed for conversational AI applications. They support multiple message roles (system, user, assistant) and multimodal content including text, images, and videos.
Key Features
- Structured Messages: Organize prompts as a list of messages with roles (system, user, assistant)
- Multimodal Support: Include images, videos, and text in the same prompt
- Variable Substitution: Use Mustache (
{{variable}}) or Jinja2 syntax - Version Control: Automatic versioning when messages change
- Template Validation: Optional validation of template placeholders
Creating Chat Prompts
Create chat prompts using the createChatPrompt method:
Formatting Chat Prompts
Format chat prompts with variables to get ready-to-use message arrays:
Multi-Turn Conversations
Create templates for multi-turn conversations:
Multimodal Chat Prompts
Chat prompts support multimodal content for vision-enabled models:
Image Analysis
Video Analysis
Mixed Content
When formatting multimodal prompts, you can specify supportedModalities to
control how content is rendered:
- If a modality is supported (e.g.,
{ vision: true }), the structured content is preserved - If a modality is not supported, it’s replaced with text placeholders (e.g.,
<<<image>>><<</image>>>)
This allows you to use the same prompt template with different models that may or may not support certain modalities.
Retrieving Chat Prompts
Get chat prompts by name or specific version:
Searching Chat Prompts
Search for chat prompts specifically using the template_structure filter:
Without the template_structure filter, searchPrompts returns both text and chat prompts.
Template Types for Chat Prompts
Chat prompts support two template types:
Mustache (Default)
Jinja2
Jinja2 templates support advanced features like conditionals, loops, and filters, making them more powerful for complex prompt logic. However, Mustache templates are simpler and more portable.
Chat Prompt Versioning
Chat prompts are automatically versioned when the messages change:
Version Management for Chat Prompts
Chat prompts support the same version management features as text prompts:
Updating Chat Prompt Properties
Update chat prompt metadata without creating new versions:
Updating properties (name, description, tags) does not create a new
version. Only changes to the messages, metadata, or type trigger
version creation.
API Reference
OpikClient Methods
createPrompt(options)
Creates a new prompt or returns existing version if content unchanged.
Arguments:
options.name: string- Prompt name (required)options.prompt: string- Template content (required)options.type?: PromptType- Template engine (default:MUSTACHE)options.promptId?: string- Optional prompt IDoptions.description?: string- Optional descriptionoptions.metadata?: JsonNode- Optional metadataoptions.changeDescription?: string- Version change descriptionoptions.tags?: string[]- Optional tags
Returns: Promise<Prompt> - Created or existing prompt
getPrompt(options)
Retrieves a prompt by name and optional version.
Arguments:
options.name: string- Prompt name (required)options.commit?: string- Optional commit ID for specific version
Returns: Promise<Prompt | null> - Prompt instance or null if not found
searchPrompts(filterString?)
Searches prompts with optional OQL filtering.
Arguments:
filterString?: string- Optional OQL filter expression
Returns: Promise<Prompt[]> - Array of matching prompts
Supported OQL fields:
id,name,created_by: String fieldstags: List field (use “contains” operator)
Operators: =, !=, contains, not_contains, starts_with, ends_with, >, <
deletePrompts(ids)
Deletes multiple prompts and all their versions.
Arguments:
ids: string[]- Array of prompt IDs to delete
Returns: Promise<void>
updatePromptVersionTags(versionIds, options?)
Updates tags for one or more prompt versions in a single batch operation.
Arguments:
versionIds: string[]- Array of prompt version IDs to updateoptions.tags?: string[] | null- Tags to set or merge:[]– clears all tags (whenmergeTagsis false or unspecified)['tag1', 'tag2']– sets or merges tags (based onmergeTags)null/ omitted – preserves existing tags unchanged
options.mergeTags?: boolean– Iftrue, adds new tags to existing tags (union). Iffalse(default), replaces all existing tags.
Returns: Promise<void>
Examples:
createChatPrompt(options)
Creates a new chat prompt or returns existing version if content unchanged.
Arguments:
options.name: string- Chat prompt name (required)options.messages: ChatMessage[]- Array of chat messages with roles and content (required)options.type?: PromptType- Template engine (default:MUSTACHE)options.promptId?: string- Optional prompt IDoptions.description?: string- Optional descriptionoptions.metadata?: JsonNode- Optional metadataoptions.changeDescription?: string- Version change descriptionoptions.tags?: string[]- Optional tags
Returns: Promise<ChatPrompt> - Created or existing chat prompt
ChatMessage Format:
getChatPrompt(options)
Retrieves a chat prompt by name and optional version.
Arguments:
options.name: string- Chat prompt name (required)options.commit?: string- Optional commit ID for specific version
Returns: Promise<ChatPrompt | null> - ChatPrompt instance or null if not found
Prompt Class
Methods
format(variables)
Formats the prompt template with provided variables.
Arguments:
variables: Record<string, unknown>- Variables to substitute
Returns: string - Formatted prompt text
Throws: PromptValidationError if required variables missing (Mustache only)
getVersions(options?)
Retrieves all version history for this prompt, with optional filtering, sorting, and search.
Arguments:
options.search?: string- Free-text search against template content and change descriptionoptions.filters?: string- JSON-encoded filter array. Each entry is{ field, operator, value }.options.sorting?: string- JSON-encoded sort array. Each entry is{ field, direction }wheredirectionis"ASC"or"DESC".
Supported filter fields:
Returns: Promise<PromptVersion[]> - Array of all matching versions (newest first by default)
Examples:
getVersion(commit)
Gets a specific version as a Prompt instance.
Arguments:
commit: string- Commit hash (8-char or full)
Returns: Promise<Prompt | null> - Prompt instance or null if not found
useVersion(version)
Restores a specific version by creating a new version with old content.
Arguments:
version: PromptVersion- Version object to restore
Returns: Promise<Prompt> - New prompt instance with restored content
updateProperties(updates)
Updates prompt properties without creating new version.
Arguments:
updates.name?: string- New prompt nameupdates.description?: string- New descriptionupdates.tags?: string[]- New tags array
Returns: Promise<this> - This prompt instance (for chaining)
delete()
Deletes this prompt and all its versions.
Returns: Promise<void>
Properties
id: string- Unique prompt identifiername: string- Prompt nameprompt: string- Current template contentcommit?: string- Current version commit IDtype: PromptType- Template engine typedescription?: string- Prompt descriptiontags?: readonly string[]- Prompt tagsmetadata?: JsonNode- Prompt metadatachangeDescription?: string- Latest version change description
PromptVersion Class
Methods
format(variables)
Formats this version’s template with provided variables.
Arguments:
variables: Record<string, unknown>- Variables to substitute
Returns: string - Formatted prompt text
getVersionInfo()
Gets formatted version information string.
Returns: string - Format: [commit] YYYY-MM-DD by user@email.com - Change description
getVersionAge()
Gets human-readable version age.
Returns: string - Format: “2 days ago”, “Today”, etc.
compareTo(other)
Compares this version’s template with another version.
Arguments:
other: PromptVersion- Version to compare against
Returns: string - Git-style unified diff showing changes
Properties
id: string- Version unique identifiername: string- Associated prompt nameprompt: string- Template content for this versioncommit: string- Version commit IDtype: PromptType- Template engine typetags?: string[]- Tags associated with this versionmetadata?: JsonNode- Version metadatachangeDescription?: string- Version change descriptioncreatedAt?: Date- Creation timestampcreatedBy?: string- Creator identifier
ChatPrompt Class
The ChatPrompt class extends BasePrompt and provides chat-specific functionality for managing structured message-based prompts.
Methods
format(variables, supportedModalities?)
Formats the chat prompt messages with provided variables.
Arguments:
variables: Record<string, unknown>- Variables to substitute in message contentsupportedModalities?: SupportedModalities- Optional modality support configuration
SupportedModalities Format:
Returns: ChatMessage[] - Array of formatted chat messages
Throws: PromptValidationError if required variables missing (Mustache only)
Example:
getVersions(options?)
Retrieves all version history for this chat prompt, with optional filtering, sorting, and search. Accepts the same options as the Prompt.getVersions() method.
Arguments:
options.search?: string- Search text to match against template content and change descriptionoptions.sorting?: string- Sort expressionoptions.filters?: string- JSON-encoded filter array (same format asPrompt.getVersions)
Returns: Promise<PromptVersion[]> - Array of all matching versions (newest first)
getVersion(commit)
Gets a specific version as a ChatPrompt instance.
Arguments:
commit: string- Commit hash (8-char or full)
Returns: Promise<ChatPrompt | null> - ChatPrompt instance or null if not found
useVersion(version)
Restores a specific version by creating a new version with old content.
Arguments:
version: PromptVersion- Version object to restore
Returns: Promise<ChatPrompt> - New chat prompt instance with restored content
updateProperties(updates)
Updates chat prompt properties without creating new version.
Arguments:
updates.name?: string- New prompt nameupdates.description?: string- New descriptionupdates.tags?: string[]- New tags array
Returns: Promise<this> - This chat prompt instance (for chaining)
delete()
Deletes this chat prompt and all its versions.
Returns: Promise<void>
Properties
id: string- Unique chat prompt identifiername: string- Chat prompt namemessages: ChatMessage[]- Array of chat messages with roles and contentcommit?: string- Current version commit IDtype: PromptType- Template engine typedescription?: string- Chat prompt descriptiontags?: readonly string[]- Chat prompt tagsmetadata?: JsonNode- Chat prompt metadatachangeDescription?: string- Latest version change description