Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

Add type hints for Module.__call__ and Module.acall to return Prediction (#8681)

James Summerfield97b68a693f12
diff --git a/dspy/primitives/module.py b/dspy/primitives/module.py
index a3cb0e49c..90be773e5 100644
--- a/dspy/primitives/module.py
+++ b/dspy/primitives/module.py
@@ -7,6 +7,7 @@ from dspy.dsp.utils.settings import settings, thread_local_overrides
from dspy.predict.parallel import Parallel
from dspy.primitives.base_module import BaseModule
from dspy.primitives.example import Example
+from dspy.primitives.prediction import Prediction
from dspy.utils.callback import with_callbacks
from dspy.utils.inspect_history import pretty_print_history
from dspy.utils.usage_tracker import track_usage
@@ -62,7 +63,7 @@ class Module(BaseModule, metaclass=ProgramMeta):
self.callbacks = []
@with_callbacks
- def __call__(self, *args, **kwargs):
+ def __call__(self, *args, **kwargs) -> Prediction:
caller_modules = settings.caller_modules or []
caller_modules = list(caller_modules)
caller_modules.append(self)
@@ -77,7 +78,7 @@ class Module(BaseModule, metaclass=ProgramMeta):
return self.forward(*args, **kwargs)
@with_callbacks
- async def acall(self, *args, **kwargs):
+ async def acall(self, *args, **kwargs) -> Prediction:
caller_modules = settings.caller_modules or []
caller_modules = list(caller_modules)
caller_modules.append(self)
diff --git a/tests/primitives/test_base_module.py b/tests/primitives/test_base_module.py
index b78a04d66..15e86181a 100644
--- a/tests/primitives/test_base_module.py
+++ b/tests/primitives/test_base_module.py
@@ -9,6 +9,7 @@ from litellm import Choices, Message, ModelResponse
from litellm.types.utils import Usage
import dspy
+from dspy.primitives.prediction import Prediction
from dspy.utils.dummies import DummyLM
@@ -260,7 +261,7 @@ def test_multi_module_call_with_usage_tracker(lm_for_test):
self.predict1 = dspy.ChainOfThought("question -> answer")
self.predict2 = dspy.ChainOfThought("question, answer -> score")
- def __call__(self, question: str) -> str:
+ def __call__(self, question: str) -> Prediction:
answer = self.predict1(question=question)
score = self.predict2(question=question, answer=answer)
return score
@@ -285,7 +286,7 @@ def test_usage_tracker_in_parallel():
self.predict1 = dspy.ChainOfThought("question -> answer")
self.predict2 = dspy.ChainOfThought("question, answer -> score")
- def __call__(self, question: str) -> str:
+ def __call__(self, question: str) -> Prediction:
with dspy.settings.context(lm=self.lm):
answer = self.predict1(question=question)
score = self.predict2(question=question, answer=answer)
@@ -365,7 +366,7 @@ def test_module_history():
super().__init__(**kwargs)
self.cot = dspy.ChainOfThought("question -> answer")
- def forward(self, question: str, **kwargs) -> str:
+ def forward(self, question: str, **kwargs) -> Prediction:
return self.cot(question=question)
with patch("litellm.completion") as mock_completion:
@@ -415,7 +416,7 @@ def test_module_history_with_concurrency():
super().__init__()
self.cot = dspy.ChainOfThought("question -> answer")
- def forward(self, question: str, **kwargs) -> str:
+ def forward(self, question: str, **kwargs) -> Prediction:
return self.cot(question=question)
with patch("litellm.completion") as mock_completion:
@@ -446,7 +447,7 @@ async def test_module_history_async():
super().__init__(**kwargs)
self.cot = dspy.ChainOfThought("question -> answer")
- async def aforward(self, question: str, **kwargs) -> str:
+ async def aforward(self, question: str, **kwargs) -> Prediction:
return await self.cot.acall(question=question)
with patch("litellm.acompletion") as mock_completion: