Skip to content

Conversation

TomeHirata
Copy link
Collaborator

@TomeHirata TomeHirata commented Sep 8, 2025

This PR increases the extensibility of dspy.Type and allows users to define a custom type that can be streamed or use native LM response. This is useful when users want to use provider specific response field such as tool call or citation.

Following methods are introduced into dspy.Type

  • is_streamable: Whether the custom type is streamable
  • parse_stream_chunk: Parse a stream chunk into the custom type
  • use_native_response: Whether to use the native response of the LM such as function calling

@TomeHirata TomeHirata changed the title Allow custom dspy type to be streamed Allow custom type to be streamed and use native response Sep 8, 2025
@TomeHirata TomeHirata requested a review from okhat September 8, 2025 08:33
@okhat
Copy link
Collaborator

okhat commented Sep 8, 2025

No concerns on a quick scan. If @chenmoneygithub approves, feel free to merge.

Copy link
Collaborator

@chenmoneygithub chenmoneygithub left a comment

Choose a reason for hiding this comment

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

Very clean and cool! Left some comments for discussion.

@TomeHirata
Copy link
Collaborator Author

@chenmoneygithub I've moved the native response type config to adapter, can you take a look again?

Copy link
Collaborator

@chenmoneygithub chenmoneygithub left a comment

Choose a reason for hiding this comment

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

Some minor comments, otherwise LGTM!

def output_type(self) -> type | None:
try:
return self.predict.signature.output_fields[self.signature_field_name].annotation
except Exception:
Copy link
Collaborator

Choose a reason for hiding this comment

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

when will this throw an exception?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It does not fail in normal cases, but since predict or signature_field_name is not guaranteed to be non-null, it's better to have a safeguard

Copy link
Collaborator

Choose a reason for hiding this comment

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

ic, actually I think predict and signature_field_name must be configured for stream listener, otherwise it is not doing anything.

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 know, but typing-wise they are nullable (

def __init__(
self,
signature_field_name: str,
predict: Any = None,
predict_name: str | None = None,
allow_reuse: bool = False,
), so I still want to keep the check. We can do if chain, as well.

Copy link
Collaborator

@chenmoneygithub chenmoneygithub left a comment

Choose a reason for hiding this comment

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

LGTM after #8778 (comment) is resolved.

@TomeHirata TomeHirata merged commit 10fb579 into stanfordnlp:main Sep 17, 2025
10 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.

3 participants