DSPy: The framework for programming—not prompting—language models
Python
Revert ChainOfThought to pre-dspy.Reasoning (#8822) behavior (#9258)
Omar Khattab4ef729d2c3d1
diff --git a/dspy/predict/chain_of_thought.py b/dspy/predict/chain_of_thought.pyindex 189c5c914..965afe694 100644--- a/dspy/predict/chain_of_thought.py+++ b/dspy/predict/chain_of_thought.py@@ -1,18 +1,20 @@-import loggingfrom typing import Any+from pydantic.fields import FieldInfo+import dspyfrom dspy.primitives.module import Module-from dspy.signatures.field import OutputFieldfrom dspy.signatures.signature import Signature, ensure_signature-logger = logging.getLogger(__name__)+# NOTE: This restores the legacy rationale_field behavior after PR #8822.class ChainOfThought(Module):def __init__(self,signature: str | type[Signature],+ rationale_field: FieldInfo | None = None,+ rationale_field_type: type = str,**config: dict[str, Any],):"""@@ -20,17 +22,17 @@ class ChainOfThought(Module):Args:signature (Type[dspy.Signature]): The signature of the module.+ rationale_field (Optional[Union[dspy.OutputField, pydantic.fields.FieldInfo]]): The field that will contain the reasoning.+ rationale_field_type (Type): The type of the rationale field.**config: The configuration for the module."""super().__init__()signature = ensure_signature(signature)-- if "rationale_field" in config or "rationale_field_type" in config:- logger.warning("`rationale_field` and `rationale_field_type` are deprecated, they are no-op now.")-- from dspy.adapters.types.reasoning import Reasoning-- extended_signature = signature.prepend(name="reasoning", field=OutputField(), type_=Reasoning)+ prefix = "Reasoning: Let's think step by step in order to"+ desc = "${reasoning}"+ rationale_field_type = rationale_field.annotation if rationale_field else rationale_field_type+ rationale_field = rationale_field if rationale_field else dspy.OutputField(prefix=prefix, desc=desc)+ extended_signature = signature.prepend(name="reasoning", field=rationale_field, type_=rationale_field_type)self.predict = dspy.Predict(extended_signature, **config)def forward(self, **kwargs):diff --git a/tests/adapters/test_reasoning.py b/tests/adapters/test_reasoning.pyindex ed146b8d5..90812b81c 100644--- a/tests/adapters/test_reasoning.py+++ b/tests/adapters/test_reasoning.py@@ -97,7 +97,7 @@ def test_reasoning_with_chain_of_thought():result = cot(question="What is the answer?")# Test that we can use string methods on result.reasoning- assert isinstance(result.reasoning, dspy.Reasoning)+ assert isinstance(result.reasoning, str)assert result.reasoning.strip() == "Let me think step by step"assert result.reasoning.lower() == "let me think step by step"assert "step by step" in result.reasoningdiff --git a/tests/predict/test_chain_of_thought.py b/tests/predict/test_chain_of_thought.pyindex 3000ffe8a..a8dbb70ba 100644--- a/tests/predict/test_chain_of_thought.py+++ b/tests/predict/test_chain_of_thought.py@@ -29,7 +29,7 @@ async def test_async_chain_of_thought():def test_chain_of_thought_with_native_reasoning():- """Test ChainOfThought with native reasoning support where LM returns reasoning natively."""+ """Test ChainOfThought with a model that supports native reasoning, but using manual fields."""lm = dspy.LM(model="anthropic/claude-3-7-sonnet-20250219", cache=False)dspy.settings.configure(lm=lm)@@ -39,8 +39,10 @@ def test_chain_of_thought_with_native_reasoning():choices=[Choices(message=Message(- content="[[ ## answer ## ]]\nParis\n[[ ## completion ## ]]",- reasoning_content="Step-by-step thinking about the capital of France",+ content=(+ "[[ ## reasoning ## ]]\nStep-by-step thinking about the capital of France\n"+ "[[ ## answer ## ]]\nParis\n[[ ## completion ## ]]"+ )),)],@@ -50,13 +52,10 @@ def test_chain_of_thought_with_native_reasoning():cot = ChainOfThought("question -> answer")result = cot(question="What is the capital of France?")assert result.answer == "Paris"- assert isinstance(result.reasoning, dspy.Reasoning)- assert result.reasoning.content == "Step-by-step thinking about the capital of France"+ assert isinstance(result.reasoning, str)+ assert result.reasoning == "Step-by-step thinking about the capital of France"- # Check that the reasoning_effort is automatically set to "low" when the LM supports native reasoning and not- # provided in the LM kwargsargs, kwargs = mock_completion.call_args- assert kwargs["reasoning_effort"] == "low"def test_chain_of_thought_with_manual_reasoning():@@ -83,4 +82,4 @@ def test_chain_of_thought_with_manual_reasoning():cot = ChainOfThought("question -> answer")result = cot(question="What is the capital of France?")assert result.answer == "Paris"- assert result.reasoning.content == "Step-by-step thinking about the capital of France"+ assert result.reasoning == "Step-by-step thinking about the capital of France"