liveBook · Manning

COPRO

As explained in Building LLM Applications with DSPy

COPRO is a prompt-optimization algorithm that generates candidate instructions, evaluates them, and uses prior attempts and scores to propose further variations.

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

How Building LLM Applications with DSPy covers it

How deep it goes
Explains the iteration loop, candidate-generation signatures, scoring inputs, and compilation parameters; does not provide a cost-free search procedure.
Examples
Uses an airline-message classifier, a Python compilation listing, and per-round score statistics.
What it assumes
Assumes a DSPy program, metric function, validation set, and configured LM; explains hill climbing without requiring prior familiarity.
What you can do after
Apply it via `dspy.COPRO` and `compile()` to optimize an existing program, then inspect its instructions and evaluation statistics.
How it connects
Connects instruction optimization to hill climbing, meta-prompting, DSPy adapters, and demonstration optimization.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building LLM Applications with DSPy. The section links point to the parts of the book it draws on.