Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

Support predicted output in DSPy (#8247)

Chen Qian2ac77564dce0
diff --git a/dspy/predict/predict.py b/dspy/predict/predict.py
index 9f78ca804..fd7e538bb 100644
--- a/dspy/predict/predict.py
+++ b/dspy/predict/predict.py
@@ -108,6 +108,17 @@ class Predict(Module, Parameter):
if (temperature is None or temperature <= 0.15) and num_generations > 1:
config["temperature"] = 0.7
+ if "prediction" in kwargs:
+ if (
+ isinstance(kwargs["prediction"], dict)
+ and kwargs["prediction"].get("type") == "content"
+ and "content" in kwargs["prediction"]
+ ):
+ # If the `prediction` is the standard predicted outputs format
+ # (https://platform.openai.com/docs/guides/predicted-outputs), we remvoe it from input kwargs and add it
+ # to the lm kwargs.
+ config["prediction"] = kwargs.pop("prediction")
+
if not all(k in kwargs for k in signature.input_fields):
present = [k for k in signature.input_fields if k in kwargs]
missing = [k for k in signature.input_fields if k not in kwargs]
diff --git a/tests/predict/test_predict.py b/tests/predict/test_predict.py
index ba0c47820..d4ce96158 100644
--- a/tests/predict/test_predict.py
+++ b/tests/predict/test_predict.py
@@ -572,3 +572,27 @@ async def test_async_predict():
dspy.settings.configure(lm=DummyLM([{"answer": "Paris"}]))
result = await program.acall(question="What is the capital of France?")
assert result.answer == "Paris"
+
+
+def test_predicted_outputs_piped_from_predict_to_lm_call():
+ program = Predict("question -> answer")
+ dspy.settings.configure(lm=dspy.LM("openai/gpt-4o-mini"))
+
+ with patch("litellm.completion") as mock_completion:
+ program(
+ question="Why did a chicken cross the kitchen?",
+ prediction={"type": "content", "content": "A chicken crossing the kitchen"},
+ )
+
+ assert mock_completion.call_args[1]["prediction"] == {
+ "type": "content",
+ "content": "A chicken crossing the kitchen",
+ }
+
+ # If the signature has prediction as an input field, and the prediction is not set as the standard predicted output
+ # format, it should not be passed to the LM.
+ program = Predict("question, prediction -> judgement")
+ with patch("litellm.completion") as mock_completion:
+ program(question="Why did a chicken cross the kitchen?", prediction="To get to the other side!")
+
+ assert "prediction" not in mock_completion.call_args[1]