Prompt

class opik.Prompt(name: str, prompt: str, metadata: Dict[str, Any] | None = None, type: PromptType = prompt_types.PromptType.MUSTACHE, validate_placeholders: bool = True, id: str | None = None, description: str | None = None, change_description: str | None = None, tags: List[str] | None = None, project_name: str | None = None)

Bases: BasePrompt

Prompt class represents a prompt with a name, prompt text/template and commit hash.

__init__(name: str, prompt: str, metadata: Dict[str, Any] | None = None, type: PromptType = prompt_types.PromptType.MUSTACHE, validate_placeholders: bool = True, id: str | None = None, description: str | None = None, change_description: str | None = None, tags: List[str] | None = None, project_name: str | None = None) None

Initializes a new instance of the class with the given parameters. Creates a new text prompt using the opik client and sets the initial state of the instance attributes based on the created prompt.

Parameters:
  • name – The name for the prompt.

  • prompt – The template for the prompt.

  • metadata – Optional metadata for the prompt.

  • type – The template type (MUSTACHE or JINJA2).

  • validate_placeholders – Whether to validate template placeholders.

  • id – Optional unique identifier (UUID) for the prompt.

  • description – Optional description of the prompt (up to 255 characters).

  • change_description – Optional description of changes in this version.

  • tags – Optional list of tags to associate with the prompt.

  • project_name – Optional project name for the prompt.

Raises:

PromptTemplateStructureMismatch – If a chat prompt with the same name already exists (template structure is immutable).

property synced: bool

Whether the prompt has been successfully synced with the backend.

sync_with_backend() bool

Synchronize the prompt with the backend.

Creates or updates the prompt on the Opik server. If the sync fails, a warning is logged and the prompt continues to work locally.

Returns:

True if the sync succeeded, False otherwise.

property name: str

The name of the prompt.

property prompt: str

The latest template of the prompt.

property commit: str | None

Legacy commit hash of the prompt version.

DEPRECATED — use version (e.g. "v3") instead. commit is no longer surfaced in the Opik UI and is kept only for backwards compatibility with older SDK callers.

property version: str | None

The sequential version selector for the prompt (e.g. "v3").

property version_id: str | None

The unique identifier of the prompt version.

property metadata: Dict[str, Any] | None

The metadata dictionary associated with the prompt

property type: PromptType

The prompt type of the prompt.

property id: str | None

The unique identifier (UUID) of the prompt.

property description: str | None

The description of the prompt.

property change_description: str | None

The description of changes in this version.

property tags: List[str] | None

The list of tags associated with the prompt.

property project_name: str | None

The name of the project this prompt belongs to.

property environments: List[str] | None

The environments that currently own this prompt version, or None if unowned.

format(**kwargs: Any) str | List[Dict[str, Any]]

Replaces placeholders in the template with provided keyword arguments.

Parameters:

**kwargs – Arbitrary keyword arguments where the key represents the placeholder in the template and the value is the value to replace the placeholder with.

Returns:

A string with all placeholders replaced by their corresponding values from kwargs.

classmethod from_fern_prompt_version(name: str, prompt_version: PromptVersionDetail, project_name: str | None = None) Prompt