DSPy: The framework for programming—not prompting—language models
Python
Silence missing-input warning for optional fields (#9834)
Isaac Miller17928f8150e0
diff --git a/dspy/predict/predict.py b/dspy/predict/predict.pyindex a7247b3c8..2018cffaa 100644--- a/dspy/predict/predict.py+++ b/dspy/predict/predict.py@@ -1,7 +1,7 @@import loggingimport randomimport types-from typing import Any, Literal, Union, get_args, get_origin+from typing import Annotated, Any, Literal, Union, get_args, get_originfrom pydantic import BaseModelfrom pydantic_core import PydanticUndefined@@ -216,9 +216,13 @@ class Predict(Module, Parameter):value,)- if not all(k in kwargs for k in signature.input_fields):+ missing = [+ k+ for k, field_info in signature.input_fields.items()+ if k not in kwargs and not _annotation_allows_none(field_info.annotation)+ ]+ if missing: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]logger.warning("Not all input fields were provided to module. Present: %s. Missing: %s.",present,@@ -305,6 +309,23 @@ def _get_type_name(type_annotation) -> str:return getattr(origin, "__name__", str(origin))++def _annotation_allows_none(annotation: Any) -> bool:+ origin = get_origin(annotation)+ args = get_args(annotation)++ if annotation is None or annotation is type(None):+ return True++ if origin is Annotated:+ return bool(args) and _annotation_allows_none(args[0])++ if origin is Union or origin is types.UnionType:+ return any(_annotation_allows_none(arg) for arg in args)++ return False++def _is_value_compatible_with_type(value: Any, expected: type) -> bool:"""Return True if the value matches the expected type hint."""# Special handle list[str] because we allow setting input type to str, however, invoking with a list thereof.diff --git a/tests/predict/test_predict.py b/tests/predict/test_predict.pyindex cdf414376..65e61504d 100644--- a/tests/predict/test_predict.py+++ b/tests/predict/test_predict.py@@ -1104,6 +1104,39 @@ def test_extra_fields_warning(caplog):assert "extra_field" in caplog.text+def test_missing_optional_input_field_no_warning(caplog):+ log_test_helper()++ class OptionalInputSignature(dspy.Signature):+ question: str = dspy.InputField()+ context: str | None = dspy.InputField()+ answer: str = dspy.OutputField()++ predict_instance = Predict(OptionalInputSignature)++ with caplog.at_level(logging.WARNING, logger="dspy.predict.predict"):+ predict_instance(question="test")++ assert "Not all input fields were provided" not in caplog.text+++def test_missing_required_input_field_still_warns(caplog):+ log_test_helper()++ class OptionalInputSignature(dspy.Signature):+ question: str = dspy.InputField()+ context: str | None = dspy.InputField()+ answer: str = dspy.OutputField()++ predict_instance = Predict(OptionalInputSignature)++ with caplog.at_level(logging.WARNING, logger="dspy.predict.predict"):+ predict_instance()++ assert "Not all input fields were provided" in caplog.text+ assert "Missing: ['question']" in caplog.text++def test_warning_images(caplog):"""Test whether type mismatch for images generates a warning."""log_test_helper()