[Bugfix][Rust Frontend] Omit unset sampling params in render output (#61220)
Signed-off-by: Martin Hickey <martin.hickey@ie.ibm.com>
This commit is contained in:
@@ -1374,6 +1374,40 @@ async fn render_chat_returns_generate_request_with_header_request_id() {
|
||||
assert!(json.get("mm_features").is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn render_chat_omits_unset_sampling_params() {
|
||||
let mut app = test_render_app();
|
||||
let response = app
|
||||
.call(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri("/v1/chat/completions/render")
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(
|
||||
json!({
|
||||
"model": "render-model",
|
||||
"messages": [{"role": "user", "content": "hello"}],
|
||||
"max_completion_tokens": 8
|
||||
})
|
||||
.to_string(),
|
||||
))
|
||||
.expect("build request"),
|
||||
)
|
||||
.await
|
||||
.expect("call app");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let body = to_bytes(response.into_body(), usize::MAX).await.expect("read body");
|
||||
let json: serde_json::Value = serde_json::from_slice(&body).expect("decode json");
|
||||
|
||||
let sampling_params = json["sampling_params"].as_object().expect("sampling_params object");
|
||||
assert!(
|
||||
sampling_params.values().all(|value| !value.is_null()),
|
||||
"unexpected null in {sampling_params:?}"
|
||||
);
|
||||
assert_eq!(sampling_params["max_tokens"], 8);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn render_chat_uses_processor_owned_parser_selections() {
|
||||
let mut app = test_render_app_with_parser_selections(
|
||||
|
||||
@@ -65,6 +65,7 @@ impl Default for Prompt {
|
||||
///
|
||||
/// Original Python definition:
|
||||
/// <https://github.com/vllm-project/vllm/blob/f22d6e026798a74e6542a52ef776c054f2de572a/vllm/sampling_params.py#L155-L291>
|
||||
#[serde_with::skip_serializing_none]
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct SamplingParams {
|
||||
@@ -320,4 +321,23 @@ mod tests {
|
||||
assert!(err.to_string().contains("stop strings cannot be empty"));
|
||||
assert!(err.to_string().contains("req-empty-stop"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sampling_params_serialization_omits_unset_fields() {
|
||||
let params = SamplingParams {
|
||||
max_tokens: Some(64),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let value = serde_json::to_value(¶ms).unwrap();
|
||||
let obj = value.as_object().unwrap();
|
||||
assert!(
|
||||
obj.values().all(|v| !v.is_null()),
|
||||
"unexpected null in {value}"
|
||||
);
|
||||
assert_eq!(obj["max_tokens"], 64);
|
||||
|
||||
let back: SamplingParams = serde_json::from_value(value).unwrap();
|
||||
assert_eq!(back, params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,6 +132,23 @@ def test_reasoning_parser_kwargs_rejects_unknown_keys():
|
||||
GenerateRequest.model_validate(payload)
|
||||
|
||||
|
||||
def test_rust_render_sampling_params_without_nulls_validate():
|
||||
"""Rust render omits unset sampling fields; Python generate rejects nulls."""
|
||||
sampling_params = {"max_tokens": 64, "watermarking": True}
|
||||
request = GenerateRequest.model_validate(
|
||||
{"token_ids": [1, 2, 3], "sampling_params": sampling_params}
|
||||
)
|
||||
assert request.sampling_params.max_tokens == 64
|
||||
|
||||
with pytest.raises(ValidationError, match="temperature"):
|
||||
GenerateRequest.model_validate(
|
||||
{
|
||||
"token_ids": [1, 2, 3],
|
||||
"sampling_params": {**sampling_params, "temperature": None},
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def test_output_mode_defaults_to_tokens():
|
||||
assert GenerateRequest.model_validate(_base_payload()).output_mode == "tokens"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user