File tree Expand file tree Collapse file tree 2 files changed +38
-1
lines changed Expand file tree Collapse file tree 2 files changed +38
-1
lines changed Original file line number Diff line number Diff line change @@ -99,7 +99,22 @@ def _forward_preprocess(self, **kwargs):
99
99
100
100
# Get the right LM to use.
101
101
lm = kwargs .pop ("lm" , self .lm ) or settings .lm
102
- assert isinstance (lm , BaseLM ), "No LM is loaded."
102
+
103
+ if lm is None :
104
+ raise ValueError (
105
+ "No LM is loaded. Please configure the LM using `dspy.configure(lm=dspy.LM(...))`. e.g, "
106
+ "`dspy.configure(lm=dspy.LM('openai/gpt-4o-mini'))`"
107
+ )
108
+
109
+ if isinstance (lm , str ):
110
+ # Many users mistakenly use `dspy.configure(lm="openai/gpt-4o-mini")` instead of
111
+ # `dspy.configure(lm=dspy.LM("openai/gpt-4o-mini"))`, so we are providing a specific error message.
112
+ raise ValueError (
113
+ f"LM must be an instance of `dspy.BaseLM`, not a string. Instead of using a string like "
114
+ f"'dspy.configure(lm=\" { lm } \" )', please configure the LM like 'dspy.configure(lm=dspy.LM(\" { lm } \" ))'"
115
+ )
116
+ elif not isinstance (lm , BaseLM ):
117
+ raise ValueError (f"LM must be an instance of `dspy.BaseLM`, not { type (lm )} . Received `lm={ lm } `." )
103
118
104
119
# If temperature is unset or <=0.15, and n > 1, set temperature to 0.7 to keep randomness.
105
120
temperature = config .get ("temperature" ) or lm .kwargs .get ("temperature" )
Original file line number Diff line number Diff line change @@ -582,6 +582,28 @@ def test_positional_arguments():
582
582
)
583
583
584
584
585
+ def test_error_message_on_invalid_lm_setup ():
586
+ # No LM is loaded.
587
+ with pytest .raises (ValueError , match = "No LM is loaded" ):
588
+ Predict ("question -> answer" )(question = "Why did a chicken cross the kitchen?" )
589
+
590
+ # LM is a string.
591
+ dspy .configure (lm = "openai/gpt-4o-mini" )
592
+ with pytest .raises (ValueError ) as e :
593
+ Predict ("question -> answer" )(question = "Why did a chicken cross the kitchen?" )
594
+
595
+ assert "LM must be an instance of `dspy.BaseLM`, not a string." in str (e .value )
596
+
597
+ def dummy_lm ():
598
+ pass
599
+
600
+ # LM is not an instance of dspy.BaseLM.
601
+ dspy .configure (lm = dummy_lm )
602
+ with pytest .raises (ValueError ) as e :
603
+ Predict ("question -> answer" )(question = "Why did a chicken cross the kitchen?" )
604
+ assert "LM must be an instance of `dspy.BaseLM`, not <class 'function'>." in str (e .value )
605
+
606
+
585
607
@pytest .mark .parametrize ("adapter_type" , ["chat" , "json" ])
586
608
def test_field_constraints (adapter_type ):
587
609
class SpyLM (dspy .LM ):
You can’t perform that action at this time.
0 commit comments