DSPy: The framework for programming—not prompting—language models
Python
test output from make_example
Thomas D Ahleb7dca2453050
diff --git a/dspy/functional/functional.py b/dspy/functional/functional.pyindex c1dcec9a7..15cf41a27 100644--- a/dspy/functional/functional.py+++ b/dspy/functional/functional.py@@ -72,12 +72,18 @@ class TypedPredictor(dspy.Module):@staticmethoddef _make_example(type_):# Note: DSPy will cache this call so we only pay the first time TypedPredictor is called.- return dspy.Predict(+ json_object = dspy.Predict(dspy.Signature("json_schema -> json_object","Make a very succinct json object that validates with the following schema",))(json_schema=json.dumps(type_.model_json_schema())).json_object+ # We use the model_validate_json method to make sure the example is valid+ try:+ type_.model_validate_json(_unwrap_json(json_object))+ except (pydantic.ValidationError, ValueError):+ return "" # Unable to make an example+ return json_object# TODO: Another fun idea is to only (but automatically) do this if the output fails.# We could also have a more general "suggest solution" prompt that tries to fix the output# More directly.diff --git a/tests/functional/test_functional.py b/tests/functional/test_functional.pyindex 6cb005812..438891a55 100644--- a/tests/functional/test_functional.py+++ b/tests/functional/test_functional.py@@ -5,6 +5,7 @@ from pydantic import Field, BaseModel, field_validatorfrom typing import Annotatedimport warningsfrom typing import List+from pyparsing import Literalimport pytest@@ -47,23 +48,6 @@ def test_list_output():assert question == expected-def test_list_output():- @predictor- def hard_question(topic: str) -> List[str]:- """Think of a hard factual question about a topic."""-- expected = ["What is the speed of light?", "What is the speed of sound?"]- lm = DummyLM(- ['{"value": ["What is the speed of light?", "What is the speed of sound?"]}']- )- dspy.settings.configure(lm=lm)-- question = hard_question(topic="Physics")- lm.inspect_history(n=2)-- assert question == expected--def test_simple_type():class Question(pydantic.BaseModel):value: str