DSPy: The framework for programming—not prompting—language models
Python
perf(dspy): avoid walking the stack in the forward guard (#10534)
Isaac Millereaac7ed03632
diff --git a/dspy/primitives/module.py b/dspy/primitives/module.pyindex 10f092393..2b3f891e9 100644--- a/dspy/primitives/module.py+++ b/dspy/primitives/module.py@@ -1,5 +1,5 @@-import inspectimport logging+import sysfrom typing import Any, TextIOfrom dspy.dsp.utils.settings import settings@@ -337,9 +337,8 @@ class Module(BaseModule, metaclass=ProgramMeta):attr = super().__getattribute__(name)if name == "forward" and callable(attr):- # Check if forward is called through __call__ or directly- stack = inspect.stack()- forward_called_directly = len(stack) <= 1 or stack[1].function != "__call__"+ # Only the immediate caller matters; avoid walking the full stack on every call.+ forward_called_directly = sys._getframe(1).f_code.co_name != "__call__"if forward_called_directly:logger.warning(diff --git a/tests/primitives/test_base_module.py b/tests/primitives/test_base_module.pyindex 290f32b5a..9d7ff19a6 100644--- a/tests/primitives/test_base_module.py+++ b/tests/primitives/test_base_module.py@@ -544,6 +544,20 @@ def test_forward_through_call_no_warning(capsys):assert "directly is discouraged" not in captured.err+def test_forward_guard_does_not_walk_the_stack(monkeypatch):+ import inspect++ class TestModule(dspy.Module):+ def forward(self, x):+ return x++ def fail(*args, **kwargs):+ raise AssertionError("inspect.stack() must not run on module call")++ monkeypatch.setattr(inspect, "stack", fail)+ assert TestModule()(x="test") == "test"++def test_modules_to_serialize_registration_does_not_outlive_the_save(tmp_path):# cloudpickle's by-value registry is process-wide and keyed by module# name. A save must not leave a module registered, or every later pickle