DSPy: The framework for programming—not prompting—language models
Python
Adjust reasoning model token requirement (#8750)
Omar Khattab9725d4de612c
diff --git a/dspy/adapters/two_step_adapter.py b/dspy/adapters/two_step_adapter.pyindex 9e3f7bbb1..b78375f14 100644--- a/dspy/adapters/two_step_adapter.py+++ b/dspy/adapters/two_step_adapter.py@@ -30,7 +30,7 @@ class TwoStepAdapter(Adapter):Example:```import dspy- lm = dspy.LM(model="openai/o3-mini", max_tokens=10000, temperature = 1.0)+ lm = dspy.LM(model="openai/o3-mini", max_tokens=16000, temperature = 1.0)adapter = dspy.TwoStepAdapter(dspy.LM("openai/gpt-4o-mini"))dspy.configure(lm=lm, adapter=adapter)program = dspy.ChainOfThought("question->answer")diff --git a/dspy/clients/lm.py b/dspy/clients/lm.pyindex 02a532fda..b04ac1ac9 100644--- a/dspy/clients/lm.py+++ b/dspy/clients/lm.py@@ -86,10 +86,10 @@ class LM(BaseLM):model_pattern = re.match(r"^(?:o[1345]|gpt-5)(?:-(?:mini|nano))?", model_family)if model_pattern:- if max_tokens < 20000 or temperature != 1.0:+ if max_tokens < 16000 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-5', temperature=1.0, max_tokens=20000)"+ "OpenAI's reasoning models require passing temperature=1.0 and max_tokens >= 16000 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)if self.kwargs.get("rollout_id") is None:diff --git a/tests/clients/test_lm.py b/tests/clients/test_lm.pyindex fbe626f60..3bafc72ea 100644--- a/tests/clients/test_lm.py+++ b/tests/clients/test_lm.py@@ -269,12 +269,12 @@ def test_reasoning_model_token_parameter():lm = dspy.LM(model=model_name,temperature=1.0 if is_reasoning_model else 0.7,- max_tokens=20_000 if is_reasoning_model else 1000,+ max_tokens=16_000 if is_reasoning_model else 1000,)if is_reasoning_model:assert "max_completion_tokens" in lm.kwargsassert "max_tokens" not in lm.kwargs- assert lm.kwargs["max_completion_tokens"] == 20_000+ assert lm.kwargs["max_completion_tokens"] == 16_000else:assert "max_completion_tokens" not in lm.kwargsassert "max_tokens" in lm.kwargs@@ -285,21 +285,21 @@ 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 >= 20000",+ match="reasoning models require passing temperature=1.0 and max_tokens >= 16000",):dspy.LM(model=model_name,temperature=0.7, # Should be 1.0- max_tokens=1000, # Should be >= 20_000+ max_tokens=1000, # Should be >= 16_000)# Should pass with correct parameterslm = dspy.LM(model=model_name,temperature=1.0,- max_tokens=20_000,+ max_tokens=16_000,)- assert lm.kwargs["max_completion_tokens"] == 20_000+ assert lm.kwargs["max_completion_tokens"] == 16_000def test_dump_state():