Skip to content

Support predicted output in DSPy #8247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions dspy/predict/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ def _forward_preprocess(self, **kwargs):
if (temperature is None or temperature <= 0.15) and num_generations > 1:
config["temperature"] = 0.7

if "prediction" in kwargs:
if (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to merge for now but:

  1. Should we just check (or maybe also check?) whether prediction is a key in the signature?

  2. I suppose the cleanest way for this would have been to make it a new type and handle it in adapters 😅 But this would start to feel like too many types...

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]
Expand Down
24 changes: 24 additions & 0 deletions tests/predict/test_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]