DSPy: The framework for programming—not prompting—language models
Python
Fix rollout_id warning for default LM temperature (#9316)
Omar Khattab26bfd249aa67
diff --git a/dspy/clients/lm.py b/dspy/clients/lm.pyindex 53582e1b7..5b027b211 100644--- a/dspy/clients/lm.py+++ b/dspy/clients/lm.py@@ -111,7 +111,7 @@ class LM(BaseLM):self._warn_zero_temp_rollout(self.kwargs.get("temperature"), self.kwargs.get("rollout_id"))def _warn_zero_temp_rollout(self, temperature: float | None, rollout_id):- if not self._warned_zero_temp_rollout and rollout_id is not None and (temperature is None or temperature == 0):+ if not self._warned_zero_temp_rollout and rollout_id is not None and temperature == 0:warnings.warn("rollout_id has no effect when temperature=0; set temperature>0 to bypass the cache.",stacklevel=3,diff --git a/tests/clients/test_lm.py b/tests/clients/test_lm.pyindex 15e0e02fd..71e5ee5a8 100644--- a/tests/clients/test_lm.py+++ b/tests/clients/test_lm.py@@ -190,7 +190,7 @@ def test_zero_temperature_rollout_warns_once(monkeypatch):monkeypatch.setattr(litellm, "completion", fake_completion)- lm = dspy.LM(model="openai/dspy-test-model", model_type="chat")+ lm = dspy.LM(model="openai/dspy-test-model", model_type="chat", temperature=0)with pytest.warns(UserWarning, match="rollout_id has no effect"):lm("Query", rollout_id=1)with warnings.catch_warnings(record=True) as record:@@ -199,6 +199,23 @@ def test_zero_temperature_rollout_warns_once(monkeypatch):assert len(record) == 0+def test_rollout_id_with_default_temperature_does_not_warn(monkeypatch):+ def fake_completion(*, cache, num_retries, retry_strategy, **request):+ return ModelResponse(+ choices=[Choices(message=Message(role="assistant", content="Hi!"))],+ usage={"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2},+ model="openai/gpt-5-nano",+ )++ monkeypatch.setattr(litellm, "completion", fake_completion)++ with warnings.catch_warnings(record=True) as record:+ warnings.simplefilter("always")+ lm = dspy.LM(model="openai/gpt-5-nano", model_type="chat", rollout_id=1)+ lm("Query")+ assert len(record) == 0++def test_text_lms_can_be_queried(litellm_test_server):api_base, _ = litellm_test_serverexpected_response = ["Hi!"]