fix: support freeform stopReason in sampling#327
fix: support freeform stopReason in sampling#327LucaButBoring wants to merge 3 commits intomodelcontextprotocol:mainfrom
Conversation
Previously, sampling response parsing would fail if a stopReason was provided besides endTurn/stopSequence/maxTokens. This fixes that to map the arbitrary values to UNKNOWN instead. Spec: https://github.com/modelcontextprotocol/modelcontextprotocol/blob/f5ccad944fdf2b7d9cc70cf817f66ca5a8aa03a4/schema/2024-11-05/schema.ts#L807
tzolov
left a comment
There was a problem hiding this comment.
@LucaButBoring, thanks for addressing this.
The UNKNOWN approach masks the original stop reason. I'm not sure how important the exact stop-reason is for downstream applications?
A better approach would be relaxing the stop reason type to String, but this would be a breaking change. What do you think?
Yes, I was thinking about this - the reason I added public sealed class StopReason permits EndTurn, StopSequence, MaxTokens, Unknown {
private String value;
public StopReason(String value) {
this.value = value;
}
public class EndTurn : StopReason {
public EndTurn() {
super("endTurn");
}
}
public class StopSequence : StopReason {
public EndTurn() {
super("stopSequence");
}
}
public class MaxTokens : StopReason {
public MaxTokens() {
super("maxTokens");
}
}
public class Unknown : StopReason {
public Unknown(String value) {
super(value);
}
public String getValue() {
return value;
}
}
}That way consumers could handle it like so: if (stopReason instanceof EndTurn) {
// ...
} /* other cases */ else if (stopReason instanceof Unknown unknownStopReason) {
log.info("Sampling stopped due to unhandled stop reason: {}", unknownStopReason)
}I'll defer to you, though - if we're willing to make a breaking change for this and prefer that it just become a |
|
I like the suggested approach, but lets postpone the breaking change until it is clearly needed. |
- Add string values and JsonCreator method to StopReason enum - Map unknown stop reason values to UNKNOWN constant for forward compatibility - Add test coverage for unknown stop reason deserialization Spec: https://github.com/modelcontextprotocol/modelcontextprotocol/blob/f5ccad944fdf2b7d9cc70cf817f66ca5a8aa03a4/schema/2024-11-05/schema.ts#L807 Resolves #328
|
Rebased, squashed and merged at aff24fd |
Supports freeform stop reasons in sampling responses by mapping them to a new
UNKNOWNvalue.Motivation and Context
Previously, sampling response parsing would fail if a stopReason was provided besides endTurn/stopSequence/maxTokens. This fixes that to map the arbitrary values to UNKNOWN instead.
Spec: https://github.com/modelcontextprotocol/modelcontextprotocol/blob/f5ccad944fdf2b7d9cc70cf817f66ca5a8aa03a4/schema/2024-11-05/schema.ts#L807
How Has This Been Tested?
Unit tests
Breaking Changes
None
Types of changes
Checklist
Additional context
Closes #328