DSPy: The framework for programming—not prompting—language models
Python
feat(callbacks): expose optimizer compile events (#10120)
Isaac Miller80553206bc1a
diff --git a/docs/docs/tutorials/observability/index.md b/docs/docs/tutorials/observability/index.mdindex d5ae66ca2..ec696568f 100644--- a/docs/docs/tutorials/observability/index.md+++ b/docs/docs/tutorials/observability/index.md@@ -205,6 +205,7 @@ Sometimes, you may want to implement a custom logging solution. For instance, yo|`on_adapter_parse_start` / `on_adapter_parse_end`| Triggered when a `dspy.Adapter` subclass postprocess the output text from an LM. ||`on_tool_start` / `on_tool_end` | Triggered when a `dspy.Tool` subclass is invoked. ||`on_evaluate_start` / `on_evaluate_end` | Triggered when a `dspy.Evaluate` instance is invoked. |+|`on_compile_start` / `on_compile_end` | Triggered when a DSPy optimizer's `compile()` method is invoked. |Here's an example of custom callback that logs the intermediate steps of a ReAct agent:diff --git a/dspy/teleprompt/teleprompt.py b/dspy/teleprompt/teleprompt.pyindex ad1b8b865..507d79a1a 100644--- a/dspy/teleprompt/teleprompt.py+++ b/dspy/teleprompt/teleprompt.py@@ -1,12 +1,66 @@+import asyncio+import functools+import inspect+import threadingfrom typing import Any+import dspy.utils.callback_context as callback_contextfrom dspy.primitives import Example, Module+from dspy.utils.callback import with_callbacks+++def _compile_invocation_key(instance):+ try:+ task = asyncio.current_task()+ except RuntimeError:+ task = None+ return id(instance), threading.get_ident(), id(task) if task is not None else None+++def _with_compile_callbacks(fn):+ callback_fn = with_callbacks(fn)++ if inspect.iscoroutinefunction(fn):+ @functools.wraps(fn)+ async def async_wrapper(instance, *args, **kwargs):+ active_compiles = callback_context._ACTIVE_COMPILES.get()+ invocation_key = _compile_invocation_key(instance)+ if invocation_key in active_compiles:+ return await fn(instance, *args, **kwargs)+ token = callback_context._ACTIVE_COMPILES.set((*active_compiles, invocation_key))+ try:+ return await callback_fn(instance, *args, **kwargs)+ finally:+ callback_context._ACTIVE_COMPILES.reset(token)++ return async_wrapper++ @functools.wraps(fn)+ def wrapper(instance, *args, **kwargs):+ active_compiles = callback_context._ACTIVE_COMPILES.get()+ invocation_key = _compile_invocation_key(instance)+ if invocation_key in active_compiles:+ return fn(instance, *args, **kwargs)+ token = callback_context._ACTIVE_COMPILES.set((*active_compiles, invocation_key))+ try:+ return callback_fn(instance, *args, **kwargs)+ finally:+ callback_context._ACTIVE_COMPILES.reset(token)++ return wrapperclass Teleprompter:def __init__(self):pass+ def __init_subclass__(cls, **kwargs):+ super().__init_subclass__(**kwargs)+ # Cover conventional class-body overrides while preserving inherited wrapped methods.+ # Replacing compile after class creation is not a supported instrumentation path.+ if "compile" in cls.__dict__:+ cls.compile = _with_compile_callbacks(cls.compile)+def compile(self, student: Module, *, trainset: list[Example], teacher: Module | None = None, valset: list[Example] | None = None, **kwargs) -> Module:"""Optimize the student program.diff --git a/dspy/utils/callback.py b/dspy/utils/callback.pyindex 4652c8c7a..529287e83 100644--- a/dspy/utils/callback.py+++ b/dspy/utils/callback.py@@ -285,6 +285,26 @@ class BaseCallback:def on_interpreter_shutdown_end(self, call_id: str, outputs: Any | None, exception: Exception | None = None):pass+ def on_compile_start(self, call_id: str, instance: Any, inputs: dict[str, Any]):+ """A handler triggered when an optimizer's compile method is called.++ Args:+ call_id: A unique identifier for the compile call. Can be used to connect start/end handlers.+ instance: The optimizer instance.+ inputs: The inputs to the optimizer's compile method as key-value pairs.+ """+ pass++ def on_compile_end(self, call_id: str, outputs: Any | None, exception: Exception | None = None):+ """A handler triggered after an optimizer's compile method is called.++ Args:+ call_id: A unique identifier for the compile call. Can be used to connect start/end handlers.+ outputs: The compiled program, or None if compilation failed.+ exception: The exception raised during compilation, if any.+ """+ pass+def with_callbacks(fn):"""Decorator to add callback functionality to instance methods."""@@ -381,12 +401,16 @@ def with_callbacks(fn):def _get_on_start_handler(callback: BaseCallback, instance: Any, fn: Callable) -> Callable:"""Selects the appropriate on_start handler of the callback based on the instance and function name."""+ from dspy.teleprompt.teleprompt import Teleprompter+if isinstance(instance, dspy.BaseLM):return callback.on_lm_startelif isinstance(instance, dspy.Evaluate):return callback.on_evaluate_startelif isinstance(instance, dspy.CodeInterpreter):return getattr(callback, f"on_interpreter_{_INTERPRETER_OPERATIONS[fn.__name__]}_start")+ elif isinstance(instance, Teleprompter):+ return callback.on_compile_startif isinstance(instance, dspy.Adapter):if fn.__name__ == "format":@@ -405,12 +429,16 @@ def _get_on_start_handler(callback: BaseCallback, instance: Any, fn: Callable) -def _get_on_end_handler(callback: BaseCallback, instance: Any, fn: Callable) -> Callable:"""Selects the appropriate on_end handler of the callback based on the instance and function name."""+ from dspy.teleprompt.teleprompt import Teleprompter+if isinstance(instance, dspy.BaseLM):return callback.on_lm_endelif isinstance(instance, dspy.Evaluate):return callback.on_evaluate_endelif isinstance(instance, dspy.CodeInterpreter):return getattr(callback, f"on_interpreter_{_INTERPRETER_OPERATIONS[fn.__name__]}_end")+ elif isinstance(instance, Teleprompter):+ return callback.on_compile_endif isinstance(instance, (dspy.Adapter)):if fn.__name__ == "format":diff --git a/dspy/utils/callback_context.py b/dspy/utils/callback_context.pyindex 7aca4d701..ca848409c 100644--- a/dspy/utils/callback_context.py+++ b/dspy/utils/callback_context.py@@ -3,6 +3,7 @@ from contextvars import ContextVarfrom typing import CallableACTIVE_CALL_ID = ContextVar("active_call_id", default=None)+_ACTIVE_COMPILES = ContextVar("active_compiles", default=())def _bind_active_call_id(fn: Callable) -> Callable:diff --git a/tests/callback/test_optimizer_callback.py b/tests/callback/test_optimizer_callback.pynew file mode 100644index 000000000..e16930327--- /dev/null+++ b/tests/callback/test_optimizer_callback.py@@ -0,0 +1,350 @@+import asyncio+from unittest.mock import Mock, patch++import cloudpickle+import pytest++import dspy+from dspy.teleprompt.bettertogether import BetterTogether+from dspy.teleprompt.teleprompt import Teleprompter+from dspy.teleprompt.vanilla import LabeledFewShot+from dspy.utils.callback import ACTIVE_CALL_ID, BaseCallback+from dspy.utils.dummies import DummyLM+++class RecordingCallback(BaseCallback):+ def __init__(self):+ self.events = []++ def on_compile_start(self, call_id, instance, inputs):+ self.events.append({+ "handler": "compile_start",+ "call_id": call_id,+ "parent_call_id": ACTIVE_CALL_ID.get(),+ "instance": instance,+ "inputs": inputs,+ })++ def on_compile_end(self, call_id, outputs, exception):+ self.events.append({+ "handler": "compile_end",+ "call_id": call_id,+ "parent_call_id": ACTIVE_CALL_ID.get(),+ "outputs": outputs,+ "exception": exception,+ })++ def on_module_start(self, call_id, instance, inputs):+ self.events.append({+ "handler": "module_start",+ "call_id": call_id,+ "parent_call_id": ACTIVE_CALL_ID.get(),+ "instance": instance,+ "inputs": inputs,+ })++ def on_module_end(self, call_id, outputs, exception):+ self.events.append({+ "handler": "module_end",+ "call_id": call_id,+ "parent_call_id": ACTIVE_CALL_ID.get(),+ "outputs": outputs,+ "exception": exception,+ })+++def events_for(callback, handler):+ return [event for event in callback.events if event["handler"] == handler]+++def test_compile_callbacks_report_bound_inputs_output_and_call_id():+ class Optimizer(Teleprompter):+ def compile(self, student, *, trainset, teacher=None):+ return student++ callback = RecordingCallback()+ optimizer = Optimizer()+ student = dspy.Module()+ trainset = [dspy.Example(question="test").with_inputs("question")]++ with dspy.context(callbacks=[callback]):+ result = optimizer.compile(student, trainset=trainset)++ start, end = callback.events+ assert start["handler"] == "compile_start"+ assert start["instance"] is optimizer+ assert start["inputs"] == {"student": student, "trainset": trainset, "teacher": None}+ assert start["parent_call_id"] is None+ assert end["handler"] == "compile_end"+ assert end["call_id"] == start["call_id"]+ assert end["outputs"] is result is student+ assert end["exception"] is None+++def test_compile_callback_reports_and_propagates_same_exception():+ expected_error = ValueError("compile failed")++ class FailingOptimizer(Teleprompter):+ def __init__(self):+ self.should_fail = True++ def compile(self, student, *, trainset):+ if self.should_fail:+ self.should_fail = False+ raise expected_error+ return student++ callback = RecordingCallback()+ optimizer = FailingOptimizer()+ student = dspy.Module()++ with dspy.context(callbacks=[callback]):+ with pytest.raises(ValueError) as exc_info:+ optimizer.compile(student, trainset=[])+ assert optimizer.compile(student, trainset=[]) is student++ first_start, first_end, second_start, second_end = callback.events+ assert first_end["call_id"] == first_start["call_id"]+ assert first_end["outputs"] is None+ assert first_end["exception"] is expected_error+ assert exc_info.value is expected_error+ assert second_end["call_id"] == second_start["call_id"]+ assert second_end["outputs"] is student+ assert second_end["exception"] is None+++def test_nested_optimizer_and_module_callbacks_preserve_hierarchy():+ class Student(dspy.Module):+ def forward(self, value):+ return dspy.Prediction(value=value)++ class InnerOptimizer(Teleprompter):+ def compile(self, student, *, trainset):+ student(value=trainset[0])+ return student++ class OuterOptimizer(Teleprompter):+ def compile(self, student, *, trainset, optimizer):+ return optimizer.compile(student, trainset=trainset)++ callback = RecordingCallback()+ outer = OuterOptimizer()+ inner = InnerOptimizer()+ student = Student()++ with dspy.context(callbacks=[callback]):+ assert outer.compile(student, trainset=["value"], optimizer=inner) is student++ assert [event["handler"] for event in callback.events] == [+ "compile_start",+ "compile_start",+ "module_start",+ "module_end",+ "compile_end",+ "compile_end",+ ]+ outer_start, inner_start, module_start, module_end, inner_end, outer_end = callback.events+ assert inner_start["parent_call_id"] == outer_start["call_id"]+ assert module_start["parent_call_id"] == inner_start["call_id"]+ assert module_end["parent_call_id"] == inner_start["call_id"]+ assert inner_end["parent_call_id"] == outer_start["call_id"]+ assert outer_end["parent_call_id"] is None+ assert len({outer_start["call_id"], inner_start["call_id"], module_start["call_id"]}) == 3+++def test_inherited_compile_is_not_wrapped_twice_and_override_is_wrapped_once():+ class BaseOptimizer(Teleprompter):+ def compile(self, student, *, trainset):+ return student++ class InheritedOptimizer(BaseOptimizer):+ pass++ class OverridingOptimizer(BaseOptimizer):+ def compile(self, student, *, trainset):+ return super().compile(student, trainset=trainset)++ callback = RecordingCallback()+ student = dspy.Module()++ with dspy.context(callbacks=[callback]):+ InheritedOptimizer().compile(student, trainset=[])+ OverridingOptimizer().compile(student, trainset=[])++ starts = events_for(callback, "compile_start")+ ends = events_for(callback, "compile_end")+ assert len(starts) == len(ends) == 2+ assert isinstance(starts[0]["instance"], InheritedOptimizer)+ assert isinstance(starts[1]["instance"], OverridingOptimizer)+ assert starts[1]["parent_call_id"] is None+++async def test_async_override_calling_super_emits_one_compile_callback_pair():+ class BaseOptimizer(Teleprompter):+ async def compile(self, student, *, trainset):+ return student++ class OverridingOptimizer(BaseOptimizer):+ async def compile(self, student, *, trainset):+ return await super().compile(student, trainset=trainset)++ callback = RecordingCallback()+ optimizer = OverridingOptimizer()+ student = dspy.Module()++ with dspy.context(callbacks=[callback]):+ assert await optimizer.compile(student, trainset=[]) is student++ start, end = callback.events+ assert start["instance"] is optimizer+ assert start["parent_call_id"] is None+ assert end["call_id"] == start["call_id"]+++async def test_same_instance_compile_in_child_task_emits_nested_callback_pair():+ class Optimizer(Teleprompter):+ async def compile(self, student, *, trainset, child=False):+ if not child:+ return await asyncio.create_task(self.compile(student, trainset=trainset, child=True))+ return student++ callback = RecordingCallback()+ optimizer = Optimizer()+ student = dspy.Module()++ with dspy.context(callbacks=[callback]):+ assert await optimizer.compile(student, trainset=[]) is student++ outer_start, child_start, child_end, outer_end = callback.events+ assert [event["handler"] for event in callback.events] == [+ "compile_start",+ "compile_start",+ "compile_end",+ "compile_end",+ ]+ assert outer_start["instance"] is child_start["instance"] is optimizer+ assert outer_start["parent_call_id"] is None+ assert child_start["parent_call_id"] == outer_start["call_id"]+ assert child_end["call_id"] == child_start["call_id"]+ assert child_end["parent_call_id"] == outer_start["call_id"]+ assert outer_end["call_id"] == outer_start["call_id"]+ assert outer_end["parent_call_id"] is None+++def test_compile_callback_wrapper_round_trips_through_cloudpickle():+ class Optimizer(Teleprompter):+ def compile(self, student, *, trainset):+ return student++ optimizer = cloudpickle.loads(cloudpickle.dumps(Optimizer))()+ callback = RecordingCallback()+ student = dspy.Module()++ with dspy.context(callbacks=[callback]):+ assert optimizer.compile(student, trainset=[]) is student++ start, end = callback.events+ assert start["instance"] is optimizer+ assert end["call_id"] == start["call_id"]+++def test_existing_optimizer_compile_is_instrumented():+ callback = RecordingCallback()+ optimizer = LabeledFewShot(k=0)+ student = dspy.Module()++ with dspy.context(callbacks=[callback]):+ compiled = optimizer.compile(student, trainset=[])++ start, end = callback.events+ assert start["instance"] is optimizer+ assert start["inputs"] == {"student": student, "trainset": [], "sample": True}+ assert end["outputs"] is compiled+ assert end["exception"] is None+++def test_bettertogether_nests_child_compile_callback():+ class Student(dspy.Module):+ def __init__(self):+ super().__init__()+ self.predictor = dspy.Predict("question -> answer")++ def forward(self, question):+ return self.predictor(question=question)++ class ChildOptimizer(Teleprompter):+ def compile(self, student, *, trainset):+ return student++ callback = RecordingCallback()+ child = ChildOptimizer()+ optimizer = BetterTogether(metric=lambda example, prediction, trace=None: 1.0, child=child)+ student = Student()+ student.set_lm(DummyLM([{"answer": "answer"}]))+ trainset = [dspy.Example(question="question", answer="answer").with_inputs("question")]++ with (+ patch("dspy.teleprompt.bettertogether.eval_candidate_program", return_value=Mock(score=1.0)),+ patch("dspy.teleprompt.bettertogether.launch_lms"),+ patch("dspy.teleprompt.bettertogether.kill_lms"),+ dspy.context(callbacks=[callback]),+ ):+ optimizer.compile(student, trainset=trainset, valset=trainset, strategy="child")++ starts = events_for(callback, "compile_start")+ ends = events_for(callback, "compile_end")+ assert len(starts) == len(ends) == 2+ assert starts[0]["instance"] is optimizer+ assert starts[0]["parent_call_id"] is None+ assert starts[1]["instance"] is child+ assert starts[1]["parent_call_id"] == starts[0]["call_id"]+ assert ends[0]["call_id"] == starts[1]["call_id"]+ assert ends[0]["parent_call_id"] == starts[0]["call_id"]+ assert ends[1]["call_id"] == starts[0]["call_id"]+ assert ends[1]["parent_call_id"] is None+++def test_bettertogether_reports_child_failure_but_completes_outer_run():+ expected_error = RuntimeError("child compile failed")++ class Student(dspy.Module):+ def __init__(self):+ super().__init__()+ self.predictor = dspy.Predict("question -> answer")++ def forward(self, question):+ return self.predictor(question=question)++ class FailingChildOptimizer(Teleprompter):+ def compile(self, student, *, trainset):+ raise expected_error++ callback = RecordingCallback()+ child = FailingChildOptimizer()+ optimizer = BetterTogether(metric=lambda example, prediction, trace=None: 1.0, child=child)+ student = Student()+ student.set_lm(DummyLM([{"answer": "answer"}]))+ trainset = [dspy.Example(question="question", answer="answer").with_inputs("question")]++ with (+ patch("dspy.teleprompt.bettertogether.eval_candidate_program", return_value=Mock(score=1.0)),+ patch("dspy.teleprompt.bettertogether.launch_lms"),+ patch("dspy.teleprompt.bettertogether.kill_lms"),+ dspy.context(callbacks=[callback]),+ ):+ compiled = optimizer.compile(student, trainset=trainset, valset=trainset, strategy="child")++ starts = events_for(callback, "compile_start")+ ends = events_for(callback, "compile_end")+ assert len(starts) == len(ends) == 2+ assert starts[1]["instance"] is child+ assert starts[1]["parent_call_id"] == starts[0]["call_id"]+ assert ends[0]["call_id"] == starts[1]["call_id"]+ assert ends[0]["outputs"] is None+ assert ends[0]["exception"] is expected_error+ assert ends[1]["call_id"] == starts[0]["call_id"]+ assert ends[1]["outputs"] is compiled+ assert ends[1]["exception"] is None+ assert compiled.flag_compilation_error_occurred is True