diff --git a/dspy/signatures/signature.py b/dspy/signatures/signature.py index 236a774ddd..f33df96ef0 100644 --- a/dspy/signatures/signature.py +++ b/dspy/signatures/signature.py @@ -245,6 +245,31 @@ class Signature(BaseModel, metaclass=SignatureMeta): @classmethod def with_instructions(cls, instructions: str) -> type["Signature"]: + """Return a new Signature class with identical fields and new instructions. + + This method does not mutate `cls`. It constructs a fresh Signature + class using the current fields and the provided ``instructions``. + + Args: + instructions (str): Instruction text to attach to the new signature. + + Returns: + type[Signature]: A new Signature class whose fields match `cls.fields` + and whose instructions equal ``instructions``. + + Example: + ``` + from dspy.signatures import Signature, InputField, OutputField + + class MySig(Signature): + input_text: str = InputField(desc="Input text") + output_text: str = OutputField(desc="Output text") + + NewSig = MySig.with_instructions("Translate to French.") + assert NewSig is not MySig + assert NewSig.instructions == "Translate to French." + ``` + """ return Signature(cls.fields, instructions) @classmethod