Custom Optimizer Prompts
The Opik Optimizer uses a PromptLibrary system that lets you customize the internal prompts used by each optimizer. This is useful when you need to:
- Add domain-specific constraints (legal, medical, coding standards)
- Inject safety or compliance requirements
- Adjust output formatting or style
- Experiment with different reasoning approaches
Quick Start
Every optimizer accepts a prompt_overrides parameter:
How It Works
Each optimizer defines its own DEFAULT_PROMPTS dictionary with keys specific to that algorithm. The PromptLibrary:
- Stores the default prompts
- Applies your overrides (dict or callable)
- Validates that override keys exist (catches typos early)
- Provides
get_prompt()for runtime access
Override Methods
Dict Override (Simple Replacement)
Best when you know exactly which prompt to replace with a static string:
Callable Override (Dynamic Modification)
Best when you need to modify existing prompts, apply conditional logic, or update multiple prompts:
Discovering Available Keys
Each optimizer has different prompt keys. Use list_prompts() to discover them:
Common Keys by Optimizer
Reading Prompts at Runtime
After creating an optimizer, you can inspect the current prompts:
Template Variables
Some prompts contain placeholders that get filled at runtime using Python’s {variable} format. When overriding prompts with placeholders, keep the same placeholders:
Use Cases
Adding Domain Constraints
Enforcing Output Format
Adding Safety Guardrails
Error Handling
The PromptLibrary validates keys to catch typos early:
Best Practices
Tips for effective prompt customization:
- List keys first – Always use
list_prompts()to see available keys before overriding - Keep placeholders – If a prompt has
{variables}, keep them in your override - Test incrementally – Override one prompt at a time to isolate effects
- Use callable for complex logic – Dict is simpler, but callable is more powerful
- Don’t break JSON – Some prompts expect JSON output; maintain that structure
Full Example
Related
- API Reference – Full parameter documentation
- Custom metrics – Build specialized evaluation metrics
- Extending optimizers – Create custom optimizer subclasses