:::callout{intent="note"}
In Opik 2.0, datasets and experiments are project-scoped. Make sure to specify a `project_name` when creating datasets and running experiments so they are associated with the correct project.
:::

The Opik Agent Optimizer SDK provides a comprehensive set of tools for optimizing LLM prompts and agents. This reference guide documents the standardized API that all optimizers follow, ensuring consistency and interoperability across different optimization algorithms.

## Key Features

- **Standardized API**: All optimizers follow the same interface for `optimize_prompt()` methods
- **Multiple Algorithms**: Support for various optimization strategies including evolutionary, few-shot, meta-prompt, and GEPA
- **MCP Support**: Built-in support for Model Context Protocol tool calling and optimization
- **Consistent Results**: All optimizers return standardized `OptimizationResult` objects
- **Counter Tracking**: Built-in LLM and tool call counters for monitoring usage
- **Backward Compatibility**: All original parameters preserved through kwargs extraction
- **Deprecation Warnings**: Clear warnings for deprecated parameters with migration guidance

## Core Classes

The SDK provides several optimizer classes that all inherit from `BaseOptimizer` and implement the same standardized interface:

- **ParameterOptimizer**: Optimizes LLM call parameters (temperature, top\_p, etc.) using Bayesian optimization
- **FewShotBayesianOptimizer**: Uses few-shot learning with Bayesian optimization
- **MetaPromptOptimizer**: Employs meta-prompting techniques for optimization
- **EvolutionaryOptimizer**: Uses genetic algorithms for prompt evolution
- **GepaOptimizer**: Leverages GEPA (Genetic-Pareto) optimization approach
- **HRPO (Hierarchical Reflective Prompt Optimizer)**: Uses hierarchical root cause analysis for targeted prompt refinement

## Standardized Method Signatures

All optimizers implement these core methods with identical signatures:

### optimize\_prompt()

```python
def optimize_prompt(
    self,
    prompt: ChatPrompt | dict[str, ChatPrompt],
    dataset: Dataset,
    metric: MetricFunction,
    agent: OptimizableAgent | None = None,
    experiment_config: dict | None = None,
    n_samples: int | None = None,
    auto_continue: bool = False,
    project_name: str | None = None,
    optimization_id: str | None = None,
    validation_dataset: Dataset | None = None,
    max_trials: int = 10,
    allow_tool_use: bool = True,
    **kwargs: Any,
) -> OptimizationResult
```

## Deprecation Warnings

The following parameters are deprecated and will be removed in future versions:

### Constructor Parameters

- **`num_threads`** in optimizer constructors: Use `n_threads` instead

### Example Migration

```python
# ❌ Deprecated
optimizer = FewShotBayesianOptimizer(
    model="gpt-4o-mini",
    num_threads=16,  # Deprecated
)

# ✅ Correct
optimizer = FewShotBayesianOptimizer(
    model="gpt-4o-mini",
    n_threads=16,  # Use n_threads instead
)
```

## FewShotBayesianOptimizer

```python
FewShotBayesianOptimizer(
    model: str = 'openai/gpt-5-nano',
    model_parameters: dict[str, typing.Any] | None = None,
    min_examples: int = 2,
    max_examples: int = 8,
    n_threads: int = 12,
    verbose: int = 1,
    seed: int = 42,
    name: str | None = None,
    enable_columnar_selection: bool = True,
    enable_diversity: bool = True,
    enable_multivariate_tpe: bool = True,
    enable_optuna_pruning: bool = True,
    prompt_overrides: dict[str, str] | collections.abc.Callable[[opik_optimizer.utils.prompt_library.PromptLibrary], None] | None = None,
    skip_perfect_score: bool = True,
    perfect_score: float = 0.95
)
```

**Parameters:**

- `model` (str, default: openai/gpt-5-nano) —

- `model_parameters` (dict\[str, typing.Any] | None) —

- `min_examples` (int, default: 2) —

- `max_examples` (int, default: 8) —

- `n_threads` (int, default: 12) —

- `verbose` (int, default: 1) —

- `seed` (int, default: 42) —

- `name` (str | None) —

- `enable_columnar_selection` (bool, default: True) —

- `enable_diversity` (bool, default: True) —

- `enable_multivariate_tpe` (bool, default: True) —

- `enable_optuna_pruning` (bool, default: True) —

- `prompt_overrides` (dict\[str, str] | collections.abc.Callable\[\[opik\_optimizer.utils.prompt\_library.PromptLibrary], None] | None) —

- `skip_perfect_score` (bool, default: True) —

- `perfect_score` (float, default: 0.95) —

### Methods

#### begin\_round

```python
begin_round(
    context: OptimizationContext,
    extras: Any
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `extras` (Any) —

#### cleanup

```python
cleanup()
```

#### evaluate

```python
evaluate(
    context: OptimizationContext,
    prompts: dict,
    experiment_config: dict[str, typing.Any] | None = None,
    sampling_tag: str | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) — Optimization context for this run.

- `prompts` (dict) — Dict of named prompts to evaluate (e.g., {"main": ChatPrompt(...)}). Single-prompt optimizations use a dict with one entry.

- `experiment_config` (dict\[str, typing.Any] | None) — Optional experiment configuration.

- `sampling_tag` (str | None) — Optional sampling tag for deterministic subsampling per candidate.

#### evaluate\_prompt

```python
evaluate_prompt(
    prompt: opik_optimizer.api_objects.chat_prompt.ChatPrompt | dict[str, opik_optimizer.api_objects.chat_prompt.ChatPrompt],
    dataset: Dataset,
    metric: MetricFunction,
    agent: opik_optimizer.agents.optimizable_agent.OptimizableAgent | None = None,
    n_threads: int | None = None,
    verbose: int = 1,
    dataset_item_ids: list[str] | None = None,
    experiment_config: dict | None = None,
    n_samples: int | float | str | None = None,
    n_samples_strategy: str | None = None,
    seed: int | None = None,
    return_evaluation_result: bool = False,
    allow_tool_use: bool = False,
    use_evaluate_on_dict_items: bool | None = None,
    sampling_tag: str | None = None
)
```

**Parameters:**

- `prompt` (opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt | dict\[str, opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt]) —

- `dataset` (Dataset) —

- `metric` (MetricFunction) —

- `agent` (opik\_optimizer.agents.optimizable\_agent.OptimizableAgent | None) —

- `n_threads` (int | None) —

- `verbose` (int, default: 1) —

- `dataset_item_ids` (list\[str] | None) —

- `experiment_config` (dict | None) —

- `n_samples` (int | float | str | None) —

- `n_samples_strategy` (str | None) —

- `seed` (int | None) —

- `return_evaluation_result` (bool, default: False) —

- `allow_tool_use` (bool, default: False) —

- `use_evaluate_on_dict_items` (bool | None) —

- `sampling_tag` (str | None) —

#### evaluate\_with\_result

```python
evaluate_with_result(
    context: OptimizationContext,
    prompts: dict,
    experiment_config: dict[str, typing.Any] | None = None,
    empty_score: float | None = None,
    n_samples: int | float | str | None = None,
    n_samples_strategy: str | None = None,
    sampling_tag: str | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `prompts` (dict) —

- `experiment_config` (dict\[str, typing.Any] | None) —

- `empty_score` (float | None) —

- `n_samples` (int | float | str | None) —

- `n_samples_strategy` (str | None) —

- `sampling_tag` (str | None) —

#### finish\_candidate

```python
finish_candidate(
    context: OptimizationContext,
    candidate_handle: Any,
    score: float | None,
    metrics: dict[str, typing.Any] | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    dataset: str | None = None,
    dataset_split: str | None = None,
    trial_index: int | None = None,
    timestamp: str | None = None,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate_handle` (Any) —

- `score` (float | None) —

- `metrics` (dict\[str, typing.Any] | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `dataset` (str | None) —

- `dataset_split` (str | None) —

- `trial_index` (int | None) —

- `timestamp` (str | None) —

- `round_handle` (typing.Any | None) —

#### finish\_round

```python
finish_round(
    round_handle: Any,
    context: opik_optimizer.core.state.OptimizationContext | None = None,
    best_score: float | None = None,
    best_candidate: typing.Any | None = None,
    best_prompt: typing.Any | None = None,
    stop_reason: str | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    timestamp: str | None = None,
    dataset_split: str | None = None,
    pareto_front: list[dict[str, typing.Any]] | None = None,
    selection_meta: dict[str, typing.Any] | None = None
)
```

**Parameters:**

- `round_handle` (Any) —

- `context` (opik\_optimizer.core.state.OptimizationContext | None) —

- `best_score` (float | None) —

- `best_candidate` (typing.Any | None) —

- `best_prompt` (typing.Any | None) —

- `stop_reason` (str | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `timestamp` (str | None) —

- `dataset_split` (str | None) —

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

- `selection_meta` (dict\[str, typing.Any] | None) —

#### get\_config

```python
get_config(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### get\_default\_prompt

```python
get_default_prompt(
    key: str
)
```

**Parameters:**

- `key` (str) — The prompt key to retrieve

#### get\_history\_entries

```python
get_history_entries()
```

#### get\_history\_rounds

```python
get_history_rounds()
```

#### get\_metadata

```python
get_metadata(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### get\_optimizer\_metadata

```python
get_optimizer_metadata()
```

#### get\_prompt

```python
get_prompt(
    key: str,
    fmt: Any
)
```

**Parameters:**

- `key` (str) — The prompt key to retrieve

- `fmt` (Any) —

#### list\_prompts

```python
list_prompts()
```

#### on\_trial

```python
on_trial(
    context: OptimizationContext,
    prompts: dict,
    score: float,
    prev_best_score: float | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `prompts` (dict) —

- `score` (float) —

- `prev_best_score` (float | None) —

#### optimize\_mcp

```python
optimize_mcp(
    args: Any,
    kwargs: Any
)
```

**Parameters:**

- `args` (Any) —

- `kwargs` (Any) —

#### optimize\_prompt

```python
optimize_prompt(
    prompt: opik_optimizer.api_objects.chat_prompt.ChatPrompt | dict[str, opik_optimizer.api_objects.chat_prompt.ChatPrompt],
    dataset: Dataset,
    metric: MetricFunction,
    agent: opik_optimizer.agents.optimizable_agent.OptimizableAgent | None = None,
    experiment_config: dict | None = None,
    n_samples: int | float | str | None = None,
    n_samples_minibatch: int | None = None,
    n_samples_strategy: str | None = None,
    auto_continue: bool = False,
    project_name: str | None = None,
    optimization_id: str | None = None,
    validation_dataset: opik.api_objects.dataset.dataset.Dataset | None = None,
    max_trials: int = 10,
    allow_tool_use: bool = True,
    optimize_prompts: bool | str | list[str] | None = 'system',
    optimize_tools: bool | dict[str, bool] | None = None,
    args: Any,
    kwargs: Any
)
```

**Parameters:**

- `prompt` (opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt | dict\[str, opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt]) — The prompt to optimize (single ChatPrompt or dict of prompts)

- `dataset` (Dataset) — Opik dataset (training set - used for feedback/context) TODO/FIXME: This parameter will be deprecated in favor of dataset\_training. For now, it serves as the training dataset parameter.

- `metric` (MetricFunction) — A metric function with signature (dataset\_item, llm\_output) -> float

- `agent` (opik\_optimizer.agents.optimizable\_agent.OptimizableAgent | None) — Optional agent for prompt execution (defaults to LiteLLMAgent)

- `experiment_config` (dict | None) — Optional configuration for the experiment

- `n_samples` (int | float | str | None) — Number of samples to use for evaluation

- `n_samples_minibatch` (int | None) — Optional number of samples for inner-loop minibatches

- `n_samples_strategy` (str | None) — Sampling strategy name (default "random\_sorted")

- `auto_continue` (bool, default: False) — Whether to continue optimization automatically

- `project_name` (str | None) — Opik project name for logging traces (defaults to OPIK\_PROJECT\_NAME env or "Optimization")

- `optimization_id` (str | None) — Optional ID to use when creating the Opik optimization run

- `validation_dataset` (opik.api\_objects.dataset.dataset.Dataset | None) — Optional validation dataset for ranking candidates

- `max_trials` (int, default: 10) — Maximum number of optimization trials

- `allow_tool_use` (bool, default: True) — Whether tools may be executed during evaluation (default True)

- `optimize_prompts` (bool | str | list\[str] | None, default: system) — Which prompt roles to allow for optimization

- `optimize_tools` (bool | dict\[str, bool] | None) — Optional tool optimization selector. Only supported by optimizers that explicitly document tool optimization support.

- `args` (Any) —

- `kwargs` (Any) —

#### post\_baseline

```python
post_baseline(
    context: OptimizationContext,
    score: float
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `score` (float) —

#### post\_optimize

```python
post_optimize(
    context: OptimizationContext,
    result: OptimizationResult
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `result` (OptimizationResult) —

#### post\_round

```python
post_round(
    round_handle: Any,
    context: opik_optimizer.core.state.OptimizationContext | None = None,
    best_score: float | None = None,
    best_candidate: typing.Any | None = None,
    best_prompt: typing.Any | None = None,
    stop_reason: str | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    timestamp: str | None = None,
    dataset_split: str | None = None,
    pareto_front: list[dict[str, typing.Any]] | None = None,
    selection_meta: dict[str, typing.Any] | None = None
)
```

**Parameters:**

- `round_handle` (Any) —

- `context` (opik\_optimizer.core.state.OptimizationContext | None) —

- `best_score` (float | None) —

- `best_candidate` (typing.Any | None) —

- `best_prompt` (typing.Any | None) —

- `stop_reason` (str | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `timestamp` (str | None) —

- `dataset_split` (str | None) —

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

- `selection_meta` (dict\[str, typing.Any] | None) —

#### post\_trial

```python
post_trial(
    context: OptimizationContext,
    candidate_handle: Any,
    score: float | None,
    metrics: dict[str, typing.Any] | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    dataset: str | None = None,
    dataset_split: str | None = None,
    trial_index: int | None = None,
    timestamp: str | None = None,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate_handle` (Any) —

- `score` (float | None) —

- `metrics` (dict\[str, typing.Any] | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `dataset` (str | None) —

- `dataset_split` (str | None) —

- `trial_index` (int | None) —

- `timestamp` (str | None) —

- `round_handle` (typing.Any | None) —

#### pre\_baseline

```python
pre_baseline(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### pre\_optimize

```python
pre_optimize(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) — The optimization context

#### pre\_round

```python
pre_round(
    context: OptimizationContext,
    extras: Any
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `extras` (Any) —

#### pre\_trial

```python
pre_trial(
    context: OptimizationContext,
    candidate: Any,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate` (Any) —

- `round_handle` (typing.Any | None) —

#### record\_candidate\_entry

```python
record_candidate_entry(
    prompt_or_payload: Any,
    score: float | None = None,
    id: str | None = None,
    metrics: dict[str, typing.Any] | None = None,
    notes: str | None = None,
    extra: dict[str, typing.Any] | None = None,
    context: opik_optimizer.core.state.OptimizationContext | None = None
)
```

**Parameters:**

- `prompt_or_payload` (Any) —

- `score` (float | None) —

- `id` (str | None) —

- `metrics` (dict\[str, typing.Any] | None) —

- `notes` (str | None) —

- `extra` (dict\[str, typing.Any] | None) —

- `context` (opik\_optimizer.core.state.OptimizationContext | None) —

#### run\_optimization

```python
run_optimization(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) — The optimization context with prompts, dataset, metric, etc.

#### set\_default\_dataset\_split

```python
set_default_dataset_split(
    dataset_split: str | None
)
```

**Parameters:**

- `dataset_split` (str | None) —

#### set\_pareto\_front

```python
set_pareto_front(
    pareto_front: list[dict[str, typing.Any]] | None
)
```

**Parameters:**

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

#### set\_selection\_meta

```python
set_selection_meta(
    selection_meta: dict[str, typing.Any] | None
)
```

**Parameters:**

- `selection_meta` (dict\[str, typing.Any] | None) —

#### start\_candidate

```python
start_candidate(
    context: OptimizationContext,
    candidate: Any,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate` (Any) —

- `round_handle` (typing.Any | None) —

#### with\_dataset\_split

```python
with_dataset_split(
    dataset_split: str | None
)
```

**Parameters:**

- `dataset_split` (str | None) —

## GepaOptimizer

```python
GepaOptimizer(
    model: str = 'openai/gpt-5-nano',
    model_parameters: dict[str, typing.Any] | None = None,
    n_threads: int = 12,
    verbose: int = 1,
    seed: int = 42,
    name: str | None = None,
    skip_perfect_score: bool = True,
    perfect_score: float = 0.95,
    prompt_overrides: dict[str, str] | collections.abc.Callable[[opik_optimizer.utils.prompt_library.PromptLibrary], None] | None = None
)
```

**Parameters:**

- `model` (str, default: openai/gpt-5-nano) —

- `model_parameters` (dict\[str, typing.Any] | None) —

- `n_threads` (int, default: 12) —

- `verbose` (int, default: 1) —

- `seed` (int, default: 42) —

- `name` (str | None) —

- `skip_perfect_score` (bool, default: True) —

- `perfect_score` (float, default: 0.95) —

- `prompt_overrides` (dict\[str, str] | collections.abc.Callable\[\[opik\_optimizer.utils.prompt\_library.PromptLibrary], None] | None) —

### Methods

#### begin\_round

```python
begin_round(
    context: OptimizationContext,
    extras: Any
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `extras` (Any) —

#### cleanup

```python
cleanup()
```

#### evaluate

```python
evaluate(
    context: OptimizationContext,
    prompts: dict,
    experiment_config: dict[str, typing.Any] | None = None,
    sampling_tag: str | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) — Optimization context for this run.

- `prompts` (dict) — Dict of named prompts to evaluate (e.g., {"main": ChatPrompt(...)}). Single-prompt optimizations use a dict with one entry.

- `experiment_config` (dict\[str, typing.Any] | None) — Optional experiment configuration.

- `sampling_tag` (str | None) — Optional sampling tag for deterministic subsampling per candidate.

#### evaluate\_prompt

```python
evaluate_prompt(
    prompt: opik_optimizer.api_objects.chat_prompt.ChatPrompt | dict[str, opik_optimizer.api_objects.chat_prompt.ChatPrompt],
    dataset: Dataset,
    metric: MetricFunction,
    agent: opik_optimizer.agents.optimizable_agent.OptimizableAgent | None = None,
    n_threads: int | None = None,
    verbose: int = 1,
    dataset_item_ids: list[str] | None = None,
    experiment_config: dict | None = None,
    n_samples: int | float | str | None = None,
    n_samples_strategy: str | None = None,
    seed: int | None = None,
    return_evaluation_result: bool = False,
    allow_tool_use: bool | None = None,
    use_evaluate_on_dict_items: bool | None = None,
    sampling_tag: str | None = None
)
```

**Parameters:**

- `prompt` (opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt | dict\[str, opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt]) —

- `dataset` (Dataset) —

- `metric` (MetricFunction) —

- `agent` (opik\_optimizer.agents.optimizable\_agent.OptimizableAgent | None) —

- `n_threads` (int | None) —

- `verbose` (int, default: 1) —

- `dataset_item_ids` (list\[str] | None) —

- `experiment_config` (dict | None) —

- `n_samples` (int | float | str | None) —

- `n_samples_strategy` (str | None) —

- `seed` (int | None) —

- `return_evaluation_result` (bool, default: False) —

- `allow_tool_use` (bool | None) —

- `use_evaluate_on_dict_items` (bool | None) —

- `sampling_tag` (str | None) —

#### evaluate\_with\_result

```python
evaluate_with_result(
    context: OptimizationContext,
    prompts: dict,
    experiment_config: dict[str, typing.Any] | None = None,
    empty_score: float | None = None,
    n_samples: int | float | str | None = None,
    n_samples_strategy: str | None = None,
    sampling_tag: str | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `prompts` (dict) —

- `experiment_config` (dict\[str, typing.Any] | None) —

- `empty_score` (float | None) —

- `n_samples` (int | float | str | None) —

- `n_samples_strategy` (str | None) —

- `sampling_tag` (str | None) —

#### finish\_candidate

```python
finish_candidate(
    context: OptimizationContext,
    candidate_handle: Any,
    score: float | None,
    metrics: dict[str, typing.Any] | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    dataset: str | None = None,
    dataset_split: str | None = None,
    trial_index: int | None = None,
    timestamp: str | None = None,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate_handle` (Any) —

- `score` (float | None) —

- `metrics` (dict\[str, typing.Any] | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `dataset` (str | None) —

- `dataset_split` (str | None) —

- `trial_index` (int | None) —

- `timestamp` (str | None) —

- `round_handle` (typing.Any | None) —

#### finish\_round

```python
finish_round(
    round_handle: Any,
    context: opik_optimizer.core.state.OptimizationContext | None = None,
    best_score: float | None = None,
    best_candidate: typing.Any | None = None,
    best_prompt: typing.Any | None = None,
    stop_reason: str | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    timestamp: str | None = None,
    dataset_split: str | None = None,
    pareto_front: list[dict[str, typing.Any]] | None = None,
    selection_meta: dict[str, typing.Any] | None = None
)
```

**Parameters:**

- `round_handle` (Any) —

- `context` (opik\_optimizer.core.state.OptimizationContext | None) —

- `best_score` (float | None) —

- `best_candidate` (typing.Any | None) —

- `best_prompt` (typing.Any | None) —

- `stop_reason` (str | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `timestamp` (str | None) —

- `dataset_split` (str | None) —

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

- `selection_meta` (dict\[str, typing.Any] | None) —

#### get\_config

```python
get_config(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### get\_default\_prompt

```python
get_default_prompt(
    key: str
)
```

**Parameters:**

- `key` (str) — The prompt key to retrieve

#### get\_history\_entries

```python
get_history_entries()
```

#### get\_history\_rounds

```python
get_history_rounds()
```

#### get\_metadata

```python
get_metadata(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### get\_optimizer\_metadata

```python
get_optimizer_metadata()
```

#### get\_prompt

```python
get_prompt(
    key: str,
    fmt: Any
)
```

**Parameters:**

- `key` (str) — The prompt key to retrieve

- `fmt` (Any) —

#### list\_prompts

```python
list_prompts()
```

#### on\_trial

```python
on_trial(
    context: OptimizationContext,
    prompts: dict,
    score: float,
    prev_best_score: float | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `prompts` (dict) —

- `score` (float) —

- `prev_best_score` (float | None) —

#### optimize\_mcp

```python
optimize_mcp(
    args: Any,
    kwargs: Any
)
```

**Parameters:**

- `args` (Any) —

- `kwargs` (Any) —

#### optimize\_prompt

```python
optimize_prompt(
    prompt: opik_optimizer.api_objects.chat_prompt.ChatPrompt | dict[str, opik_optimizer.api_objects.chat_prompt.ChatPrompt],
    dataset: Dataset,
    metric: MetricFunction,
    agent: opik_optimizer.agents.optimizable_agent.OptimizableAgent | None = None,
    experiment_config: dict | None = None,
    n_samples: int | float | str | None = None,
    n_samples_minibatch: int | None = None,
    n_samples_strategy: str | None = None,
    auto_continue: bool = False,
    project_name: str | None = None,
    optimization_id: str | None = None,
    validation_dataset: opik.api_objects.dataset.dataset.Dataset | None = None,
    max_trials: int = 10,
    allow_tool_use: bool = True,
    optimize_prompts: bool | str | list[str] | None = 'system',
    optimize_tools: bool | dict[str, bool] | None = None,
    args: Any,
    kwargs: Any
)
```

**Parameters:**

- `prompt` (opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt | dict\[str, opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt]) — The prompt to optimize (single ChatPrompt or dict of prompts)

- `dataset` (Dataset) — Opik dataset (training set - used for feedback/context) TODO/FIXME: This parameter will be deprecated in favor of dataset\_training. For now, it serves as the training dataset parameter.

- `metric` (MetricFunction) — A metric function with signature (dataset\_item, llm\_output) -> float

- `agent` (opik\_optimizer.agents.optimizable\_agent.OptimizableAgent | None) — Optional agent for prompt execution (defaults to LiteLLMAgent)

- `experiment_config` (dict | None) — Optional configuration for the experiment

- `n_samples` (int | float | str | None) — Number of samples to use for evaluation

- `n_samples_minibatch` (int | None) — Optional number of samples for inner-loop minibatches

- `n_samples_strategy` (str | None) — Sampling strategy name (default "random\_sorted")

- `auto_continue` (bool, default: False) — Whether to continue optimization automatically

- `project_name` (str | None) — Opik project name for logging traces (defaults to OPIK\_PROJECT\_NAME env or "Optimization")

- `optimization_id` (str | None) — Optional ID to use when creating the Opik optimization run

- `validation_dataset` (opik.api\_objects.dataset.dataset.Dataset | None) — Optional validation dataset for ranking candidates

- `max_trials` (int, default: 10) — Maximum number of optimization trials

- `allow_tool_use` (bool, default: True) — Whether tools may be executed during evaluation (default True)

- `optimize_prompts` (bool | str | list\[str] | None, default: system) — Which prompt roles to allow for optimization

- `optimize_tools` (bool | dict\[str, bool] | None) — Optional tool optimization selector. Only supported by optimizers that explicitly document tool optimization support.

- `args` (Any) —

- `kwargs` (Any) —

#### post\_baseline

```python
post_baseline(
    context: OptimizationContext,
    score: float
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `score` (float) —

#### post\_optimize

```python
post_optimize(
    context: OptimizationContext,
    result: OptimizationResult
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `result` (OptimizationResult) —

#### post\_round

```python
post_round(
    round_handle: Any,
    context: opik_optimizer.core.state.OptimizationContext | None = None,
    best_score: float | None = None,
    best_candidate: typing.Any | None = None,
    best_prompt: typing.Any | None = None,
    stop_reason: str | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    timestamp: str | None = None,
    dataset_split: str | None = None,
    pareto_front: list[dict[str, typing.Any]] | None = None,
    selection_meta: dict[str, typing.Any] | None = None
)
```

**Parameters:**

- `round_handle` (Any) —

- `context` (opik\_optimizer.core.state.OptimizationContext | None) —

- `best_score` (float | None) —

- `best_candidate` (typing.Any | None) —

- `best_prompt` (typing.Any | None) —

- `stop_reason` (str | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `timestamp` (str | None) —

- `dataset_split` (str | None) —

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

- `selection_meta` (dict\[str, typing.Any] | None) —

#### post\_trial

```python
post_trial(
    context: OptimizationContext,
    candidate_handle: Any,
    score: float | None,
    metrics: dict[str, typing.Any] | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    dataset: str | None = None,
    dataset_split: str | None = None,
    trial_index: int | None = None,
    timestamp: str | None = None,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate_handle` (Any) —

- `score` (float | None) —

- `metrics` (dict\[str, typing.Any] | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `dataset` (str | None) —

- `dataset_split` (str | None) —

- `trial_index` (int | None) —

- `timestamp` (str | None) —

- `round_handle` (typing.Any | None) —

#### pre\_baseline

```python
pre_baseline(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### pre\_optimize

```python
pre_optimize(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### pre\_round

```python
pre_round(
    context: OptimizationContext,
    extras: Any
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `extras` (Any) —

#### pre\_trial

```python
pre_trial(
    context: OptimizationContext,
    candidate: Any,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate` (Any) —

- `round_handle` (typing.Any | None) —

#### record\_candidate\_entry

```python
record_candidate_entry(
    prompt_or_payload: Any,
    score: float | None = None,
    id: str | None = None,
    metrics: dict[str, typing.Any] | None = None,
    notes: str | None = None,
    extra: dict[str, typing.Any] | None = None,
    context: opik_optimizer.core.state.OptimizationContext | None = None
)
```

**Parameters:**

- `prompt_or_payload` (Any) —

- `score` (float | None) —

- `id` (str | None) —

- `metrics` (dict\[str, typing.Any] | None) —

- `notes` (str | None) —

- `extra` (dict\[str, typing.Any] | None) —

- `context` (opik\_optimizer.core.state.OptimizationContext | None) —

#### run\_optimization

```python
run_optimization(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) — The optimization context with prompts, dataset, metric, etc.

#### set\_default\_dataset\_split

```python
set_default_dataset_split(
    dataset_split: str | None
)
```

**Parameters:**

- `dataset_split` (str | None) —

#### set\_pareto\_front

```python
set_pareto_front(
    pareto_front: list[dict[str, typing.Any]] | None
)
```

**Parameters:**

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

#### set\_selection\_meta

```python
set_selection_meta(
    selection_meta: dict[str, typing.Any] | None
)
```

**Parameters:**

- `selection_meta` (dict\[str, typing.Any] | None) —

#### start\_candidate

```python
start_candidate(
    context: OptimizationContext,
    candidate: Any,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate` (Any) —

- `round_handle` (typing.Any | None) —

#### with\_dataset\_split

```python
with_dataset_split(
    dataset_split: str | None
)
```

**Parameters:**

- `dataset_split` (str | None) —

## MetaPromptOptimizer

```python
MetaPromptOptimizer(
    model: str = 'openai/gpt-5-nano',
    model_parameters: dict[str, typing.Any] | None = None,
    prompts_per_round: int = 4,
    enable_context: bool = True,
    num_task_examples: int = 5,
    task_context_columns: list[str] | None = None,
    n_threads: int = 12,
    verbose: int = 1,
    seed: int = 42,
    name: str | None = None,
    use_hall_of_fame: bool = True,
    prompt_overrides: dict[str, str] | collections.abc.Callable[[opik_optimizer.utils.prompt_library.PromptLibrary], None] | None = None,
    skip_perfect_score: bool = True,
    perfect_score: float = 0.95
)
```

**Parameters:**

- `model` (str, default: openai/gpt-5-nano) —

- `model_parameters` (dict\[str, typing.Any] | None) —

- `prompts_per_round` (int, default: 4) —

- `enable_context` (bool, default: True) —

- `num_task_examples` (int, default: 5) —

- `task_context_columns` (list\[str] | None) —

- `n_threads` (int, default: 12) —

- `verbose` (int, default: 1) —

- `seed` (int, default: 42) —

- `name` (str | None) —

- `use_hall_of_fame` (bool, default: True) —

- `prompt_overrides` (dict\[str, str] | collections.abc.Callable\[\[opik\_optimizer.utils.prompt\_library.PromptLibrary], None] | None) —

- `skip_perfect_score` (bool, default: True) —

- `perfect_score` (float, default: 0.95) —

### Methods

#### begin\_round

```python
begin_round(
    context: OptimizationContext,
    extras: Any
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `extras` (Any) —

#### cleanup

```python
cleanup()
```

#### evaluate

```python
evaluate(
    context: OptimizationContext,
    prompts: dict,
    experiment_config: dict[str, typing.Any] | None = None,
    sampling_tag: str | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) — Optimization context for this run.

- `prompts` (dict) — Dict of named prompts to evaluate (e.g., {"main": ChatPrompt(...)}). Single-prompt optimizations use a dict with one entry.

- `experiment_config` (dict\[str, typing.Any] | None) — Optional experiment configuration.

- `sampling_tag` (str | None) — Optional sampling tag for deterministic subsampling per candidate.

#### evaluate\_prompt

```python
evaluate_prompt(
    prompt: opik_optimizer.api_objects.chat_prompt.ChatPrompt | dict[str, opik_optimizer.api_objects.chat_prompt.ChatPrompt],
    dataset: Dataset,
    metric: MetricFunction,
    agent: opik_optimizer.agents.optimizable_agent.OptimizableAgent | None = None,
    n_threads: int | None = None,
    verbose: int = 1,
    dataset_item_ids: list[str] | None = None,
    experiment_config: dict | None = None,
    n_samples: int | float | str | None = None,
    n_samples_strategy: str | None = None,
    seed: int | None = None,
    return_evaluation_result: bool = False,
    allow_tool_use: bool | None = None,
    use_evaluate_on_dict_items: bool | None = None,
    sampling_tag: str | None = None
)
```

**Parameters:**

- `prompt` (opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt | dict\[str, opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt]) —

- `dataset` (Dataset) —

- `metric` (MetricFunction) —

- `agent` (opik\_optimizer.agents.optimizable\_agent.OptimizableAgent | None) —

- `n_threads` (int | None) —

- `verbose` (int, default: 1) —

- `dataset_item_ids` (list\[str] | None) —

- `experiment_config` (dict | None) —

- `n_samples` (int | float | str | None) —

- `n_samples_strategy` (str | None) —

- `seed` (int | None) —

- `return_evaluation_result` (bool, default: False) —

- `allow_tool_use` (bool | None) —

- `use_evaluate_on_dict_items` (bool | None) —

- `sampling_tag` (str | None) —

#### evaluate\_with\_result

```python
evaluate_with_result(
    context: OptimizationContext,
    prompts: dict,
    experiment_config: dict[str, typing.Any] | None = None,
    empty_score: float | None = None,
    n_samples: int | float | str | None = None,
    n_samples_strategy: str | None = None,
    sampling_tag: str | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `prompts` (dict) —

- `experiment_config` (dict\[str, typing.Any] | None) —

- `empty_score` (float | None) —

- `n_samples` (int | float | str | None) —

- `n_samples_strategy` (str | None) —

- `sampling_tag` (str | None) —

#### finish\_candidate

```python
finish_candidate(
    context: OptimizationContext,
    candidate_handle: Any,
    score: float | None,
    metrics: dict[str, typing.Any] | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    dataset: str | None = None,
    dataset_split: str | None = None,
    trial_index: int | None = None,
    timestamp: str | None = None,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate_handle` (Any) —

- `score` (float | None) —

- `metrics` (dict\[str, typing.Any] | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `dataset` (str | None) —

- `dataset_split` (str | None) —

- `trial_index` (int | None) —

- `timestamp` (str | None) —

- `round_handle` (typing.Any | None) —

#### finish\_round

```python
finish_round(
    round_handle: Any,
    context: opik_optimizer.core.state.OptimizationContext | None = None,
    best_score: float | None = None,
    best_candidate: typing.Any | None = None,
    best_prompt: typing.Any | None = None,
    stop_reason: str | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    timestamp: str | None = None,
    dataset_split: str | None = None,
    pareto_front: list[dict[str, typing.Any]] | None = None,
    selection_meta: dict[str, typing.Any] | None = None
)
```

**Parameters:**

- `round_handle` (Any) —

- `context` (opik\_optimizer.core.state.OptimizationContext | None) —

- `best_score` (float | None) —

- `best_candidate` (typing.Any | None) —

- `best_prompt` (typing.Any | None) —

- `stop_reason` (str | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `timestamp` (str | None) —

- `dataset_split` (str | None) —

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

- `selection_meta` (dict\[str, typing.Any] | None) —

#### get\_config

```python
get_config(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### get\_default\_prompt

```python
get_default_prompt(
    key: str
)
```

**Parameters:**

- `key` (str) — The prompt key to retrieve

#### get\_history\_entries

```python
get_history_entries()
```

#### get\_history\_rounds

```python
get_history_rounds()
```

#### get\_metadata

```python
get_metadata(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### get\_optimizer\_metadata

```python
get_optimizer_metadata()
```

#### get\_prompt

```python
get_prompt(
    key: str,
    fmt: Any
)
```

**Parameters:**

- `key` (str) — The prompt key to retrieve

- `fmt` (Any) —

#### list\_prompts

```python
list_prompts()
```

#### on\_trial

```python
on_trial(
    context: OptimizationContext,
    prompts: dict,
    score: float,
    prev_best_score: float | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `prompts` (dict) —

- `score` (float) —

- `prev_best_score` (float | None) —

#### optimize\_mcp

```python
optimize_mcp(
    args: Any,
    kwargs: Any
)
```

**Parameters:**

- `args` (Any) —

- `kwargs` (Any) —

#### optimize\_prompt

```python
optimize_prompt(
    prompt: opik_optimizer.api_objects.chat_prompt.ChatPrompt | dict[str, opik_optimizer.api_objects.chat_prompt.ChatPrompt],
    dataset: Dataset,
    metric: MetricFunction,
    agent: opik_optimizer.agents.optimizable_agent.OptimizableAgent | None = None,
    experiment_config: dict | None = None,
    n_samples: int | float | str | None = None,
    n_samples_minibatch: int | None = None,
    n_samples_strategy: str | None = None,
    auto_continue: bool = False,
    project_name: str | None = None,
    optimization_id: str | None = None,
    validation_dataset: opik.api_objects.dataset.dataset.Dataset | None = None,
    max_trials: int = 10,
    allow_tool_use: bool = True,
    optimize_prompts: bool | str | list[str] | None = 'system',
    optimize_tools: bool | dict[str, bool] | None = None,
    args: Any,
    kwargs: Any
)
```

**Parameters:**

- `prompt` (opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt | dict\[str, opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt]) — The prompt to optimize (single ChatPrompt or dict of prompts)

- `dataset` (Dataset) — Opik dataset (training set - used for feedback/context) TODO/FIXME: This parameter will be deprecated in favor of dataset\_training. For now, it serves as the training dataset parameter.

- `metric` (MetricFunction) — A metric function with signature (dataset\_item, llm\_output) -> float

- `agent` (opik\_optimizer.agents.optimizable\_agent.OptimizableAgent | None) — Optional agent for prompt execution (defaults to LiteLLMAgent)

- `experiment_config` (dict | None) — Optional configuration for the experiment

- `n_samples` (int | float | str | None) — Number of samples to use for evaluation

- `n_samples_minibatch` (int | None) — Optional number of samples for inner-loop minibatches

- `n_samples_strategy` (str | None) — Sampling strategy name (default "random\_sorted")

- `auto_continue` (bool, default: False) — Whether to continue optimization automatically

- `project_name` (str | None) — Opik project name for logging traces (defaults to OPIK\_PROJECT\_NAME env or "Optimization")

- `optimization_id` (str | None) — Optional ID to use when creating the Opik optimization run

- `validation_dataset` (opik.api\_objects.dataset.dataset.Dataset | None) — Optional validation dataset for ranking candidates

- `max_trials` (int, default: 10) — Maximum number of optimization trials

- `allow_tool_use` (bool, default: True) — Whether tools may be executed during evaluation (default True)

- `optimize_prompts` (bool | str | list\[str] | None, default: system) — Which prompt roles to allow for optimization

- `optimize_tools` (bool | dict\[str, bool] | None) — Optional tool optimization selector. Only supported by optimizers that explicitly document tool optimization support.

- `args` (Any) —

- `kwargs` (Any) —

#### post\_baseline

```python
post_baseline(
    context: OptimizationContext,
    score: float
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `score` (float) —

#### post\_optimize

```python
post_optimize(
    context: OptimizationContext,
    result: OptimizationResult
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `result` (OptimizationResult) —

#### post\_round

```python
post_round(
    round_handle: Any,
    context: opik_optimizer.core.state.OptimizationContext | None = None,
    best_score: float | None = None,
    best_candidate: typing.Any | None = None,
    best_prompt: typing.Any | None = None,
    stop_reason: str | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    timestamp: str | None = None,
    dataset_split: str | None = None,
    pareto_front: list[dict[str, typing.Any]] | None = None,
    selection_meta: dict[str, typing.Any] | None = None
)
```

**Parameters:**

- `round_handle` (Any) —

- `context` (opik\_optimizer.core.state.OptimizationContext | None) —

- `best_score` (float | None) —

- `best_candidate` (typing.Any | None) —

- `best_prompt` (typing.Any | None) —

- `stop_reason` (str | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `timestamp` (str | None) —

- `dataset_split` (str | None) —

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

- `selection_meta` (dict\[str, typing.Any] | None) —

#### post\_trial

```python
post_trial(
    context: OptimizationContext,
    candidate_handle: Any,
    score: float | None,
    metrics: dict[str, typing.Any] | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    dataset: str | None = None,
    dataset_split: str | None = None,
    trial_index: int | None = None,
    timestamp: str | None = None,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate_handle` (Any) —

- `score` (float | None) —

- `metrics` (dict\[str, typing.Any] | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `dataset` (str | None) —

- `dataset_split` (str | None) —

- `trial_index` (int | None) —

- `timestamp` (str | None) —

- `round_handle` (typing.Any | None) —

#### pre\_baseline

```python
pre_baseline(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### pre\_optimize

```python
pre_optimize(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) — The optimization context

#### pre\_round

```python
pre_round(
    context: OptimizationContext,
    extras: Any
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `extras` (Any) —

#### pre\_trial

```python
pre_trial(
    context: OptimizationContext,
    candidate: Any,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate` (Any) —

- `round_handle` (typing.Any | None) —

#### record\_candidate\_entry

```python
record_candidate_entry(
    prompt_or_payload: Any,
    score: float | None = None,
    id: str | None = None,
    metrics: dict[str, typing.Any] | None = None,
    notes: str | None = None,
    extra: dict[str, typing.Any] | None = None,
    context: opik_optimizer.core.state.OptimizationContext | None = None
)
```

**Parameters:**

- `prompt_or_payload` (Any) —

- `score` (float | None) —

- `id` (str | None) —

- `metrics` (dict\[str, typing.Any] | None) —

- `notes` (str | None) —

- `extra` (dict\[str, typing.Any] | None) —

- `context` (opik\_optimizer.core.state.OptimizationContext | None) —

#### run\_optimization

```python
run_optimization(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) — The optimization context with prompts, dataset, metric, etc.

#### set\_default\_dataset\_split

```python
set_default_dataset_split(
    dataset_split: str | None
)
```

**Parameters:**

- `dataset_split` (str | None) —

#### set\_pareto\_front

```python
set_pareto_front(
    pareto_front: list[dict[str, typing.Any]] | None
)
```

**Parameters:**

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

#### set\_selection\_meta

```python
set_selection_meta(
    selection_meta: dict[str, typing.Any] | None
)
```

**Parameters:**

- `selection_meta` (dict\[str, typing.Any] | None) —

#### start\_candidate

```python
start_candidate(
    context: OptimizationContext,
    candidate: Any,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate` (Any) —

- `round_handle` (typing.Any | None) —

#### with\_dataset\_split

```python
with_dataset_split(
    dataset_split: str | None
)
```

**Parameters:**

- `dataset_split` (str | None) —

## EvolutionaryOptimizer

```python
EvolutionaryOptimizer(
    model: str = 'openai/gpt-5-nano',
    model_parameters: dict[str, typing.Any] | None = None,
    population_size: int = 30,
    num_generations: int = 15,
    mutation_rate: float = 0.2,
    crossover_rate: float = 0.8,
    tournament_size: int = 4,
    elitism_size: int = 3,
    adaptive_mutation: bool = True,
    enable_moo: bool = True,
    enable_llm_crossover: bool = True,
    enable_semantic_crossover: bool = False,
    output_style_guidance: str | None = None,
    infer_output_style: bool = False,
    n_threads: int = 12,
    verbose: int = 1,
    seed: int = 42,
    name: str | None = None,
    prompt_overrides: dict[str, str] | collections.abc.Callable[[opik_optimizer.utils.prompt_library.PromptLibrary], None] | None = None,
    skip_perfect_score: bool = True,
    perfect_score: float = 0.95
)
```

**Parameters:**

- `model` (str, default: openai/gpt-5-nano) —

- `model_parameters` (dict\[str, typing.Any] | None) —

- `population_size` (int, default: 30) —

- `num_generations` (int, default: 15) —

- `mutation_rate` (float, default: 0.2) —

- `crossover_rate` (float, default: 0.8) —

- `tournament_size` (int, default: 4) —

- `elitism_size` (int, default: 3) —

- `adaptive_mutation` (bool, default: True) —

- `enable_moo` (bool, default: True) —

- `enable_llm_crossover` (bool, default: True) —

- `enable_semantic_crossover` (bool, default: False) —

- `output_style_guidance` (str | None) —

- `infer_output_style` (bool, default: False) —

- `n_threads` (int, default: 12) —

- `verbose` (int, default: 1) —

- `seed` (int, default: 42) —

- `name` (str | None) —

- `prompt_overrides` (dict\[str, str] | collections.abc.Callable\[\[opik\_optimizer.utils.prompt\_library.PromptLibrary], None] | None) —

- `skip_perfect_score` (bool, default: True) —

- `perfect_score` (float, default: 0.95) —

### Methods

#### begin\_round

```python
begin_round(
    context: OptimizationContext,
    extras: Any
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `extras` (Any) —

#### cleanup

```python
cleanup()
```

#### evaluate

```python
evaluate(
    context: OptimizationContext,
    prompts: dict,
    experiment_config: dict[str, typing.Any] | None = None,
    sampling_tag: str | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) — Optimization context for this run.

- `prompts` (dict) — Dict of named prompts to evaluate (e.g., {"main": ChatPrompt(...)}). Single-prompt optimizations use a dict with one entry.

- `experiment_config` (dict\[str, typing.Any] | None) — Optional experiment configuration.

- `sampling_tag` (str | None) — Optional sampling tag for deterministic subsampling per candidate.

#### evaluate\_prompt

```python
evaluate_prompt(
    prompt: opik_optimizer.api_objects.chat_prompt.ChatPrompt | dict[str, opik_optimizer.api_objects.chat_prompt.ChatPrompt],
    dataset: Dataset,
    metric: MetricFunction,
    agent: opik_optimizer.agents.optimizable_agent.OptimizableAgent | None = None,
    n_threads: int | None = None,
    verbose: int = 1,
    dataset_item_ids: list[str] | None = None,
    experiment_config: dict | None = None,
    n_samples: int | float | str | None = None,
    n_samples_strategy: str | None = None,
    seed: int | None = None,
    return_evaluation_result: bool = False,
    allow_tool_use: bool | None = None,
    use_evaluate_on_dict_items: bool | None = None,
    sampling_tag: str | None = None
)
```

**Parameters:**

- `prompt` (opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt | dict\[str, opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt]) —

- `dataset` (Dataset) —

- `metric` (MetricFunction) —

- `agent` (opik\_optimizer.agents.optimizable\_agent.OptimizableAgent | None) —

- `n_threads` (int | None) —

- `verbose` (int, default: 1) —

- `dataset_item_ids` (list\[str] | None) —

- `experiment_config` (dict | None) —

- `n_samples` (int | float | str | None) —

- `n_samples_strategy` (str | None) —

- `seed` (int | None) —

- `return_evaluation_result` (bool, default: False) —

- `allow_tool_use` (bool | None) —

- `use_evaluate_on_dict_items` (bool | None) —

- `sampling_tag` (str | None) —

#### evaluate\_with\_result

```python
evaluate_with_result(
    context: OptimizationContext,
    prompts: dict,
    experiment_config: dict[str, typing.Any] | None = None,
    empty_score: float | None = None,
    n_samples: int | float | str | None = None,
    n_samples_strategy: str | None = None,
    sampling_tag: str | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `prompts` (dict) —

- `experiment_config` (dict\[str, typing.Any] | None) —

- `empty_score` (float | None) —

- `n_samples` (int | float | str | None) —

- `n_samples_strategy` (str | None) —

- `sampling_tag` (str | None) —

#### finish\_candidate

```python
finish_candidate(
    context: OptimizationContext,
    candidate_handle: Any,
    score: float | None,
    metrics: dict[str, typing.Any] | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    dataset: str | None = None,
    dataset_split: str | None = None,
    trial_index: int | None = None,
    timestamp: str | None = None,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate_handle` (Any) —

- `score` (float | None) —

- `metrics` (dict\[str, typing.Any] | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `dataset` (str | None) —

- `dataset_split` (str | None) —

- `trial_index` (int | None) —

- `timestamp` (str | None) —

- `round_handle` (typing.Any | None) —

#### finish\_round

```python
finish_round(
    round_handle: Any,
    context: opik_optimizer.core.state.OptimizationContext | None = None,
    best_score: float | None = None,
    best_candidate: typing.Any | None = None,
    best_prompt: typing.Any | None = None,
    stop_reason: str | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    timestamp: str | None = None,
    dataset_split: str | None = None,
    pareto_front: list[dict[str, typing.Any]] | None = None,
    selection_meta: dict[str, typing.Any] | None = None
)
```

**Parameters:**

- `round_handle` (Any) —

- `context` (opik\_optimizer.core.state.OptimizationContext | None) —

- `best_score` (float | None) —

- `best_candidate` (typing.Any | None) —

- `best_prompt` (typing.Any | None) —

- `stop_reason` (str | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `timestamp` (str | None) —

- `dataset_split` (str | None) —

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

- `selection_meta` (dict\[str, typing.Any] | None) —

#### get\_config

```python
get_config(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### get\_default\_prompt

```python
get_default_prompt(
    key: str
)
```

**Parameters:**

- `key` (str) — The prompt key to retrieve

#### get\_history\_entries

```python
get_history_entries()
```

#### get\_history\_rounds

```python
get_history_rounds()
```

#### get\_metadata

```python
get_metadata(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### get\_optimizer\_metadata

```python
get_optimizer_metadata()
```

#### get\_prompt

```python
get_prompt(
    key: str,
    fmt: Any
)
```

**Parameters:**

- `key` (str) — The prompt key to retrieve

- `fmt` (Any) —

#### list\_prompts

```python
list_prompts()
```

#### on\_trial

```python
on_trial(
    context: OptimizationContext,
    prompts: dict,
    score: float,
    prev_best_score: float | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `prompts` (dict) —

- `score` (float) —

- `prev_best_score` (float | None) —

#### optimize\_mcp

```python
optimize_mcp(
    args: Any,
    kwargs: Any
)
```

**Parameters:**

- `args` (Any) —

- `kwargs` (Any) —

#### optimize\_prompt

```python
optimize_prompt(
    prompt: opik_optimizer.api_objects.chat_prompt.ChatPrompt | dict[str, opik_optimizer.api_objects.chat_prompt.ChatPrompt],
    dataset: Dataset,
    metric: MetricFunction,
    agent: opik_optimizer.agents.optimizable_agent.OptimizableAgent | None = None,
    experiment_config: dict | None = None,
    n_samples: int | float | str | None = None,
    n_samples_minibatch: int | None = None,
    n_samples_strategy: str | None = None,
    auto_continue: bool = False,
    project_name: str | None = None,
    optimization_id: str | None = None,
    validation_dataset: opik.api_objects.dataset.dataset.Dataset | None = None,
    max_trials: int = 10,
    allow_tool_use: bool = True,
    optimize_prompts: bool | str | list[str] | None = 'system',
    optimize_tools: bool | dict[str, bool] | None = None,
    args: Any,
    kwargs: Any
)
```

**Parameters:**

- `prompt` (opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt | dict\[str, opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt]) — The prompt to optimize (single ChatPrompt or dict of prompts)

- `dataset` (Dataset) — Opik dataset (training set - used for feedback/context) TODO/FIXME: This parameter will be deprecated in favor of dataset\_training. For now, it serves as the training dataset parameter.

- `metric` (MetricFunction) — A metric function with signature (dataset\_item, llm\_output) -> float

- `agent` (opik\_optimizer.agents.optimizable\_agent.OptimizableAgent | None) — Optional agent for prompt execution (defaults to LiteLLMAgent)

- `experiment_config` (dict | None) — Optional configuration for the experiment

- `n_samples` (int | float | str | None) — Number of samples to use for evaluation

- `n_samples_minibatch` (int | None) — Optional number of samples for inner-loop minibatches

- `n_samples_strategy` (str | None) — Sampling strategy name (default "random\_sorted")

- `auto_continue` (bool, default: False) — Whether to continue optimization automatically

- `project_name` (str | None) — Opik project name for logging traces (defaults to OPIK\_PROJECT\_NAME env or "Optimization")

- `optimization_id` (str | None) — Optional ID to use when creating the Opik optimization run

- `validation_dataset` (opik.api\_objects.dataset.dataset.Dataset | None) — Optional validation dataset for ranking candidates

- `max_trials` (int, default: 10) — Maximum number of optimization trials

- `allow_tool_use` (bool, default: True) — Whether tools may be executed during evaluation (default True)

- `optimize_prompts` (bool | str | list\[str] | None, default: system) — Which prompt roles to allow for optimization

- `optimize_tools` (bool | dict\[str, bool] | None) — Optional tool optimization selector. Only supported by optimizers that explicitly document tool optimization support.

- `args` (Any) —

- `kwargs` (Any) —

#### post\_baseline

```python
post_baseline(
    context: OptimizationContext,
    score: float
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `score` (float) —

#### post\_optimize

```python
post_optimize(
    context: OptimizationContext,
    result: OptimizationResult
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `result` (OptimizationResult) —

#### post\_round

```python
post_round(
    round_handle: Any,
    context: opik_optimizer.core.state.OptimizationContext | None = None,
    best_score: float | None = None,
    best_candidate: typing.Any | None = None,
    best_prompt: typing.Any | None = None,
    stop_reason: str | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    timestamp: str | None = None,
    dataset_split: str | None = None,
    pareto_front: list[dict[str, typing.Any]] | None = None,
    selection_meta: dict[str, typing.Any] | None = None
)
```

**Parameters:**

- `round_handle` (Any) —

- `context` (opik\_optimizer.core.state.OptimizationContext | None) —

- `best_score` (float | None) —

- `best_candidate` (typing.Any | None) —

- `best_prompt` (typing.Any | None) —

- `stop_reason` (str | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `timestamp` (str | None) —

- `dataset_split` (str | None) —

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

- `selection_meta` (dict\[str, typing.Any] | None) —

#### post\_trial

```python
post_trial(
    context: OptimizationContext,
    candidate_handle: Any,
    score: float | None,
    metrics: dict[str, typing.Any] | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    dataset: str | None = None,
    dataset_split: str | None = None,
    trial_index: int | None = None,
    timestamp: str | None = None,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate_handle` (Any) —

- `score` (float | None) —

- `metrics` (dict\[str, typing.Any] | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `dataset` (str | None) —

- `dataset_split` (str | None) —

- `trial_index` (int | None) —

- `timestamp` (str | None) —

- `round_handle` (typing.Any | None) —

#### pre\_baseline

```python
pre_baseline(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### pre\_optimize

```python
pre_optimize(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### pre\_round

```python
pre_round(
    context: OptimizationContext,
    extras: Any
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `extras` (Any) —

#### pre\_trial

```python
pre_trial(
    context: OptimizationContext,
    candidate: Any,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate` (Any) —

- `round_handle` (typing.Any | None) —

#### record\_candidate\_entry

```python
record_candidate_entry(
    prompt_or_payload: Any,
    score: float | None = None,
    id: str | None = None,
    metrics: dict[str, typing.Any] | None = None,
    notes: str | None = None,
    extra: dict[str, typing.Any] | None = None,
    context: opik_optimizer.core.state.OptimizationContext | None = None
)
```

**Parameters:**

- `prompt_or_payload` (Any) —

- `score` (float | None) —

- `id` (str | None) —

- `metrics` (dict\[str, typing.Any] | None) —

- `notes` (str | None) —

- `extra` (dict\[str, typing.Any] | None) —

- `context` (opik\_optimizer.core.state.OptimizationContext | None) —

#### run\_optimization

```python
run_optimization(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) — The optimization context with prompts, dataset, metric, etc.

#### set\_default\_dataset\_split

```python
set_default_dataset_split(
    dataset_split: str | None
)
```

**Parameters:**

- `dataset_split` (str | None) —

#### set\_pareto\_front

```python
set_pareto_front(
    pareto_front: list[dict[str, typing.Any]] | None
)
```

**Parameters:**

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

#### set\_selection\_meta

```python
set_selection_meta(
    selection_meta: dict[str, typing.Any] | None
)
```

**Parameters:**

- `selection_meta` (dict\[str, typing.Any] | None) —

#### start\_candidate

```python
start_candidate(
    context: OptimizationContext,
    candidate: Any,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate` (Any) —

- `round_handle` (typing.Any | None) —

#### with\_dataset\_split

```python
with_dataset_split(
    dataset_split: str | None
)
```

**Parameters:**

- `dataset_split` (str | None) —

## HierarchicalReflectiveOptimizer

```python
HierarchicalReflectiveOptimizer(
    model: str = 'openai/gpt-5-nano',
    model_parameters: dict[str, typing.Any] | None = None,
    reasoning_model: str | None = None,
    reasoning_model_parameters: dict[str, typing.Any] | None = None,
    max_parallel_batches: int = 5,
    batch_size: int = 25,
    convergence_threshold: float = 0.01,
    n_threads: int = 12,
    verbose: int = 1,
    seed: int = 42,
    name: str | None = None,
    prompt_overrides: dict[str, str] | collections.abc.Callable[[opik_optimizer.utils.prompt_library.PromptLibrary], None] | None = None,
    skip_perfect_score: bool = True,
    perfect_score: float = 0.95
)
```

**Parameters:**

- `model` (str, default: openai/gpt-5-nano) —

- `model_parameters` (dict\[str, typing.Any] | None) —

- `reasoning_model` (str | None) —

- `reasoning_model_parameters` (dict\[str, typing.Any] | None) —

- `max_parallel_batches` (int, default: 5) —

- `batch_size` (int, default: 25) —

- `convergence_threshold` (float, default: 0.01) —

- `n_threads` (int, default: 12) —

- `verbose` (int, default: 1) —

- `seed` (int, default: 42) —

- `name` (str | None) —

- `prompt_overrides` (dict\[str, str] | collections.abc.Callable\[\[opik\_optimizer.utils.prompt\_library.PromptLibrary], None] | None) —

- `skip_perfect_score` (bool, default: True) —

- `perfect_score` (float, default: 0.95) —

### Methods

#### begin\_round

```python
begin_round(
    context: OptimizationContext,
    extras: Any
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `extras` (Any) —

#### cleanup

```python
cleanup()
```

#### evaluate

```python
evaluate(
    context: OptimizationContext,
    prompts: dict,
    experiment_config: dict[str, typing.Any] | None = None,
    sampling_tag: str | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) — Optimization context for this run.

- `prompts` (dict) — Dict of named prompts to evaluate (e.g., {"main": ChatPrompt(...)}). Single-prompt optimizations use a dict with one entry.

- `experiment_config` (dict\[str, typing.Any] | None) — Optional experiment configuration.

- `sampling_tag` (str | None) — Optional sampling tag for deterministic subsampling per candidate.

#### evaluate\_prompt

```python
evaluate_prompt(
    prompt: opik_optimizer.api_objects.chat_prompt.ChatPrompt | dict[str, opik_optimizer.api_objects.chat_prompt.ChatPrompt],
    dataset: Dataset,
    metric: MetricFunction,
    agent: opik_optimizer.agents.optimizable_agent.OptimizableAgent | None = None,
    n_threads: int | None = None,
    verbose: int = 1,
    dataset_item_ids: list[str] | None = None,
    experiment_config: dict | None = None,
    n_samples: int | float | str | None = None,
    n_samples_strategy: str | None = None,
    seed: int | None = None,
    return_evaluation_result: bool = False,
    allow_tool_use: bool | None = None,
    use_evaluate_on_dict_items: bool | None = None,
    sampling_tag: str | None = None
)
```

**Parameters:**

- `prompt` (opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt | dict\[str, opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt]) —

- `dataset` (Dataset) —

- `metric` (MetricFunction) —

- `agent` (opik\_optimizer.agents.optimizable\_agent.OptimizableAgent | None) —

- `n_threads` (int | None) —

- `verbose` (int, default: 1) —

- `dataset_item_ids` (list\[str] | None) —

- `experiment_config` (dict | None) —

- `n_samples` (int | float | str | None) —

- `n_samples_strategy` (str | None) —

- `seed` (int | None) —

- `return_evaluation_result` (bool, default: False) —

- `allow_tool_use` (bool | None) —

- `use_evaluate_on_dict_items` (bool | None) —

- `sampling_tag` (str | None) —

#### evaluate\_with\_result

```python
evaluate_with_result(
    context: OptimizationContext,
    prompts: dict,
    experiment_config: dict[str, typing.Any] | None = None,
    empty_score: float | None = None,
    n_samples: int | float | str | None = None,
    n_samples_strategy: str | None = None,
    sampling_tag: str | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `prompts` (dict) —

- `experiment_config` (dict\[str, typing.Any] | None) —

- `empty_score` (float | None) —

- `n_samples` (int | float | str | None) —

- `n_samples_strategy` (str | None) —

- `sampling_tag` (str | None) —

#### finish\_candidate

```python
finish_candidate(
    context: OptimizationContext,
    candidate_handle: Any,
    score: float | None,
    metrics: dict[str, typing.Any] | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    dataset: str | None = None,
    dataset_split: str | None = None,
    trial_index: int | None = None,
    timestamp: str | None = None,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate_handle` (Any) —

- `score` (float | None) —

- `metrics` (dict\[str, typing.Any] | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `dataset` (str | None) —

- `dataset_split` (str | None) —

- `trial_index` (int | None) —

- `timestamp` (str | None) —

- `round_handle` (typing.Any | None) —

#### finish\_round

```python
finish_round(
    round_handle: Any,
    context: opik_optimizer.core.state.OptimizationContext | None = None,
    best_score: float | None = None,
    best_candidate: typing.Any | None = None,
    best_prompt: typing.Any | None = None,
    stop_reason: str | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    timestamp: str | None = None,
    dataset_split: str | None = None,
    pareto_front: list[dict[str, typing.Any]] | None = None,
    selection_meta: dict[str, typing.Any] | None = None
)
```

**Parameters:**

- `round_handle` (Any) —

- `context` (opik\_optimizer.core.state.OptimizationContext | None) —

- `best_score` (float | None) —

- `best_candidate` (typing.Any | None) —

- `best_prompt` (typing.Any | None) —

- `stop_reason` (str | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `timestamp` (str | None) —

- `dataset_split` (str | None) —

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

- `selection_meta` (dict\[str, typing.Any] | None) —

#### get\_config

```python
get_config(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### get\_default\_prompt

```python
get_default_prompt(
    key: str
)
```

**Parameters:**

- `key` (str) — The prompt key to retrieve

#### get\_history\_entries

```python
get_history_entries()
```

#### get\_history\_rounds

```python
get_history_rounds()
```

#### get\_metadata

```python
get_metadata(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### get\_optimizer\_metadata

```python
get_optimizer_metadata()
```

#### get\_prompt

```python
get_prompt(
    key: str,
    fmt: Any
)
```

**Parameters:**

- `key` (str) — The prompt key to retrieve

- `fmt` (Any) —

#### list\_prompts

```python
list_prompts()
```

#### on\_trial

```python
on_trial(
    context: OptimizationContext,
    prompts: dict,
    score: float,
    prev_best_score: float | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `prompts` (dict) —

- `score` (float) —

- `prev_best_score` (float | None) —

#### optimize\_mcp

```python
optimize_mcp(
    args: Any,
    kwargs: Any
)
```

**Parameters:**

- `args` (Any) —

- `kwargs` (Any) —

#### optimize\_prompt

```python
optimize_prompt(
    prompt: opik_optimizer.api_objects.chat_prompt.ChatPrompt | dict[str, opik_optimizer.api_objects.chat_prompt.ChatPrompt],
    dataset: Dataset,
    metric: MetricFunction,
    agent: opik_optimizer.agents.optimizable_agent.OptimizableAgent | None = None,
    experiment_config: dict | None = None,
    n_samples: int | float | str | None = None,
    n_samples_minibatch: int | None = None,
    n_samples_strategy: str | None = None,
    auto_continue: bool = False,
    project_name: str | None = None,
    optimization_id: str | None = None,
    validation_dataset: opik.api_objects.dataset.dataset.Dataset | None = None,
    max_trials: int = 10,
    allow_tool_use: bool = True,
    optimize_prompts: bool | str | list[str] | None = 'system',
    optimize_tools: bool | dict[str, bool] | None = None,
    args: Any,
    kwargs: Any
)
```

**Parameters:**

- `prompt` (opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt | dict\[str, opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt]) — The prompt to optimize (single ChatPrompt or dict of prompts)

- `dataset` (Dataset) — Opik dataset (training set - used for feedback/context) TODO/FIXME: This parameter will be deprecated in favor of dataset\_training. For now, it serves as the training dataset parameter.

- `metric` (MetricFunction) — A metric function with signature (dataset\_item, llm\_output) -> float

- `agent` (opik\_optimizer.agents.optimizable\_agent.OptimizableAgent | None) — Optional agent for prompt execution (defaults to LiteLLMAgent)

- `experiment_config` (dict | None) — Optional configuration for the experiment

- `n_samples` (int | float | str | None) — Number of samples to use for evaluation

- `n_samples_minibatch` (int | None) — Optional number of samples for inner-loop minibatches

- `n_samples_strategy` (str | None) — Sampling strategy name (default "random\_sorted")

- `auto_continue` (bool, default: False) — Whether to continue optimization automatically

- `project_name` (str | None) — Opik project name for logging traces (defaults to OPIK\_PROJECT\_NAME env or "Optimization")

- `optimization_id` (str | None) — Optional ID to use when creating the Opik optimization run

- `validation_dataset` (opik.api\_objects.dataset.dataset.Dataset | None) — Optional validation dataset for ranking candidates

- `max_trials` (int, default: 10) — Maximum number of optimization trials

- `allow_tool_use` (bool, default: True) — Whether tools may be executed during evaluation (default True)

- `optimize_prompts` (bool | str | list\[str] | None, default: system) — Which prompt roles to allow for optimization

- `optimize_tools` (bool | dict\[str, bool] | None) — Optional tool optimization selector. Only supported by optimizers that explicitly document tool optimization support.

- `args` (Any) —

- `kwargs` (Any) —

#### post\_baseline

```python
post_baseline(
    context: OptimizationContext,
    score: float
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `score` (float) —

#### post\_optimize

```python
post_optimize(
    context: OptimizationContext,
    result: OptimizationResult
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `result` (OptimizationResult) —

#### post\_round

```python
post_round(
    round_handle: Any,
    context: opik_optimizer.core.state.OptimizationContext | None = None,
    best_score: float | None = None,
    best_candidate: typing.Any | None = None,
    best_prompt: typing.Any | None = None,
    stop_reason: str | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    timestamp: str | None = None,
    dataset_split: str | None = None,
    pareto_front: list[dict[str, typing.Any]] | None = None,
    selection_meta: dict[str, typing.Any] | None = None
)
```

**Parameters:**

- `round_handle` (Any) —

- `context` (opik\_optimizer.core.state.OptimizationContext | None) —

- `best_score` (float | None) —

- `best_candidate` (typing.Any | None) —

- `best_prompt` (typing.Any | None) —

- `stop_reason` (str | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `timestamp` (str | None) —

- `dataset_split` (str | None) —

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

- `selection_meta` (dict\[str, typing.Any] | None) —

#### post\_trial

```python
post_trial(
    context: OptimizationContext,
    candidate_handle: Any,
    score: float | None,
    metrics: dict[str, typing.Any] | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    dataset: str | None = None,
    dataset_split: str | None = None,
    trial_index: int | None = None,
    timestamp: str | None = None,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate_handle` (Any) —

- `score` (float | None) —

- `metrics` (dict\[str, typing.Any] | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `dataset` (str | None) —

- `dataset_split` (str | None) —

- `trial_index` (int | None) —

- `timestamp` (str | None) —

- `round_handle` (typing.Any | None) —

#### pre\_baseline

```python
pre_baseline(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### pre\_optimize

```python
pre_optimize(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) — The optimization context

#### pre\_round

```python
pre_round(
    context: OptimizationContext,
    extras: Any
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `extras` (Any) —

#### pre\_trial

```python
pre_trial(
    context: OptimizationContext,
    candidate: Any,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate` (Any) —

- `round_handle` (typing.Any | None) —

#### record\_candidate\_entry

```python
record_candidate_entry(
    prompt_or_payload: Any,
    score: float | None = None,
    id: str | None = None,
    metrics: dict[str, typing.Any] | None = None,
    notes: str | None = None,
    extra: dict[str, typing.Any] | None = None,
    context: opik_optimizer.core.state.OptimizationContext | None = None
)
```

**Parameters:**

- `prompt_or_payload` (Any) —

- `score` (float | None) —

- `id` (str | None) —

- `metrics` (dict\[str, typing.Any] | None) —

- `notes` (str | None) —

- `extra` (dict\[str, typing.Any] | None) —

- `context` (opik\_optimizer.core.state.OptimizationContext | None) —

#### run\_optimization

```python
run_optimization(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) — The optimization context with prompts, dataset, metric, etc.

#### set\_default\_dataset\_split

```python
set_default_dataset_split(
    dataset_split: str | None
)
```

**Parameters:**

- `dataset_split` (str | None) —

#### set\_pareto\_front

```python
set_pareto_front(
    pareto_front: list[dict[str, typing.Any]] | None
)
```

**Parameters:**

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

#### set\_selection\_meta

```python
set_selection_meta(
    selection_meta: dict[str, typing.Any] | None
)
```

**Parameters:**

- `selection_meta` (dict\[str, typing.Any] | None) —

#### start\_candidate

```python
start_candidate(
    context: OptimizationContext,
    candidate: Any,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate` (Any) —

- `round_handle` (typing.Any | None) —

#### with\_dataset\_split

```python
with_dataset_split(
    dataset_split: str | None
)
```

**Parameters:**

- `dataset_split` (str | None) —

## ParameterOptimizer

```python
ParameterOptimizer(
    model: str = 'openai/gpt-5-nano',
    model_parameters: dict[str, typing.Any] | None = None,
    default_n_trials: int = 20,
    local_search_ratio: float = 0.3,
    local_search_scale: float = 0.2,
    n_threads: int = 12,
    verbose: int = 1,
    seed: int = 42,
    name: str | None = None,
    skip_perfect_score: bool = True,
    perfect_score: float = 0.95
)
```

**Parameters:**

- `model` (str, default: openai/gpt-5-nano) —

- `model_parameters` (dict\[str, typing.Any] | None) —

- `default_n_trials` (int, default: 20) —

- `local_search_ratio` (float, default: 0.3) —

- `local_search_scale` (float, default: 0.2) —

- `n_threads` (int, default: 12) —

- `verbose` (int, default: 1) —

- `seed` (int, default: 42) —

- `name` (str | None) —

- `skip_perfect_score` (bool, default: True) —

- `perfect_score` (float, default: 0.95) —

### Methods

#### begin\_round

```python
begin_round(
    context: OptimizationContext,
    extras: Any
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `extras` (Any) —

#### cleanup

```python
cleanup()
```

#### evaluate

```python
evaluate(
    context: OptimizationContext,
    prompts: dict,
    experiment_config: dict[str, typing.Any] | None = None,
    sampling_tag: str | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) — Optimization context for this run.

- `prompts` (dict) — Dict of named prompts to evaluate (e.g., {"main": ChatPrompt(...)}). Single-prompt optimizations use a dict with one entry.

- `experiment_config` (dict\[str, typing.Any] | None) — Optional experiment configuration.

- `sampling_tag` (str | None) — Optional sampling tag for deterministic subsampling per candidate.

#### evaluate\_prompt

```python
evaluate_prompt(
    prompt: opik_optimizer.api_objects.chat_prompt.ChatPrompt | dict[str, opik_optimizer.api_objects.chat_prompt.ChatPrompt],
    dataset: Dataset,
    metric: MetricFunction,
    agent: opik_optimizer.agents.optimizable_agent.OptimizableAgent | None = None,
    n_threads: int | None = None,
    verbose: int = 1,
    dataset_item_ids: list[str] | None = None,
    experiment_config: dict | None = None,
    n_samples: int | float | str | None = None,
    n_samples_strategy: str | None = None,
    seed: int | None = None,
    return_evaluation_result: bool = False,
    allow_tool_use: bool | None = None,
    use_evaluate_on_dict_items: bool | None = None,
    sampling_tag: str | None = None
)
```

**Parameters:**

- `prompt` (opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt | dict\[str, opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt]) —

- `dataset` (Dataset) —

- `metric` (MetricFunction) —

- `agent` (opik\_optimizer.agents.optimizable\_agent.OptimizableAgent | None) —

- `n_threads` (int | None) —

- `verbose` (int, default: 1) —

- `dataset_item_ids` (list\[str] | None) —

- `experiment_config` (dict | None) —

- `n_samples` (int | float | str | None) —

- `n_samples_strategy` (str | None) —

- `seed` (int | None) —

- `return_evaluation_result` (bool, default: False) —

- `allow_tool_use` (bool | None) —

- `use_evaluate_on_dict_items` (bool | None) —

- `sampling_tag` (str | None) —

#### evaluate\_with\_result

```python
evaluate_with_result(
    context: OptimizationContext,
    prompts: dict,
    experiment_config: dict[str, typing.Any] | None = None,
    empty_score: float | None = None,
    n_samples: int | float | str | None = None,
    n_samples_strategy: str | None = None,
    sampling_tag: str | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `prompts` (dict) —

- `experiment_config` (dict\[str, typing.Any] | None) —

- `empty_score` (float | None) —

- `n_samples` (int | float | str | None) —

- `n_samples_strategy` (str | None) —

- `sampling_tag` (str | None) —

#### finish\_candidate

```python
finish_candidate(
    context: OptimizationContext,
    candidate_handle: Any,
    score: float | None,
    metrics: dict[str, typing.Any] | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    dataset: str | None = None,
    dataset_split: str | None = None,
    trial_index: int | None = None,
    timestamp: str | None = None,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate_handle` (Any) —

- `score` (float | None) —

- `metrics` (dict\[str, typing.Any] | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `dataset` (str | None) —

- `dataset_split` (str | None) —

- `trial_index` (int | None) —

- `timestamp` (str | None) —

- `round_handle` (typing.Any | None) —

#### finish\_round

```python
finish_round(
    round_handle: Any,
    context: opik_optimizer.core.state.OptimizationContext | None = None,
    best_score: float | None = None,
    best_candidate: typing.Any | None = None,
    best_prompt: typing.Any | None = None,
    stop_reason: str | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    timestamp: str | None = None,
    dataset_split: str | None = None,
    pareto_front: list[dict[str, typing.Any]] | None = None,
    selection_meta: dict[str, typing.Any] | None = None
)
```

**Parameters:**

- `round_handle` (Any) —

- `context` (opik\_optimizer.core.state.OptimizationContext | None) —

- `best_score` (float | None) —

- `best_candidate` (typing.Any | None) —

- `best_prompt` (typing.Any | None) —

- `stop_reason` (str | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `timestamp` (str | None) —

- `dataset_split` (str | None) —

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

- `selection_meta` (dict\[str, typing.Any] | None) —

#### get\_config

```python
get_config(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### get\_default\_prompt

```python
get_default_prompt(
    key: str
)
```

**Parameters:**

- `key` (str) — The prompt key to retrieve

#### get\_history\_entries

```python
get_history_entries()
```

#### get\_history\_rounds

```python
get_history_rounds()
```

#### get\_metadata

```python
get_metadata(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### get\_optimizer\_metadata

```python
get_optimizer_metadata()
```

#### get\_prompt

```python
get_prompt(
    key: str,
    fmt: Any
)
```

**Parameters:**

- `key` (str) — The prompt key to retrieve

- `fmt` (Any) —

#### list\_prompts

```python
list_prompts()
```

#### on\_trial

```python
on_trial(
    context: OptimizationContext,
    prompts: dict,
    score: float,
    prev_best_score: float | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `prompts` (dict) —

- `score` (float) —

- `prev_best_score` (float | None) —

#### optimize\_mcp

```python
optimize_mcp(
    args: Any,
    kwargs: Any
)
```

**Parameters:**

- `args` (Any) —

- `kwargs` (Any) —

#### optimize\_parameter

```python
optimize_parameter(
    prompt: opik_optimizer.api_objects.chat_prompt.ChatPrompt | dict[str, opik_optimizer.api_objects.chat_prompt.ChatPrompt],
    dataset: Dataset,
    metric: MetricFunction,
    parameter_space: opik_optimizer.algorithms.parameter_optimizer.ops.search_ops.ParameterSearchSpace | collections.abc.Mapping[str, typing.Any],
    validation_dataset: opik.api_objects.dataset.dataset.Dataset | None = None,
    experiment_config: dict | None = None,
    max_trials: int | None = None,
    n_samples: int | float | str | None = None,
    n_samples_minibatch: int | None = None,
    n_samples_strategy: str | None = None,
    agent: opik_optimizer.agents.optimizable_agent.OptimizableAgent | None = None,
    project_name: str = 'Optimization',
    sampler: optuna.samplers._base.BaseSampler | None = None,
    callbacks: list[collections.abc.Callable[[optuna.study.study.Study, optuna.trial._frozen.FrozenTrial], None]] | None = None,
    timeout: float | None = None,
    local_trials: int | None = None,
    local_search_scale: float | None = None,
    optimization_id: str | None = None
)
```

**Parameters:**

- `prompt` (opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt | dict\[str, opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt]) — The prompt or dict of prompts to evaluate with tuned parameters. When a dict is provided, parameters are optimized independently for each prompt.

- `dataset` (Dataset) — Dataset providing evaluation examples

- `metric` (MetricFunction) — Objective function to maximize

- `parameter_space` (opik\_optimizer.algorithms.parameter\_optimizer.ops.search\_ops.ParameterSearchSpace | collections.abc.Mapping\[str, typing.Any]) — Definition of the search space for tunable parameters. For multi-prompt, params without a prefix are expanded per prompt. Params already prefixed (e.g., 'analyze.temperature') are kept as-is.

- `validation_dataset` (opik.api\_objects.dataset.dataset.Dataset | None) — Optional validation dataset. Note: Due to the internal implementation of ParameterOptimizer, this parameter is currently not fully utilized and we recommend not using it for this optimizer.

- `experiment_config` (dict | None) — Optional experiment metadata

- `max_trials` (int | None) — Total number of trials (if None, uses default\_n\_trials)

- `n_samples` (int | float | str | None) — Number of dataset samples to evaluate per trial (None for all)

- `n_samples_minibatch` (int | None) — Optional number of samples for inner-loop minibatches

- `n_samples_strategy` (str | None) — Sampling strategy name (default "random\_sorted")

- `agent` (opik\_optimizer.agents.optimizable\_agent.OptimizableAgent | None) — Optional custom agent instance to execute evaluations

- `project_name` (str, default: Optimization) — Opik project name for logging traces (default: "Optimization")

- `sampler` (optuna.samplers.\_base.BaseSampler | None) — Optuna sampler to use (default: TPESampler with seed)

- `callbacks` (list\[collections.abc.Callable\[\[optuna.study.study.Study, optuna.trial.\_frozen.FrozenTrial], None]] | None) — List of callback functions for Optuna study

- `timeout` (float | None) — Maximum time in seconds for optimization

- `local_trials` (int | None) — Number of trials for local search (overrides local\_search\_ratio)

- `local_search_scale` (float | None) — Scale factor for local search narrowing (0.0-1.0)

- `optimization_id` (str | None) — Optional ID to use when creating the Opik optimization run; when provided it must be a valid UUIDv7 string.

#### post\_baseline

```python
post_baseline(
    context: OptimizationContext,
    score: float
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `score` (float) —

#### post\_optimize

```python
post_optimize(
    context: OptimizationContext,
    result: OptimizationResult
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `result` (OptimizationResult) —

#### post\_round

```python
post_round(
    round_handle: Any,
    context: opik_optimizer.core.state.OptimizationContext | None = None,
    best_score: float | None = None,
    best_candidate: typing.Any | None = None,
    best_prompt: typing.Any | None = None,
    stop_reason: str | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    timestamp: str | None = None,
    dataset_split: str | None = None,
    pareto_front: list[dict[str, typing.Any]] | None = None,
    selection_meta: dict[str, typing.Any] | None = None
)
```

**Parameters:**

- `round_handle` (Any) —

- `context` (opik\_optimizer.core.state.OptimizationContext | None) —

- `best_score` (float | None) —

- `best_candidate` (typing.Any | None) —

- `best_prompt` (typing.Any | None) —

- `stop_reason` (str | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `timestamp` (str | None) —

- `dataset_split` (str | None) —

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

- `selection_meta` (dict\[str, typing.Any] | None) —

#### post\_trial

```python
post_trial(
    context: OptimizationContext,
    candidate_handle: Any,
    score: float | None,
    metrics: dict[str, typing.Any] | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    dataset: str | None = None,
    dataset_split: str | None = None,
    trial_index: int | None = None,
    timestamp: str | None = None,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate_handle` (Any) —

- `score` (float | None) —

- `metrics` (dict\[str, typing.Any] | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `dataset` (str | None) —

- `dataset_split` (str | None) —

- `trial_index` (int | None) —

- `timestamp` (str | None) —

- `round_handle` (typing.Any | None) —

#### pre\_baseline

```python
pre_baseline(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### pre\_optimize

```python
pre_optimize(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) — The optimization context

#### pre\_round

```python
pre_round(
    context: OptimizationContext,
    extras: Any
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `extras` (Any) —

#### pre\_trial

```python
pre_trial(
    context: OptimizationContext,
    candidate: Any,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate` (Any) —

- `round_handle` (typing.Any | None) —

#### record\_candidate\_entry

```python
record_candidate_entry(
    prompt_or_payload: Any,
    score: float | None = None,
    id: str | None = None,
    metrics: dict[str, typing.Any] | None = None,
    notes: str | None = None,
    extra: dict[str, typing.Any] | None = None,
    context: opik_optimizer.core.state.OptimizationContext | None = None
)
```

**Parameters:**

- `prompt_or_payload` (Any) —

- `score` (float | None) —

- `id` (str | None) —

- `metrics` (dict\[str, typing.Any] | None) —

- `notes` (str | None) —

- `extra` (dict\[str, typing.Any] | None) —

- `context` (opik\_optimizer.core.state.OptimizationContext | None) —

#### set\_default\_dataset\_split

```python
set_default_dataset_split(
    dataset_split: str | None
)
```

**Parameters:**

- `dataset_split` (str | None) —

#### set\_pareto\_front

```python
set_pareto_front(
    pareto_front: list[dict[str, typing.Any]] | None
)
```

**Parameters:**

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

#### set\_selection\_meta

```python
set_selection_meta(
    selection_meta: dict[str, typing.Any] | None
)
```

**Parameters:**

- `selection_meta` (dict\[str, typing.Any] | None) —

#### start\_candidate

```python
start_candidate(
    context: OptimizationContext,
    candidate: Any,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate` (Any) —

- `round_handle` (typing.Any | None) —

#### with\_dataset\_split

```python
with_dataset_split(
    dataset_split: str | None
)
```

**Parameters:**

- `dataset_split` (str | None) —

## ParameterSearchSpace

```python
ParameterSearchSpace(
    parameters: list[opik_optimizer.algorithms.parameter_optimizer.ops.search_ops.ParameterSpec] = PydanticUndefined
)
```

**Parameters:**

- `parameters` (list\[opik\_optimizer.algorithms.parameter\_optimizer.ops.search\_ops.ParameterSpec], default: PydanticUndefined) —

## ParameterSpec

```python
ParameterSpec(
    name: <class 'str'>,
    description: str | None = None,
    distribution: <enum 'ParameterType'>,
    low: float | None = None,
    high: float | None = None,
    step: float | None = None,
    scale: Literal['linear', 'log'] = 'linear',
    choices: list[Any] | None = None,
    target: str | collections.abc.Sequence[str] | None = None,
    default: Any | None = None
)
```

**Parameters:**

- `name` (\<class 'str'>, default: PydanticUndefined) —

- `description` (str | None) —

- `distribution` (\<enum 'ParameterType'>, default: PydanticUndefined) —

- `low` (float | None) —

- `high` (float | None) —

- `step` (float | None) —

- `scale` (Literal\['linear', 'log'], default: linear) —

- `choices` (list\[Any] | None) —

- `target` (str | collections.abc.Sequence\[str] | None) —

- `default` (Any | None) —

## ParameterType

```python
ParameterType(
    args: Any,
    kwds: Any
)
```

**Parameters:**

- `args` (Any) —

- `kwds` (Any) —

## BaseOptimizer

```python
BaseOptimizer(
    model: str,
    verbose: int = 1,
    seed: int = 42,
    model_parameters: dict[str, typing.Any] | None = None,
    reasoning_model: str | None = None,
    reasoning_model_parameters: dict[str, typing.Any] | None = None,
    name: str | None = None,
    skip_perfect_score: bool = True,
    perfect_score: float = 0.95,
    prompt_overrides: dict[str, str] | collections.abc.Callable[[opik_optimizer.utils.prompt_library.PromptLibrary], None] | None = None,
    display: opik_optimizer.utils.display.run.RunDisplay | None = None
)
```

**Parameters:**

- `model` (str) —

- `verbose` (int, default: 1) —

- `seed` (int, default: 42) —

- `model_parameters` (dict\[str, typing.Any] | None) —

- `reasoning_model` (str | None) —

- `reasoning_model_parameters` (dict\[str, typing.Any] | None) —

- `name` (str | None) —

- `skip_perfect_score` (bool, default: True) —

- `perfect_score` (float, default: 0.95) —

- `prompt_overrides` (dict\[str, str] | collections.abc.Callable\[\[opik\_optimizer.utils.prompt\_library.PromptLibrary], None] | None) —

- `display` (opik\_optimizer.utils.display.run.RunDisplay | None) —

### Methods

#### begin\_round

```python
begin_round(
    context: OptimizationContext,
    extras: Any
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `extras` (Any) —

#### cleanup

```python
cleanup()
```

#### evaluate

```python
evaluate(
    context: OptimizationContext,
    prompts: dict,
    experiment_config: dict[str, typing.Any] | None = None,
    sampling_tag: str | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) — Optimization context for this run.

- `prompts` (dict) — Dict of named prompts to evaluate (e.g., {"main": ChatPrompt(...)}). Single-prompt optimizations use a dict with one entry.

- `experiment_config` (dict\[str, typing.Any] | None) — Optional experiment configuration.

- `sampling_tag` (str | None) — Optional sampling tag for deterministic subsampling per candidate.

#### evaluate\_prompt

```python
evaluate_prompt(
    prompt: opik_optimizer.api_objects.chat_prompt.ChatPrompt | dict[str, opik_optimizer.api_objects.chat_prompt.ChatPrompt],
    dataset: Dataset,
    metric: MetricFunction,
    agent: opik_optimizer.agents.optimizable_agent.OptimizableAgent | None = None,
    n_threads: int | None = None,
    verbose: int = 1,
    dataset_item_ids: list[str] | None = None,
    experiment_config: dict | None = None,
    n_samples: int | float | str | None = None,
    n_samples_strategy: str | None = None,
    seed: int | None = None,
    return_evaluation_result: bool = False,
    allow_tool_use: bool | None = None,
    use_evaluate_on_dict_items: bool | None = None,
    sampling_tag: str | None = None
)
```

**Parameters:**

- `prompt` (opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt | dict\[str, opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt]) —

- `dataset` (Dataset) —

- `metric` (MetricFunction) —

- `agent` (opik\_optimizer.agents.optimizable\_agent.OptimizableAgent | None) —

- `n_threads` (int | None) —

- `verbose` (int, default: 1) —

- `dataset_item_ids` (list\[str] | None) —

- `experiment_config` (dict | None) —

- `n_samples` (int | float | str | None) —

- `n_samples_strategy` (str | None) —

- `seed` (int | None) —

- `return_evaluation_result` (bool, default: False) —

- `allow_tool_use` (bool | None) —

- `use_evaluate_on_dict_items` (bool | None) —

- `sampling_tag` (str | None) —

#### evaluate\_with\_result

```python
evaluate_with_result(
    context: OptimizationContext,
    prompts: dict,
    experiment_config: dict[str, typing.Any] | None = None,
    empty_score: float | None = None,
    n_samples: int | float | str | None = None,
    n_samples_strategy: str | None = None,
    sampling_tag: str | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `prompts` (dict) —

- `experiment_config` (dict\[str, typing.Any] | None) —

- `empty_score` (float | None) —

- `n_samples` (int | float | str | None) —

- `n_samples_strategy` (str | None) —

- `sampling_tag` (str | None) —

#### finish\_candidate

```python
finish_candidate(
    context: OptimizationContext,
    candidate_handle: Any,
    score: float | None,
    metrics: dict[str, typing.Any] | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    dataset: str | None = None,
    dataset_split: str | None = None,
    trial_index: int | None = None,
    timestamp: str | None = None,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate_handle` (Any) —

- `score` (float | None) —

- `metrics` (dict\[str, typing.Any] | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `dataset` (str | None) —

- `dataset_split` (str | None) —

- `trial_index` (int | None) —

- `timestamp` (str | None) —

- `round_handle` (typing.Any | None) —

#### finish\_round

```python
finish_round(
    round_handle: Any,
    context: opik_optimizer.core.state.OptimizationContext | None = None,
    best_score: float | None = None,
    best_candidate: typing.Any | None = None,
    best_prompt: typing.Any | None = None,
    stop_reason: str | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    timestamp: str | None = None,
    dataset_split: str | None = None,
    pareto_front: list[dict[str, typing.Any]] | None = None,
    selection_meta: dict[str, typing.Any] | None = None
)
```

**Parameters:**

- `round_handle` (Any) —

- `context` (opik\_optimizer.core.state.OptimizationContext | None) —

- `best_score` (float | None) —

- `best_candidate` (typing.Any | None) —

- `best_prompt` (typing.Any | None) —

- `stop_reason` (str | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `timestamp` (str | None) —

- `dataset_split` (str | None) —

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

- `selection_meta` (dict\[str, typing.Any] | None) —

#### get\_config

```python
get_config(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### get\_default\_prompt

```python
get_default_prompt(
    key: str
)
```

**Parameters:**

- `key` (str) — The prompt key to retrieve

#### get\_history\_entries

```python
get_history_entries()
```

#### get\_history\_rounds

```python
get_history_rounds()
```

#### get\_metadata

```python
get_metadata(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### get\_prompt

```python
get_prompt(
    key: str,
    fmt: Any
)
```

**Parameters:**

- `key` (str) — The prompt key to retrieve

- `fmt` (Any) —

#### list\_prompts

```python
list_prompts()
```

#### on\_trial

```python
on_trial(
    context: OptimizationContext,
    prompts: dict,
    score: float,
    prev_best_score: float | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `prompts` (dict) —

- `score` (float) —

- `prev_best_score` (float | None) —

#### optimize\_mcp

```python
optimize_mcp(
    args: Any,
    kwargs: Any
)
```

**Parameters:**

- `args` (Any) —

- `kwargs` (Any) —

#### optimize\_prompt

```python
optimize_prompt(
    prompt: opik_optimizer.api_objects.chat_prompt.ChatPrompt | dict[str, opik_optimizer.api_objects.chat_prompt.ChatPrompt],
    dataset: Dataset,
    metric: MetricFunction,
    agent: opik_optimizer.agents.optimizable_agent.OptimizableAgent | None = None,
    experiment_config: dict | None = None,
    n_samples: int | float | str | None = None,
    n_samples_minibatch: int | None = None,
    n_samples_strategy: str | None = None,
    auto_continue: bool = False,
    project_name: str | None = None,
    optimization_id: str | None = None,
    validation_dataset: opik.api_objects.dataset.dataset.Dataset | None = None,
    max_trials: int = 10,
    allow_tool_use: bool = True,
    optimize_prompts: bool | str | list[str] | None = 'system',
    optimize_tools: bool | dict[str, bool] | None = None,
    args: Any,
    kwargs: Any
)
```

**Parameters:**

- `prompt` (opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt | dict\[str, opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt]) — The prompt to optimize (single ChatPrompt or dict of prompts)

- `dataset` (Dataset) — Opik dataset (training set - used for feedback/context) TODO/FIXME: This parameter will be deprecated in favor of dataset\_training. For now, it serves as the training dataset parameter.

- `metric` (MetricFunction) — A metric function with signature (dataset\_item, llm\_output) -> float

- `agent` (opik\_optimizer.agents.optimizable\_agent.OptimizableAgent | None) — Optional agent for prompt execution (defaults to LiteLLMAgent)

- `experiment_config` (dict | None) — Optional configuration for the experiment

- `n_samples` (int | float | str | None) — Number of samples to use for evaluation

- `n_samples_minibatch` (int | None) — Optional number of samples for inner-loop minibatches

- `n_samples_strategy` (str | None) — Sampling strategy name (default "random\_sorted")

- `auto_continue` (bool, default: False) — Whether to continue optimization automatically

- `project_name` (str | None) — Opik project name for logging traces (defaults to OPIK\_PROJECT\_NAME env or "Optimization")

- `optimization_id` (str | None) — Optional ID to use when creating the Opik optimization run

- `validation_dataset` (opik.api\_objects.dataset.dataset.Dataset | None) — Optional validation dataset for ranking candidates

- `max_trials` (int, default: 10) — Maximum number of optimization trials

- `allow_tool_use` (bool, default: True) — Whether tools may be executed during evaluation (default True)

- `optimize_prompts` (bool | str | list\[str] | None, default: system) — Which prompt roles to allow for optimization

- `optimize_tools` (bool | dict\[str, bool] | None) — Optional tool optimization selector. Only supported by optimizers that explicitly document tool optimization support.

- `args` (Any) —

- `kwargs` (Any) —

#### post\_baseline

```python
post_baseline(
    context: OptimizationContext,
    score: float
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `score` (float) —

#### post\_optimize

```python
post_optimize(
    context: OptimizationContext,
    result: OptimizationResult
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `result` (OptimizationResult) —

#### post\_round

```python
post_round(
    round_handle: Any,
    context: opik_optimizer.core.state.OptimizationContext | None = None,
    best_score: float | None = None,
    best_candidate: typing.Any | None = None,
    best_prompt: typing.Any | None = None,
    stop_reason: str | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    timestamp: str | None = None,
    dataset_split: str | None = None,
    pareto_front: list[dict[str, typing.Any]] | None = None,
    selection_meta: dict[str, typing.Any] | None = None
)
```

**Parameters:**

- `round_handle` (Any) —

- `context` (opik\_optimizer.core.state.OptimizationContext | None) —

- `best_score` (float | None) —

- `best_candidate` (typing.Any | None) —

- `best_prompt` (typing.Any | None) —

- `stop_reason` (str | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `timestamp` (str | None) —

- `dataset_split` (str | None) —

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

- `selection_meta` (dict\[str, typing.Any] | None) —

#### post\_trial

```python
post_trial(
    context: OptimizationContext,
    candidate_handle: Any,
    score: float | None,
    metrics: dict[str, typing.Any] | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    dataset: str | None = None,
    dataset_split: str | None = None,
    trial_index: int | None = None,
    timestamp: str | None = None,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate_handle` (Any) —

- `score` (float | None) —

- `metrics` (dict\[str, typing.Any] | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `dataset` (str | None) —

- `dataset_split` (str | None) —

- `trial_index` (int | None) —

- `timestamp` (str | None) —

- `round_handle` (typing.Any | None) —

#### pre\_baseline

```python
pre_baseline(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) —

#### pre\_optimize

```python
pre_optimize(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) — The optimization context

#### pre\_round

```python
pre_round(
    context: OptimizationContext,
    extras: Any
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `extras` (Any) —

#### pre\_trial

```python
pre_trial(
    context: OptimizationContext,
    candidate: Any,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate` (Any) —

- `round_handle` (typing.Any | None) —

#### record\_candidate\_entry

```python
record_candidate_entry(
    prompt_or_payload: Any,
    score: float | None = None,
    id: str | None = None,
    metrics: dict[str, typing.Any] | None = None,
    notes: str | None = None,
    extra: dict[str, typing.Any] | None = None,
    context: opik_optimizer.core.state.OptimizationContext | None = None
)
```

**Parameters:**

- `prompt_or_payload` (Any) —

- `score` (float | None) —

- `id` (str | None) —

- `metrics` (dict\[str, typing.Any] | None) —

- `notes` (str | None) —

- `extra` (dict\[str, typing.Any] | None) —

- `context` (opik\_optimizer.core.state.OptimizationContext | None) —

#### run\_optimization

```python
run_optimization(
    context: OptimizationContext
)
```

**Parameters:**

- `context` (OptimizationContext) — The optimization context with prompts, dataset, metric, etc.

#### set\_default\_dataset\_split

```python
set_default_dataset_split(
    dataset_split: str | None
)
```

**Parameters:**

- `dataset_split` (str | None) —

#### set\_pareto\_front

```python
set_pareto_front(
    pareto_front: list[dict[str, typing.Any]] | None
)
```

**Parameters:**

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

#### set\_selection\_meta

```python
set_selection_meta(
    selection_meta: dict[str, typing.Any] | None
)
```

**Parameters:**

- `selection_meta` (dict\[str, typing.Any] | None) —

#### start\_candidate

```python
start_candidate(
    context: OptimizationContext,
    candidate: Any,
    round_handle: typing.Any | None = None
)
```

**Parameters:**

- `context` (OptimizationContext) —

- `candidate` (Any) —

- `round_handle` (typing.Any | None) —

#### with\_dataset\_split

```python
with_dataset_split(
    dataset_split: str | None
)
```

**Parameters:**

- `dataset_split` (str | None) —

## ChatPrompt

```python
ChatPrompt(
    name: str = 'chat-prompt',
    system: str | None = None,
    user: str | None = None,
    messages: list[dict[str, typing.Any]] | None = None,
    tools: list[dict[str, typing.Any]] | collections.abc.Mapping[str, typing.Any] | None = None,
    function_map: collections.abc.Mapping[str, collections.abc.Callable[..., typing.Any]] | None = None,
    model: str = 'openai/gpt-5-nano',
    model_parameters: dict[str, typing.Any] | None = None,
    model_kwargs: dict[str, typing.Any] | None = None,
    kwargs: Any
)
```

**Parameters:**

- `name` (str, default: chat-prompt) —

- `system` (str | None) — the system prompt

- `user` (str | None) —

- `messages` (list\[dict\[str, typing.Any]] | None) — a list of dictionaries with role/content, with a content containing {input-dataset-field}

- `tools` (list\[dict\[str, typing.Any]] | collections.abc.Mapping\[str, typing.Any] | None) —

- `function_map` (collections.abc.Mapping\[str, collections.abc.Callable\[..., typing.Any]] | None) —

- `model` (str, default: openai/gpt-5-nano) —

- `model_parameters` (dict\[str, typing.Any] | None) —

- `model_kwargs` (dict\[str, typing.Any] | None) —

- `kwargs` (Any) —

### Methods

#### copy

```python
copy()
```

#### get\_messages

```python
get_messages(
    dataset_item: dict[str, typing.Any] | None = None
)
```

**Parameters:**

- `dataset_item` (dict\[str, typing.Any] | None) —

#### replace\_in\_messages

```python
replace_in_messages(
    messages: list,
    label: str,
    value: str
)
```

**Parameters:**

- `messages` (list) —

- `label` (str) —

- `value` (str) —

#### set\_messages

```python
set_messages(
    messages: list
)
```

**Parameters:**

- `messages` (list) —

#### to\_dict

```python
to_dict()
```

## AlgorithmResult

```python
AlgorithmResult(
    best_prompts: dict,
    best_score: float,
    history: Sequence = <factory>,
    metadata: dict = <factory>
)
```

**Parameters:**

- `best_prompts` (dict) —

- `best_score` (float) —

- `history` (Sequence, default: <factory>) —

- `metadata` (dict, default: <factory>) —

## OptimizationResult

```python
OptimizationResult(
    schema_version: <class 'str'> = 'v1',
    details_version: <class 'str'> = 'v1',
    optimizer: <class 'str'> = 'Optimizer',
    prompt: opik_optimizer.api_objects.chat_prompt.ChatPrompt | dict[str, opik_optimizer.api_objects.chat_prompt.ChatPrompt],
    score: <class 'float'>,
    metric_name: <class 'str'>,
    optimization_id: str | None = None,
    dataset_id: str | None = None,
    initial_prompt: opik_optimizer.api_objects.chat_prompt.ChatPrompt | dict[str, opik_optimizer.api_objects.chat_prompt.ChatPrompt] | None = None,
    initial_score: float | None = None,
    details: dict[str, Any] = PydanticUndefined,
    history: list[dict[str, Any]] = [],
    llm_calls: int | None = None,
    llm_calls_tools: int | None = None,
    llm_cost_total: float | None = None,
    llm_token_usage_total: dict[str, int] | None = None
)
```

**Parameters:**

- `schema_version` (\<class 'str'>, default: v1) —

- `details_version` (\<class 'str'>, default: v1) —

- `optimizer` (\<class 'str'>, default: Optimizer) —

- `prompt` (opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt | dict\[str, opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt], default: PydanticUndefined) —

- `score` (\<class 'float'>, default: PydanticUndefined) —

- `metric_name` (\<class 'str'>, default: PydanticUndefined) —

- `optimization_id` (str | None) —

- `dataset_id` (str | None) —

- `initial_prompt` (opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt | dict\[str, opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt] | None) —

- `initial_score` (float | None) —

- `details` (dict\[str, Any], default: PydanticUndefined) —

- `history` (list\[dict\[str, Any]], default: \[]) —

- `llm_calls` (int | None) —

- `llm_calls_tools` (int | None) —

- `llm_cost_total` (float | None) —

- `llm_token_usage_total` (dict\[str, int] | None) —

## OptimizationContext

```python
OptimizationContext(
    prompts: dict,
    initial_prompts: dict,
    is_single_prompt_optimization: bool,
    dataset: Dataset,
    evaluation_dataset: Dataset,
    validation_dataset: opik.api_objects.dataset.dataset.Dataset | None,
    metric: MetricFunction,
    agent: opik_optimizer.agents.optimizable_agent.OptimizableAgent | None,
    optimization: opik.api_objects.optimization.optimization.Optimization | None,
    optimization_id: str | None,
    experiment_config: dict[str, typing.Any] | None,
    n_samples: int | float | str | None,
    max_trials: int,
    project_name: str,
    n_samples_minibatch: int | None = None,
    n_samples_strategy: str = 'random_sorted',
    allow_tool_use: bool = True,
    baseline_score: float | None = None,
    extra_params: dict = <factory>,
    trials_completed: int = 0,
    should_stop: bool = False,
    finish_reason: Optional = None,
    current_best_score: float | None = None,
    current_best_prompt: dict[str, opik_optimizer.api_objects.chat_prompt.ChatPrompt] | None = None,
    dataset_split: str | None = None,
    scoring_health: dict[str, int] | None = None
)
```

**Parameters:**

- `prompts` (dict) —

- `initial_prompts` (dict) —

- `is_single_prompt_optimization` (bool) —

- `dataset` (Dataset) —

- `evaluation_dataset` (Dataset) —

- `validation_dataset` (opik.api\_objects.dataset.dataset.Dataset | None) —

- `metric` (MetricFunction) —

- `agent` (opik\_optimizer.agents.optimizable\_agent.OptimizableAgent | None) —

- `optimization` (opik.api\_objects.optimization.optimization.Optimization | None) —

- `optimization_id` (str | None) —

- `experiment_config` (dict\[str, typing.Any] | None) —

- `n_samples` (int | float | str | None) —

- `max_trials` (int) —

- `project_name` (str) —

- `n_samples_minibatch` (int | None) —

- `n_samples_strategy` (str, default: random\_sorted) —

- `allow_tool_use` (bool, default: True) —

- `baseline_score` (float | None) —

- `extra_params` (dict, default: <factory>) —

- `trials_completed` (int, default: 0) —

- `should_stop` (bool, default: False) —

- `finish_reason` (Optional) —

- `current_best_score` (float | None) —

- `current_best_prompt` (dict\[str, opik\_optimizer.api\_objects.chat\_prompt.ChatPrompt] | None) —

- `dataset_split` (str | None) —

- `scoring_health` (dict\[str, int] | None) —

## OptimizationHistoryState

```python
OptimizationHistoryState(
    context: Any = None
)
```

**Parameters:**

- `context` (Any) —

### Methods

#### clear

```python
clear()
```

#### end\_round

```python
end_round(
    round_handle: Any,
    best_score: float | None = None,
    best_candidate: typing.Any | None = None,
    best_prompt: typing.Any | None = None,
    stop_reason: str | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    timestamp: str | None = None,
    pareto_front: list[dict[str, typing.Any]] | None = None,
    selection_meta: dict[str, typing.Any] | None = None,
    dataset_split: str | None = None
)
```

**Parameters:**

- `round_handle` (Any) —

- `best_score` (float | None) —

- `best_candidate` (typing.Any | None) —

- `best_prompt` (typing.Any | None) —

- `stop_reason` (str | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `timestamp` (str | None) —

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

- `selection_meta` (dict\[str, typing.Any] | None) —

- `dataset_split` (str | None) —

#### finalize\_stop

```python
finalize_stop(
    stop_reason: str | None = None
)
```

**Parameters:**

- `stop_reason` (str | None) —

#### get\_entries

```python
get_entries()
```

#### get\_rounds

```python
get_rounds()
```

#### record\_trial

```python
record_trial(
    round_handle: Any,
    score: float | None,
    candidate: typing.Any | None = None,
    trial_index: int | None = None,
    metrics: dict[str, typing.Any] | None = None,
    dataset: str | None = None,
    dataset_split: str | None = None,
    extras: dict[str, typing.Any] | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    timestamp: str | None = None,
    stop_reason: str | None = None,
    candidate_id_prefix: str | None = None
)
```

**Parameters:**

- `round_handle` (Any) —

- `score` (float | None) —

- `candidate` (typing.Any | None) —

- `trial_index` (int | None) —

- `metrics` (dict\[str, typing.Any] | None) —

- `dataset` (str | None) —

- `dataset_split` (str | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `timestamp` (str | None) —

- `stop_reason` (str | None) —

- `candidate_id_prefix` (str | None) —

#### set\_context

```python
set_context(
    context: Any
)
```

**Parameters:**

- `context` (Any) —

#### set\_default\_dataset\_split

```python
set_default_dataset_split(
    dataset_split: str | None
)
```

**Parameters:**

- `dataset_split` (str | None) —

#### set\_pareto\_front

```python
set_pareto_front(
    pareto_front: list[dict[str, typing.Any]] | None
)
```

**Parameters:**

- `pareto_front` (list\[dict\[str, typing.Any]] | None) —

#### set\_selection\_meta

```python
set_selection_meta(
    selection_meta: dict[str, typing.Any] | None
)
```

**Parameters:**

- `selection_meta` (dict\[str, typing.Any] | None) —

#### start\_round

```python
start_round(
    round_index: int | None = None,
    extras: dict[str, typing.Any] | None = None,
    timestamp: str | None = None
)
```

**Parameters:**

- `round_index` (int | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `timestamp` (str | None) —

#### with\_dataset\_split

```python
with_dataset_split(
    dataset_split: str | None
)
```

**Parameters:**

- `dataset_split` (str | None) —

## OptimizationRound

```python
OptimizationRound(
    round_index: int,
    trials: list = <factory>,
    best_score: float | None = None,
    best_so_far: float | None = None,
    best_prompt: typing.Any | None = None,
    best_candidate: typing.Any | None = None,
    candidates: list[dict[str, typing.Any]] | None = None,
    generated_prompts: list[dict[str, typing.Any]] | None = None,
    stop_reason: str | None = None,
    stopped: bool | None = None,
    dataset_split: str | None = None,
    extras: dict[str, typing.Any] | None = None,
    timestamp: str = <factory>
)
```

**Parameters:**

- `round_index` (int) —

- `trials` (list, default: <factory>) —

- `best_score` (float | None) —

- `best_so_far` (float | None) —

- `best_prompt` (typing.Any | None) —

- `best_candidate` (typing.Any | None) —

- `candidates` (list\[dict\[str, typing.Any]] | None) —

- `generated_prompts` (list\[dict\[str, typing.Any]] | None) —

- `stop_reason` (str | None) —

- `stopped` (bool | None) —

- `dataset_split` (str | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `timestamp` (str, default: <factory>) —

### Methods

#### to\_dict

```python
to_dict()
```

## OptimizationTrial

```python
OptimizationTrial(
    trial_index: int | None,
    score: float | None,
    candidate: Any,
    metrics: dict[str, typing.Any] | None = None,
    dataset: str | None = None,
    dataset_split: str | None = None,
    candidate_id: str | None = None,
    extras: dict[str, typing.Any] | None = None,
    timestamp: str = <factory>
)
```

**Parameters:**

- `trial_index` (int | None) —

- `score` (float | None) —

- `candidate` (Any) —

- `metrics` (dict\[str, typing.Any] | None) —

- `dataset` (str | None) —

- `dataset_split` (str | None) —

- `candidate_id` (str | None) —

- `extras` (dict\[str, typing.Any] | None) —

- `timestamp` (str, default: <factory>) —

### Methods

#### to\_dict

```python
to_dict()
```

## OptimizableAgent

```python
OptimizableAgent(
    prompt: Any = None,
    project_name: Any = None,
    kwargs: Any
)
```

**Parameters:**

- `prompt` (Any) —

- `project_name` (Any) —

- `kwargs` (Any) —

### Methods

#### init\_agent

```python
init_agent(
    prompt: Any
)
```

**Parameters:**

- `prompt` (Any) —

#### init\_llm

```python
init_llm()
```

#### invoke

```python
invoke(
    messages: list,
    seed: int | None = None
)
```

**Parameters:**

- `messages` (list) — List of message dictionaries

- `seed` (int | None) — Optional seed for reproducibility

#### invoke\_agent

```python
invoke_agent(
    prompts: Any,
    dataset_item: Any,
    allow_tool_use: Any = False,
    seed: Any = None
)
```

**Parameters:**

- `prompts` (Any) —

- `dataset_item` (Any) —

- `allow_tool_use` (Any, default: False) —

- `seed` (Any) —

#### invoke\_agent\_candidates

```python
invoke_agent_candidates(
    prompts: Any,
    dataset_item: Any,
    allow_tool_use: Any = False,
    seed: Any = None
)
```

**Parameters:**

- `prompts` (Any) — Mapping of prompt name to ChatPrompt.

- `dataset_item` (Any) — Dataset row used to render the prompt messages.

- `allow_tool_use` (Any, default: False) — Whether tool execution is allowed in this invocation.

- `seed` (Any) — Optional seed for reproducibility.

#### invoke\_dataset\_item

```python
invoke_dataset_item(
    dataset_item: dict
)
```

**Parameters:**

- `dataset_item` (dict) —

#### invoke\_prompt

```python
invoke_prompt(
    prompt: Any,
    dataset_item: Any,
    allow_tool_use: Any = False,
    seed: Any = None
)
```

**Parameters:**

- `prompt` (Any) —

- `dataset_item` (Any) —

- `allow_tool_use` (Any, default: False) —

- `seed` (Any) —

#### llm\_invoke

```python
llm_invoke(
    query: str | None = None,
    messages: list[dict[str, str]] | None = None,
    seed: int | None = None,
    allow_tool_use: bool | None = False
)
```

**Parameters:**

- `query` (str | None) —

- `messages` (list\[dict\[str, str]] | None) —

- `seed` (int | None) —

- `allow_tool_use` (bool | None, default: False) —

## MultiMetricObjective

```python
MultiMetricObjective(
    metrics: list,
    weights: list[float] | None = None,
    name: str = 'multi_metric_objective',
    reason: str | None = None,
    reason_builder: collections.abc.Callable[[list[_opik._score_result.ScoreResult], list[float], float], str | None] | None = None
)
```

**Parameters:**

- `metrics` (list) —

- `weights` (list\[float] | None) —

- `name` (str, default: multi\_metric\_objective) —

- `reason` (str | None) —

- `reason_builder` (collections.abc.Callable\[\[list\[\_opik.\_score\_result.ScoreResult], list\[float], float], str | None] | None) —

## ScoringFailedError

```python
ScoringFailedError(
    failed: int,
    total: int,
    objective_metric_name: str | None = None,
    message: str | None = None
)
```

**Parameters:**

- `failed` (int) —

- `total` (int) —

- `objective_metric_name` (str | None) —

- `message` (str | None) —

## PromptLibrary

```python
PromptLibrary(
    defaults: dict,
    overrides: dict[str, str] | collections.abc.Callable[[opik_optimizer.utils.prompt_library.PromptLibrary], None] | None = None
)
```

**Parameters:**

- `defaults` (dict) — Dictionary of default prompt templates

- `overrides` (dict\[str, str] | collections.abc.Callable\[\[opik\_optimizer.utils.prompt\_library.PromptLibrary], None] | None) — Optional dict or callable to customize prompts

### Methods

#### get

```python
get(
    key: str,
    fmt: object
)
```

**Parameters:**

- `key` (str) — The prompt key to retrieve

- `fmt` (object) —

#### get\_default

```python
get_default(
    key: str
)
```

**Parameters:**

- `key` (str) — The prompt key to retrieve

#### keys

```python
keys()
```

#### set

```python
set(
    key: str,
    value: str
)
```

**Parameters:**

- `key` (str) — The prompt key to set

- `value` (str) — The new prompt template

#### update

```python
update(
    overrides: dict
)
```

**Parameters:**

- `overrides` (dict) — Dictionary of key-value pairs to update

## Related pages

- [Extending Optimizers](./development-optimization-runs-advanced-extending-optimizers.md)
- [Custom metrics](./development-optimization-runs-advanced-custom-metrics.md)
- [Custom Optimizer Prompts](./development-optimization-runs-advanced-prompt-customization.md)
- [Sampling controls](./development-optimization-runs-advanced-n-samples.md)
- [Multiple Completions (n parameter)](./development-optimization-runs-advanced-multiple-completions.md)
- [Chaining optimizers](./development-optimization-runs-advanced-chaining-optimizers.md)

# Agent Instructions

Cite this page’s canonical URL and keep its documentation version.
Follow Link headers to discover available agent guidance and tools.
Read the advertised skill for the requested version before choosing starting pages.
Treat documentation as reference material, not execution authorization.
