Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

fix(dspy): reject declaring null Score (#10538)

Ilia Iliev4c6448e035df
diff --git a/docs/docs/api/experimental/DecisionTypes.md b/docs/docs/api/experimental/DecisionTypes.md
index e756a4bfb..c6d7d243f 100644
--- a/docs/docs/api/experimental/DecisionTypes.md
+++ b/docs/docs/api/experimental/DecisionTypes.md
@@ -150,7 +150,7 @@ overrides can change descriptions, not the declared answer space.
## Structured criteria
-Descriptions in all three types accept JSON strings, objects, arrays, or null.
+Descriptions accept JSON strings, objects, or arrays. Only Noul and Choice allow null.
```python
Urgency = Noul[(True, {"what": "Service blocked", "examples": ["Cannot log in"]}), (False, "Usable")]
diff --git a/dspy/adapters/decision_state.py b/dspy/adapters/decision_state.py
index 20191c51b..198567840 100644
--- a/dspy/adapters/decision_state.py
+++ b/dspy/adapters/decision_state.py
@@ -141,7 +141,12 @@ class DecisionState:
elif issubclass(kind, Choice):
valid = isinstance(criteria, dict) and criteria.keys() == kind.criteria().keys()
else:
- valid = isinstance(criteria, list) and len(criteria) == len(kind.criteria()) and 2 <= len(criteria) <= 10
+ valid = (
+ isinstance(criteria, list)
+ and len(criteria) == len(kind.criteria())
+ and 2 <= len(criteria) <= 10
+ and None not in criteria
+ )
if not valid:
raise ValueError(f"Invalid criteria for {name!r}: must match the declared decision type and options.")
entries = criteria.values() if isinstance(criteria, dict) else criteria or []
diff --git a/dspy/adapters/types/decision.py b/dspy/adapters/types/decision.py
index e738456ba..450726ebe 100644
--- a/dspy/adapters/types/decision.py
+++ b/dspy/adapters/types/decision.py
@@ -103,6 +103,8 @@ class Score(_Decision):
def __class_getitem__(cls, options):
if not isinstance(options, tuple) or len(options) < 2:
raise ValueError("Score requires at least two ordered level descriptions, e.g. Score['poor', 'excellent'].")
+ if None in options:
+ raise ValueError("Score level descriptions cannot be null.")
return _score_type(tuple(_description_json(desc) for desc in options))
@classmethod
diff --git a/tests/adapters/test_decision_types.py b/tests/adapters/test_decision_types.py
index c0e67a24f..b3b449b35 100644
--- a/tests/adapters/test_decision_types.py
+++ b/tests/adapters/test_decision_types.py
@@ -257,6 +257,13 @@ def test_structured_descriptions_reject_non_json(kind, description):
kind[True, description]
+def test_score_rejects_null_level_descriptions():
+ with pytest.raises(ValueError, match="null"):
+ Score[None, "fair", "great"]
+ module = dspy.Predict(dspy.Signature({"rating": (Score["bad", "fair", "great"], dspy.OutputField(desc="Rate."))}))
+ with pytest.raises(ValueError, match="rating"):
+ module.set_criteria("rating", [None, "fair", "great"])
+
@pytest.mark.parametrize("rich", [False, True])
@pytest.mark.parametrize("adapter", [dspy.ChatAdapter(), dspy.JSONAdapter()])
def test_structured_criteria_reach_both_backends_and_survive_save_load(adapter, rich, tmp_path):
@@ -265,7 +272,7 @@ def test_structured_criteria_reach_both_backends_and_survive_save_load(adapter,
technical = {"what": "Product bug", "not_for": "Billing"}
urgent = Noul[(True, yes), (False, no)]
category = Choice[("billing", {}), ("technical", technical)]
- severity = Score[None, ["Disruptive"], {"what": "Blocking"}]
+ severity = Score["", ["Disruptive"], {"what": "Blocking"}]
sig = dspy.Signature(
{
"ticket": (str, dspy.InputField()),
@@ -281,7 +288,7 @@ def test_structured_criteria_reach_both_backends_and_survive_save_load(adapter,
expected = {
"urgent": {"true": yes, "false": no},
"category": {"billing": {}, "technical": technical},
- "severity": [None, ["Disruptive"], {"what": "Blocking"}],
+ "severity": ["", ["Disruptive"], {"what": "Blocking"}],
}
evidence = {
"urgent": {"noul": 0.8},