COPRO is a DSPy optimizer for prompt instructions. Instead of manually rewriting an instruction until it seems right, it generates alternatives, scores them on a validation set, and repeats from the best candidate. The process resembles hill climbing: each round searches for an improvement on what has worked so far. section 6.1
COPRO also proposes prefixes for output fields. These short phrases can guide an LM, but instructions generally matter more. Whether a prefix appears in the resulting prompt depends on the DSPy adapter, which turns program fields and other information into an LM prompt. section 6.1.1
Each optimization run follows a practical loop:
- Start with the program’s original instructions. In the first round, keep them as one candidate and ask an LM for additional instructions and output prefixes. section 6.1 section 6.1.5
- Evaluate candidates with a metric on the validation set, retain the best, and repeat for the configured number of rounds. section 6.1 section 6.1.3
- From the second round onward, give the generating LM earlier instructions, prefixes, and scores so it can propose new candidates informed by previous attempts. section 6.1.5
Listing 6.1 The COPRO optimizer
copro = dspy.COPRO(metric=validate_answer, breadth=2, depth=2,
init_temperature=1.5,
track_stats=True)
prog_copro = copro.compile(intent_classifier, trainset=val_set,
eval_kwargs={"num_threads": 3})
prediction = prog_copro(message="What meals are served on the 3pm flight
to Madrid?")
lm.inspect_history(n=1)
The Python listing instantiates `dspy.COPRO` with a metric, breadth, depth, and generation temperature, then calls `compile()` on an existing DSPy program. It assumes the classifier, validation set, metric, and LM have already been defined. section 6.1.3
`breadth` sets how many candidate prompts are evaluated per round; `depth` sets the number of rounds. COPRO tests `breadth × depth` prompts. Increasing either widens the search but can raise cost: one example run with low settings took about four minutes, 1,212 LM calls, and 601,112 tokens. Runtime and usage also depend on validation-set size and input length. section 6.1.3
Inspecting a classification run
An airline-message classifier began with “Classify the message into one of the possible labels.” After compilation, its instructions were replaced with more specific guidance about selecting a predefined label. In a separate run using 12 candidates per round and five rounds, the highest reported score rose from 84.0 in round 0 to 86.0 in round 3; it remained 86.0 in round 4. These are results of those runs, not a guarantee of improvement on another task. section 6.1 section 6.1.3
COPRO is one choice among DSPy instruction optimizers. Its search focuses on instructions and output prefixes rather than selecting demonstrations. Other optimizers can optimize demonstrations or generate rules, so the best choice depends on the task and LM. section 6.1.1 section 6.9
