[Rust Frontend] Use enum-backed domain types for engine outputs and structured outputs (#47283)
Signed-off-by: Bugen Zhao <i@bugenzhao.com>
This commit is contained in:
@@ -47,9 +47,8 @@ pub(super) fn apply_structural_tag_constraint(
|
||||
|
||||
// Overwrite any existing structured output settings with the structural tag constraint.
|
||||
request.sampling_params.structured_outputs = Some(StructuredOutputsParams {
|
||||
structural_tag: Some(structural_tag),
|
||||
backend: StructuredOutputBackend::Xgrammar,
|
||||
..Default::default()
|
||||
..StructuredOutputsParams::structural_tag(structural_tag)
|
||||
});
|
||||
|
||||
Ok(())
|
||||
@@ -119,10 +118,11 @@ mod tests {
|
||||
.as_ref()
|
||||
.expect("structured outputs should be set");
|
||||
assert_eq!(params.backend, StructuredOutputBackend::Xgrammar);
|
||||
serde_json::from_str(
|
||||
params.structural_tag.as_deref().expect("structural_tag should be set"),
|
||||
)
|
||||
.expect("structural_tag should be valid JSON")
|
||||
let structural_tag = params
|
||||
.constraint
|
||||
.as_structural_tag()
|
||||
.expect("structured output constraint should be structural_tag");
|
||||
serde_json::from_str(structural_tag).expect("structural_tag should be valid JSON")
|
||||
}
|
||||
|
||||
fn structured_outputs(request: &ChatRequest) -> &StructuredOutputsParams {
|
||||
@@ -161,9 +161,8 @@ mod tests {
|
||||
fn auto_strict_tool_choice_overwrites_existing_json_guidance() {
|
||||
let mut request = request(ChatToolChoice::Auto, vec![chat_tool("search", Some(true))]);
|
||||
request.sampling_params.structured_outputs = Some(StructuredOutputsParams {
|
||||
json: Some(json!({"type": "object"})),
|
||||
backend: StructuredOutputBackend::Xgrammar,
|
||||
..Default::default()
|
||||
..StructuredOutputsParams::json(json!({"type": "object"}))
|
||||
});
|
||||
let parser = qwen3_coder_parser(&request.tools);
|
||||
|
||||
@@ -171,8 +170,7 @@ mod tests {
|
||||
.expect("structural tag should build");
|
||||
|
||||
let params = structured_outputs(&request);
|
||||
assert!(params.json.is_none());
|
||||
assert!(params.structural_tag.is_some());
|
||||
assert!(params.constraint.is_structural_tag());
|
||||
let tag = structural_tag_value(&request);
|
||||
assert_eq!(tag["type"], "structural_tag");
|
||||
assert!(tag.to_string().contains("search"));
|
||||
@@ -195,9 +193,8 @@ mod tests {
|
||||
fn required_tool_choice_overwrites_existing_json_object_guidance() {
|
||||
let mut request = request(ChatToolChoice::Required, vec![chat_tool("search", None)]);
|
||||
request.sampling_params.structured_outputs = Some(StructuredOutputsParams {
|
||||
json_object: Some(true),
|
||||
backend: StructuredOutputBackend::Xgrammar,
|
||||
..Default::default()
|
||||
..StructuredOutputsParams::json_object()
|
||||
});
|
||||
let parser = qwen3_coder_parser(&request.tools);
|
||||
|
||||
@@ -205,8 +202,7 @@ mod tests {
|
||||
.expect("structural tag should build");
|
||||
|
||||
let params = structured_outputs(&request);
|
||||
assert!(params.json_object.is_none());
|
||||
assert!(params.structural_tag.is_some());
|
||||
assert!(params.constraint.is_structural_tag());
|
||||
let tag = structural_tag_value(&request);
|
||||
assert_eq!(tag["type"], "structural_tag");
|
||||
assert!(tag.to_string().contains("search"));
|
||||
@@ -245,9 +241,8 @@ mod tests {
|
||||
fn none_tool_choice_preserves_existing_json_object_guidance() {
|
||||
let mut request = request(ChatToolChoice::None, vec![chat_tool("search", Some(true))]);
|
||||
request.sampling_params.structured_outputs = Some(StructuredOutputsParams {
|
||||
json_object: Some(true),
|
||||
backend: StructuredOutputBackend::Xgrammar,
|
||||
..Default::default()
|
||||
..StructuredOutputsParams::json_object()
|
||||
});
|
||||
let parser = qwen3_coder_parser(&request.tools);
|
||||
|
||||
@@ -255,7 +250,6 @@ mod tests {
|
||||
.expect("structural tag decision should succeed");
|
||||
|
||||
let params = structured_outputs(&request);
|
||||
assert_eq!(params.json_object, Some(true));
|
||||
assert!(params.structural_tag.is_none());
|
||||
assert!(params.constraint.is_json_object());
|
||||
}
|
||||
}
|
||||
|
||||
+34
-23
@@ -16,7 +16,7 @@ use vllm_engine_core_client::protocol::logprobs::{
|
||||
Logprobs, MaybeWireLogprobs, PositionLogprobs, TokenLogprob,
|
||||
};
|
||||
use vllm_engine_core_client::protocol::output::{
|
||||
EngineCoreFinishReason, EngineCoreOutput, EngineCoreOutputs, StopReason,
|
||||
EngineCoreFinishReason, EngineCoreOutput, EngineCoreOutputs, RequestBatchOutputs, StopReason,
|
||||
};
|
||||
use vllm_engine_core_client::protocol::request::EngineCoreRequest;
|
||||
use vllm_engine_core_client::test_utils::{IpcNamespace, spawn_mock_engine_task};
|
||||
@@ -342,7 +342,7 @@ async fn chat_streams_text_events() {
|
||||
);
|
||||
send_outputs(
|
||||
push,
|
||||
EngineCoreOutputs {
|
||||
RequestBatchOutputs {
|
||||
outputs: vec![
|
||||
request_output("chat-1", vec![b'H' as u32], None, None),
|
||||
request_output(
|
||||
@@ -354,7 +354,8 @@ async fn chat_streams_text_events() {
|
||||
],
|
||||
finished_requests: Some(BTreeSet::from(["chat-1".to_string()])),
|
||||
..Default::default()
|
||||
},
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
.await;
|
||||
})
|
||||
@@ -454,7 +455,7 @@ async fn chat_stream_waits_for_complete_utf8_before_emitting() {
|
||||
let _ = recv_engine_message(dealer).await;
|
||||
send_outputs(
|
||||
push,
|
||||
EngineCoreOutputs {
|
||||
RequestBatchOutputs {
|
||||
outputs: vec![
|
||||
request_output("chat-utf8", bytes_to_token_ids(&[0xe4]), None, None),
|
||||
request_output(
|
||||
@@ -466,7 +467,8 @@ async fn chat_stream_waits_for_complete_utf8_before_emitting() {
|
||||
],
|
||||
finished_requests: Some(BTreeSet::from(["chat-utf8".to_string()])),
|
||||
..Default::default()
|
||||
},
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
.await;
|
||||
})
|
||||
@@ -542,7 +544,7 @@ async fn chat_stream_flushes_held_text_on_finish() {
|
||||
let _ = recv_engine_message(dealer).await;
|
||||
send_outputs(
|
||||
push,
|
||||
EngineCoreOutputs {
|
||||
RequestBatchOutputs {
|
||||
outputs: vec![request_output(
|
||||
"chat-final-flush",
|
||||
bytes_to_token_ids(b"ok st"),
|
||||
@@ -551,7 +553,8 @@ async fn chat_stream_flushes_held_text_on_finish() {
|
||||
)],
|
||||
finished_requests: Some(BTreeSet::from(["chat-final-flush".to_string()])),
|
||||
..Default::default()
|
||||
},
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
.await;
|
||||
})
|
||||
@@ -662,7 +665,7 @@ async fn chat_stream_reports_decode_failure_as_error_event() {
|
||||
let _ = recv_engine_message(dealer).await;
|
||||
send_outputs(
|
||||
push,
|
||||
EngineCoreOutputs {
|
||||
RequestBatchOutputs {
|
||||
outputs: vec![request_output(
|
||||
"chat-4",
|
||||
vec![UNKNOWN_DECODE_TOKEN_ID],
|
||||
@@ -670,7 +673,8 @@ async fn chat_stream_reports_decode_failure_as_error_event() {
|
||||
None,
|
||||
)],
|
||||
..Default::default()
|
||||
},
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
.await;
|
||||
})
|
||||
@@ -725,7 +729,7 @@ async fn chat_stream_preserves_terminal_stop_token_when_requested() {
|
||||
let _ = recv_engine_message(dealer).await;
|
||||
send_outputs(
|
||||
push,
|
||||
EngineCoreOutputs {
|
||||
RequestBatchOutputs {
|
||||
outputs: vec![request_output(
|
||||
"chat-include-stop",
|
||||
vec![b'H' as u32, b'i' as u32, b'!' as u32],
|
||||
@@ -734,7 +738,8 @@ async fn chat_stream_preserves_terminal_stop_token_when_requested() {
|
||||
)],
|
||||
finished_requests: Some(BTreeSet::from(["chat-include-stop".to_string()])),
|
||||
..Default::default()
|
||||
},
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
.await;
|
||||
})
|
||||
@@ -812,7 +817,7 @@ async fn chat_stream_separates_reasoning_blocks_automatically() {
|
||||
let _ = recv_engine_message(dealer).await;
|
||||
send_outputs(
|
||||
push,
|
||||
EngineCoreOutputs {
|
||||
RequestBatchOutputs {
|
||||
outputs: vec![
|
||||
request_output(
|
||||
"chat-reasoning",
|
||||
@@ -841,7 +846,8 @@ async fn chat_stream_separates_reasoning_blocks_automatically() {
|
||||
],
|
||||
finished_requests: Some(BTreeSet::from(["chat-reasoning".to_string()])),
|
||||
..Default::default()
|
||||
},
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
.await;
|
||||
})
|
||||
@@ -955,7 +961,7 @@ async fn chat_collectors_return_structured_message_and_visible_text() {
|
||||
let _ = recv_engine_message(dealer).await;
|
||||
send_outputs(
|
||||
push,
|
||||
EngineCoreOutputs {
|
||||
RequestBatchOutputs {
|
||||
outputs: vec![request_output(
|
||||
"chat-collect",
|
||||
bytes_to_token_ids(b"<think>inner</think>outer"),
|
||||
@@ -964,7 +970,8 @@ async fn chat_collectors_return_structured_message_and_visible_text() {
|
||||
)],
|
||||
finished_requests: Some(BTreeSet::from(["chat-collect".to_string()])),
|
||||
..Default::default()
|
||||
},
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
.await;
|
||||
})
|
||||
@@ -1018,7 +1025,7 @@ async fn chat_explicitly_disables_reasoning_parser() {
|
||||
let _ = recv_engine_message(dealer).await;
|
||||
send_outputs(
|
||||
push,
|
||||
EngineCoreOutputs {
|
||||
RequestBatchOutputs {
|
||||
outputs: vec![
|
||||
request_output(
|
||||
"chat-reasoning-disabled",
|
||||
@@ -1049,7 +1056,8 @@ async fn chat_explicitly_disables_reasoning_parser() {
|
||||
"chat-reasoning-disabled".to_string()
|
||||
])),
|
||||
..Default::default()
|
||||
},
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
.await;
|
||||
})
|
||||
@@ -1096,7 +1104,7 @@ async fn chat_stream_parses_tool_calls_automatically() {
|
||||
let _ = recv_engine_message(dealer).await;
|
||||
send_outputs(
|
||||
push,
|
||||
EngineCoreOutputs {
|
||||
RequestBatchOutputs {
|
||||
outputs: vec![
|
||||
request_output(
|
||||
"chat-tool",
|
||||
@@ -1121,7 +1129,8 @@ async fn chat_stream_parses_tool_calls_automatically() {
|
||||
],
|
||||
finished_requests: Some(BTreeSet::from(["chat-tool".to_string()])),
|
||||
..Default::default()
|
||||
},
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
.await;
|
||||
})
|
||||
@@ -1204,7 +1213,7 @@ async fn chat_collect_message_preserves_tool_call_arguments_in_final_only_mode()
|
||||
let _ = recv_engine_message(dealer).await;
|
||||
send_outputs(
|
||||
push,
|
||||
EngineCoreOutputs {
|
||||
RequestBatchOutputs {
|
||||
outputs: vec![
|
||||
request_output(
|
||||
"chat-final-only-tool",
|
||||
@@ -1231,7 +1240,8 @@ async fn chat_collect_message_preserves_tool_call_arguments_in_final_only_mode()
|
||||
"chat-final-only-tool".to_string()
|
||||
])),
|
||||
..Default::default()
|
||||
},
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
.await;
|
||||
})
|
||||
@@ -1282,7 +1292,7 @@ async fn chat_stream_and_collect_preserve_prompt_and_sample_logprobs() {
|
||||
let request: EngineCoreRequest = rmp_serde::from_slice(&add[1]).unwrap();
|
||||
send_outputs(
|
||||
push,
|
||||
EngineCoreOutputs {
|
||||
RequestBatchOutputs {
|
||||
outputs: vec![
|
||||
request_output_with_logprobs(
|
||||
&request.request_id,
|
||||
@@ -1303,7 +1313,8 @@ async fn chat_stream_and_collect_preserve_prompt_and_sample_logprobs() {
|
||||
],
|
||||
finished_requests: Some(BTreeSet::from([request.request_id])),
|
||||
..Default::default()
|
||||
},
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user