Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions dspy/signatures/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't render on mkdocs, let's use the code block wrapped by triple backtick.

Meanwhile, let's make code example runnable, currently there is no definition of MySig so the code just breaks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.

  • Updated docstring example to use triple backtick code block (valid Markdown for mkdocs).
  • Added a runnable example (MySig defined explicitly, tested manually and passes).

```
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
Expand Down