synth_ai.sdk.optimization.policy.job
Policy optimization job implementation.
This module provides the canonical PolicyOptimizationJob class for running
policy optimization (prompt/instruction optimization) jobs.
Replaces: PromptLearningJob (deprecated)
Backend endpoint: /api/policy-optimization/online/jobs
Algorithms:
-
gepa: Genetic Evolutionary Prompt Algorithm (default)
- Evolutionary algorithm for optimizing prompts through population-based search
- Uses mutation, crossover, and selection to evolve prompt candidates
- Supports both online and offline optimization modes
-
mipro: Multi-prompt Instruction Proposal Optimizer
- Systematic instruction proposal and evaluation algorithm
- Generates new prompt instructions based on reward feedback
- Supports online mode where you drive rollouts locally
- Backend provides proxy URL for prompt candidate selection
Classes
Algorithm
Supported policy optimization algorithms.
Attributes:
GEPA: Genetic Evolutionary Prompt Algorithm - Evolutionary population-based searchMIPRO: Multi-prompt Instruction Proposal Optimizer - Systematic instruction proposal
from_string
value: Algorithm name (case-insensitive)
- Algorithm enum value, defaults to GEPA if invalid
PolicyOptimizationJobConfig
Configuration for a policy optimization job.
This dataclass holds all the configuration needed to submit and run
a policy optimization job (GEPA or MIPRO).
Supports two modes:
- File-based: Provide
config_pathpointing to a TOML file - Programmatic: Provide
config_dictwith the configuration directly
config_path: Path to the TOML configuration file. Mutually exclusive with config_dict.config_dict: Dictionary with policy optimization configuration.backend_url: Base URL of the Synth API backend.api_key: Synth API key for authentication.localapi_api_key: API key for authenticating with the LocalAPI.algorithm: Optimization algorithm to use (gepa, mipro).allow_experimental: If True, allows use of experimental models.overrides: Dictionary of config overrides.
to_prompt_learning_config
PolicyOptimizationJob
High-level SDK class for running policy optimization jobs.
This is the canonical class for policy optimization, replacing
PromptLearningJob. It supports both GEPA and MIPRO algorithms.
GEPA (Genetic Evolutionary Prompt Algorithm):
- Evolutionary algorithm using population-based search
- Optimizes prompts through mutation, crossover, and selection
- Supports both online and offline optimization modes
- Best for: Comprehensive search across prompt space
- Systematic instruction proposal and evaluation
- Generates new prompt instructions based on reward feedback
- Online mode: You drive rollouts, backend provides prompt candidates
- Best for: Iterative refinement with real-time prompt evolution
job_id: The job ID (None until submitted)algorithm: The optimization algorithm being used (GEPA or MIPRO)
from_config
config_path: Path to TOML config filebackend_url: Backend API URL (defaults to env or production)api_key: API key (defaults to SYNTH_API_KEY env var)localapi_api_key: LocalAPI key (defaults to ENVIRONMENT_API_KEY env var)algorithm: Optimization algorithm (gepa or mipro)allow_experimental: Allow experimental modelsoverrides: Config overrides
- PolicyOptimizationJob instance
ValueError: If required config is missingFileNotFoundError: If config file doesn’t exist
from_dict
config_dict: Configuration dictionarybackend_url: Backend API URL (defaults to env or production)api_key: API key (defaults to SYNTH_API_KEY env var)localapi_api_key: LocalAPI key (defaults to ENVIRONMENT_API_KEY env var)algorithm: Optimization algorithm (gepa or mipro)allow_experimental: Allow experimental modelsoverrides: Config overridesskip_health_check: If True, skip LocalAPI health check
- PolicyOptimizationJob instance
from_job_id
job_id: Existing job IDbackend_url: Backend API URL (defaults to env or production)api_key: API key (defaults to SYNTH_API_KEY env var)
- PolicyOptimizationJob instance for the existing job
job_id
algorithm
submit
- Job ID
RuntimeError: If job submission failsValueError: If LocalAPI health check fails
get_status
- Job status dictionary
RuntimeError: If job hasn’t been submitted yet
poll_until_complete
timeout: Maximum seconds to wait for job completioninterval: Seconds between poll attemptsprogress: If True, print status updates during pollingon_status: Optional callback called on each status updaterequest_timeout: HTTP timeout for each status request
- PolicyOptimizationResult with typed status, best_score, etc.
RuntimeError: If job hasn’t been submitted yetTimeoutError: If timeout is exceeded
stream_until_complete
timeout: Maximum seconds to wait (default: 3600 = 1 hour)interval: Seconds between status checks (for SSE reconnects)handlers: Optional StreamHandler instances for custom event handlingon_event: Optional callback called on each event
- PolicyOptimizationResult with typed status, best_score, etc.
RuntimeError: If job hasn’t been submitted yet
get_results
- Results dictionary with best_prompt, best_score, etc.
RuntimeError: If job hasn’t been submitted yet
get_best_prompt_text
rank: Prompt rank (1 = best, 2 = second best, etc.)
- Prompt text or None if not found
cancel
reason: Optional reason for cancellation
- Dict with cancellation status
RuntimeError: If job hasn’t been submitted yet
query_workflow_state
- Dict with workflow state
RuntimeError: If job hasn’t been submitted yet