In Opik 2.0, prompts are project-scoped. Make sure to specify a projectName when creating prompts so they are associated with the correct project.
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.
Opik supports two types of prompts:
Each prompt:
This section covers text prompts. For chat prompts with structured messages, see the Chat Prompts section.
Create a text prompt with the createPrompt method:
Get prompts by name or specific version:
Opik supports two powerful template engines:
{{variable}} placeholders (default){% %} blocks and {{ }} variablesThe format() method works on both Prompt and PromptVersion instances:
Template Syntax Examples:
The createPrompt method intelligently handles versioning based on content changes:
New version is created when:
prompt) changestype) changesNo new version (returns existing) when:
tags or description differVersion 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.
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.
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:
Access comprehensive version information:
Prompts work seamlessly with Opik’s tracing functionality:
Keep your prompts versioned alongside your code:
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.
{{variable}}) or Jinja2 syntaxCreate chat prompts using the createChatPrompt method:
Format chat prompts with variables to get ready-to-use message arrays:
Create templates for multi-turn conversations:
Chat prompts support multimodal content for vision-enabled models:
When formatting multimodal prompts, you can specify supportedModalities to
control how content is rendered:
{ vision: true }), the structured content is preserved<<<image>>><<</image>>>)This allows you to use the same prompt template with different models that may or may not support certain modalities.
Get chat prompts by name or specific version:
Search for chat prompts specifically using the template_structure filter:
Without the template_structure filter, searchPrompts returns both text and chat prompts.
Chat prompts support two template types:
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 prompts are automatically versioned when the messages change:
Chat prompts support the same version management features as text prompts:
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.
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 tagsReturns: Promise<Prompt> - Created or existing prompt
getPrompt(options)Retrieves a prompt by name, optionally targeting a specific version.
Arguments:
options.name: string - Prompt name (required)options.version?: string - Sequential version identifier (e.g. "v3"). If omitted, the latest version is returned.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 expressionReturns: 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 deleteReturns: 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 (when mergeTags is false or unspecified)['tag1', 'tag2'] – sets or merges tags (based on mergeTags)null / omitted – preserves existing tags unchangedoptions.mergeTags?: boolean – If true, adds new tags to existing tags (union). If false (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 tagsReturns: Promise<ChatPrompt> - Created or existing chat prompt
ChatMessage Format:
getChatPrompt(options)Retrieves a chat prompt by name, optionally targeting a specific version.
Arguments:
options.name: string - Chat prompt name (required)options.version?: string - Sequential version identifier (e.g. "v3"). If omitted, the latest version is returned.Returns: Promise<ChatPrompt | null> - ChatPrompt instance or null if not found
format(variables)Formats the prompt template with provided variables.
Arguments:
variables: Record<string, unknown> - Variables to substituteReturns: 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 } where direction is "ASC" or "DESC".Supported filter fields:
Returns: Promise<PromptVersion[]> - Array of all matching versions (newest first by default)
Examples:
getVersion(version)Returns a Prompt instance pinned to a specific version of this prompt. Accepts either the sequential version identifier (e.g. "v3") — preferred — or a commit hash (deprecated; retained for backwards compatibility). Inputs matching /^v\d+$/ are treated as version numbers; anything else is treated as a commit.
Arguments:
version: string - Sequential version ("v<N>") or commit hash (deprecated)Returns: Promise<Prompt | null> - Prompt instance pinned to that version, 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 restoreReturns: 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 arrayReturns: Promise<this> - This prompt instance (for chaining)
delete()Deletes this prompt and all its versions.
Returns: Promise<void>
id: string - Unique prompt identifiername: string - Prompt nameprompt: string - Current template contentversion?: string - Sequential version identifier (e.g. "v3")type: PromptType - Template engine typedescription?: string - Prompt descriptiontags?: readonly string[] - Prompt tagsmetadata?: JsonNode - Prompt metadatachangeDescription?: string - Latest version change descriptionformat(variables)Formats this version’s template with provided variables.
Arguments:
variables: Record<string, unknown> - Variables to substituteReturns: string - Formatted prompt text
getVersionInfo()Gets a formatted version-info string suitable for display. Includes the version identifier (e.g. "v3"), creation date, creator, and change description.
Returns: string — Format: "[v3] 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. The diff is logged to the terminal and returned. Labels in the diff use the sequential version identifier (e.g. "[v3]").
Arguments:
other: PromptVersion - Version to compare againstReturns: string - Git-style unified diff showing changes
id: string - Version unique identifiername: string - Associated prompt nameprompt: string - Template content for this versionversion?: string - Sequential version identifier (e.g. "v3")type: PromptType - Template engine typetags?: string[] - Tags associated with this versionmetadata?: JsonNode - Version metadatachangeDescription?: string - Version change descriptioncreatedAt?: Date - Creation timestampcreatedBy?: string - Creator identifierThe ChatPrompt class extends BasePrompt and provides chat-specific functionality for managing structured message-based prompts.
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 configurationSupportedModalities 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 as Prompt.getVersions)Returns: Promise<PromptVersion[]> - Array of all matching versions (newest first)
getVersion(version)Returns a ChatPrompt instance pinned to a specific version of this chat prompt. Accepts either the sequential version identifier (e.g. "v3") — preferred — or a commit hash (deprecated; retained for backwards compatibility). Inputs matching /^v\d+$/ are treated as version numbers; anything else is treated as a commit.
Arguments:
version: string - Sequential version ("v<N>") or commit hash (deprecated)Returns: Promise<ChatPrompt | null> - ChatPrompt instance pinned to that version, 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 restoreReturns: 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 arrayReturns: Promise<this> - This chat prompt instance (for chaining)
delete()Deletes this chat prompt and all its versions.
Returns: Promise<void>
id: string - Unique chat prompt identifiername: string - Chat prompt namemessages: ChatMessage[] - Array of chat messages with roles and contentversion?: string - Sequential version identifier (e.g. "v3")type: PromptType - Template engine typedescription?: string - Chat prompt descriptiontags?: readonly string[] - Chat prompt tagsmetadata?: JsonNode - Chat prompt metadatachangeDescription?: string - Latest version change description