DSPy: The framework for programming—not prompting—language models
Python
fix(LM): Change default temperature and max_tokens to be None (#8908)
Isaac Miller15cbbd4df765
diff --git a/dspy/clients/lm.py b/dspy/clients/lm.pyindex 5c40e7812..1705ab7be 100644--- a/dspy/clients/lm.py+++ b/dspy/clients/lm.py@@ -31,8 +31,8 @@ class LM(BaseLM):self,model: str,model_type: Literal["chat", "text", "responses"] = "chat",- temperature: float = 0.0,- max_tokens: int = 4000,+ temperature: float | None = None,+ max_tokens: int | None = None,cache: bool = True,callbacks: list[BaseCallback] | None = None,num_retries: int = 3,@@ -88,9 +88,10 @@ class LM(BaseLM):model_pattern = re.match(r"^(?:o[1345]|gpt-5)(?:-(?:mini|nano))?", model_family)if model_pattern:- if max_tokens < 16000 or temperature != 1.0:++ if (temperature and temperature != 1.0) or (max_tokens and max_tokens < 16000):raise ValueError(- "OpenAI's reasoning models require passing temperature=1.0 and max_tokens >= 16000 to "+ "OpenAI's reasoning models require passing temperature=1.0 or None and max_tokens >= 16000 or None to ""`dspy.LM(...)`, e.g., dspy.LM('openai/gpt-5', temperature=1.0, max_tokens=16000)")self.kwargs = dict(temperature=temperature, max_completion_tokens=max_tokens, **kwargs)diff --git a/tests/clients/test_lm.py b/tests/clients/test_lm.pyindex 5c7d5e5a0..87712e7e5 100644--- a/tests/clients/test_lm.py+++ b/tests/clients/test_lm.py@@ -318,7 +318,7 @@ def test_reasoning_model_requirements(model_name):# Should raise assertion error if temperature or max_tokens requirements not metwith pytest.raises(ValueError,- match="reasoning models require passing temperature=1.0 and max_tokens >= 16000",+ match="reasoning models require passing temperature=1.0 or None and max_tokens >= 16000 or None",):dspy.LM(model=model_name,@@ -334,6 +334,13 @@ def test_reasoning_model_requirements(model_name):)assert lm.kwargs["max_completion_tokens"] == 16_000+ # Should pass with no parameters+ lm = dspy.LM(+ model=model_name,+ )+ assert lm.kwargs["temperature"] == None+ assert lm.kwargs["max_completion_tokens"] == None+def test_dump_state():lm = dspy.LM(