Skip to content

Provide a standard base class for creating custom Signature field type #8217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 17, 2025

Conversation

chenmoneygithub
Copy link
Collaborator

Modifying from #8210 by @arnavsinghvi11.

dspy.BaseType will be the base class for custom types for signature usage, including dspy.Image, dspy.Audio and so on.

dspy.History does not subclass from dspy.BaseType because it needs to modify the signature and get formatted as multiturn messages.

# Custom type messages are only in user messages
continue

pattern = r"<<CUSTOM-TYPE-START-IDENTIFIER>>(.*?)<<CUSTOM-TYPE-END-IDENTIFIER>>"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shall we make <CUSTOM-TYPE-START-IDENTIFIER> a const?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes!

pattern = r"<<CUSTOM-TYPE-START-IDENTIFIER>>(.*?)<<CUSTOM-TYPE-END-IDENTIFIER>>"
result = []
last_end = 0
content = message["content"]
Copy link
Collaborator

@TomeHirata TomeHirata May 16, 2025

Choose a reason for hiding this comment

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

Is it a fair assumption that content here is always a single string rather than a content part list?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

With current adapter design, yes, we format user inputs into a string, then split around custom type. Before this PR, we only split around the image tag.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks, can we add a comment for the context?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sg, added!

def format(self) -> Union[list[dict[str, Any]], str]:
try:
url = self.url
if isinstance(url, str) and "<DSPY_IMAGE_START>" in url and "<DSPY_IMAGE_END>" in url:
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: should we make <DSPY_IMAGE_START> and <DSPY_IMAGE_END> a const?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is a condition check for legacy code's compatibility, which we can remove in 1-2 version, so i would prefer not exposing a file-level constant.

expected_image_content = {"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}}
assert expected_image_content in user_message_content

# Test formatting with few-shot examples
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: shall we parameterize or split the test for clear test cases?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Splitting sounds good to me

try:
url = self.url
if isinstance(url, str) and "<DSPY_IMAGE_START>" in url and "<DSPY_IMAGE_END>" in url:
url = url.split("<DSPY_IMAGE_START>", 1)[-1].split("<DSPY_IMAGE_END>", 1)[0]
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: we can use re.search

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I read the existing dspy.Image's code again, and i think we can safely delete this code chunk.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, I was just checking how DSPY_IMAGE_START is used after this code change and reached the same conclusion 😄

Copy link
Collaborator

@TomeHirata TomeHirata left a comment

Choose a reason for hiding this comment

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

LGTM

@okhat okhat merged commit 5e17bcf into stanfordnlp:main May 17, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants