DSPy: The framework for programming—not prompting—language models
Python
Raise explicit error on not meeting reasoning model's requirement (#8650)
Chen Qianf3de1cdfdc47
diff --git a/dspy/clients/lm.py b/dspy/clients/lm.pyindex abc97a845..37d64090f 100644--- a/dspy/clients/lm.py+++ b/dspy/clients/lm.py@@ -82,10 +82,11 @@ class LM(BaseLM):model_pattern = re.match(r"^(?:o([1345])|gpt-(5))(?:-mini)?", model_family)if model_pattern:- # Handle OpenAI reasoning models (o1, o3)- assert (- max_tokens >= 20_000 and temperature == 1.0- ), "OpenAI's reasoning models require passing temperature=1.0 and max_tokens >= 20_000 to `dspy.LM(...)`"+ if max_tokens < 20000 or temperature != 1.0:+ raise ValueError(+ "OpenAI's reasoning models require passing temperature=1.0 and max_tokens >= 20000 to "+ "`dspy.LM(...)`, e.g., dspy.LM('openai/gpt-5o', temperature=1.0, max_tokens=20000)"+ )self.kwargs = dict(temperature=temperature, max_completion_tokens=max_tokens, **kwargs)else:self.kwargs = dict(temperature=temperature, max_tokens=max_tokens, **kwargs)diff --git a/tests/clients/test_lm.py b/tests/clients/test_lm.pyindex 8aa4c89dc..1385c990c 100644--- a/tests/clients/test_lm.py+++ b/tests/clients/test_lm.py@@ -219,13 +219,12 @@ def test_reasoning_model_token_parameter():def test_reasoning_model_requirements():# Should raise assertion error if temperature or max_tokens requirements not met- with pytest.raises(AssertionError) as exc_info:+ with pytest.raises(ValueError, match="reasoning models require passing temperature=1.0 and max_tokens >= 20000"):dspy.LM(model="openai/o1",temperature=0.7, # Should be 1.0max_tokens=1000, # Should be >= 20_000)- assert "reasoning models require passing temperature=1.0 and max_tokens >= 20_000" in str(exc_info.value)# Should pass with correct parameterslm = dspy.LM(