Skip to content

latent.optimize.base

Optimizer protocol and shared data models for prompt optimization.

Classes

ExperimentResult

ExperimentResult()

Result of a single optimization experiment.

OptimizationReport

OptimizationReport()

Full result of an optimization run with summary metadata.

OptimizedPrompt

OptimizedPrompt()

Serializable optimized prompt artifact.

Optimizer

Optimizer()

Protocol for prompt optimizers.

Functions

apply_optimized_prompt

apply_optimized_prompt(agent: Any, optimized: OptimizedPrompt) -> None

Apply an optimized prompt to an agent in-place.

For Judge: sets prompt_template. For ReActAgent: sets system_prompt.

OptimizedPrompt.few_shot_demos is not applied — a bootstrapped demo set does not reach Judge.few_shot, so a DSPy run that produced demos still needs them wired by hand.

An artifact with no system_prompt (DSPy writes only prompt_template) falls back to the template as the system prompt, so applying it to a plain ReActAgent is not a silent no-op. That fallback is skipped for an agent that owns a prompt_template: there the template is the row prompt, rendered per row with .format(), so installing it as the system prompt would send the model its own unrendered {placeholders} as instructions.

The prompt_template attribute must therefore exist by the time this runs. Judge declares it on the class for exactly that reason — see Judge.prompt_template.

Methods

OptimizedPrompt.from_best

from_best(result: OptimizationResult) -> OptimizedPrompt

Extract the best prompt from an OptimizationResult.

OptimizedPrompt.from_file

from_file(path: Path) -> OptimizedPrompt

Load from JSON file.

OptimizedPrompt.render_prompt

render_prompt(variables: dict[str, Any]) -> str

Render prompt_template with variable substitution using string.Template.

OptimizedPrompt.save

save(path: Path) -> None

Save to JSON file.

Optimizer.optimize

optimize(train_data: list[dict[str, Any]], metric: Any, max_iterations: int = 10) -> OptimizationResult

Run optimization and return the best prompt.

Attributes

OptimizationResult