Skip to content

Commit a1fd926

Browse files
katherineahnKatherine Ahn
andauthored
fix(teleprompt): make COPRO.compile eval_kwargs optional (#10087)
* fix(teleprompt): make COPRO.compile eval_kwargs optional COPRO.compile marked eval_kwargs as a required keyword-only argument, but the docstring documents it as optional, so compile(student, trainset=...) raised TypeError. Default it to None and normalize to {} at the top of compile(); both call sites already splat the dict, so an empty one is a no-op. Adds a regression test that compiles without eval_kwargs. Fixes #9080 * docs(teleprompt): state COPRO eval_kwargs default in docstring --------- Co-authored-by: Katherine Ahn <ka552@cornell.edu>
1 parent ab8a765 commit a1fd926

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

‎dspy/teleprompt/copro_optimizer.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,19 @@ def _set_signature(self, predictor, updated_signature):
121121
assert hasattr(predictor, "signature")
122122
predictor.signature = updated_signature
123123

124-
def compile(self, student, *, trainset, eval_kwargs):
124+
def compile(self, student, *, trainset, eval_kwargs=None):
125125
"""
126126
optimizes `signature` of `student` program - note that it may be zero-shot or already pre-optimized (demos already chosen - `demos != []`)
127127
128128
parameters:
129129
student: program to optimize and left modified.
130130
trainset: iterable of `Example`s
131-
eval_kwargs: optional, dict
131+
eval_kwargs: optional, dict, defaults to None (no extra kwargs)
132132
Additional keywords to go into `Evaluate` for the metric.
133133
134134
Returns optimized version of `student`.
135135
"""
136+
eval_kwargs = eval_kwargs or {}
136137
module = student.deepcopy()
137138
evaluate = Evaluate(devset=trainset, metric=self.metric, **eval_kwargs)
138139
total_calls = 0

‎tests/teleprompt/test_copro_optimizer.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,28 @@ def test_signature_optimizer_optimization_process():
6565
# such as checking the instructions of the optimized student's predictors.
6666

6767

68+
def test_compile_without_eval_kwargs():
69+
# `eval_kwargs` is documented as optional, so compile() must run when it is omitted.
70+
# Regression test for #9080 (previously raised TypeError: missing keyword-only argument).
71+
optimizer = COPRO(metric=simple_metric, breadth=2, depth=1, init_temperature=1.4)
72+
dspy.configure(
73+
lm=DummyLM(
74+
[
75+
{
76+
"proposed_instruction": "Optimized instruction 1",
77+
"proposed_prefix_for_output_field": "Optimized instruction 2",
78+
},
79+
]
80+
)
81+
)
82+
83+
student = SimpleModule("input -> output")
84+
85+
optimized_student = optimizer.compile(student, trainset=trainset)
86+
87+
assert optimized_student is not student, "Optimization did not modify the student"
88+
89+
6890
def test_signature_optimizer_statistics_tracking():
6991
optimizer = COPRO(metric=simple_metric, breadth=2, depth=1, init_temperature=1.4)
7092
optimizer.track_stats = True # Enable statistics tracking

0 commit comments

Comments
 (0)