From ebf53ba373bf296d6c0fb8e88c02011397636ef0 Mon Sep 17 00:00:00 2001 From: Ting SUN Date: Tue, 9 Jun 2026 13:30:54 +0800 Subject: [PATCH] [Bugfix][Rust Frontend] Set a structured-output backend so requests do not 500 (#44729) Signed-off-by: Ting Sun Signed-off-by: Bugen Zhao Co-authored-by: Bugen Zhao --- .../engine-core-client/src/protocol/mod.rs | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/rust/src/engine-core-client/src/protocol/mod.rs b/rust/src/engine-core-client/src/protocol/mod.rs index e87bc334fd..6fae982d2e 100644 --- a/rust/src/engine-core-client/src/protocol/mod.rs +++ b/rust/src/engine-core-client/src/protocol/mod.rs @@ -162,6 +162,21 @@ pub enum RequestOutputKind { FinalOnly = 2, } +/// Structured-output backend selected for EngineCore grammar compilation. +/// +/// Python vLLM stores this in `StructuredOutputsParams._backend` after request +/// validation. The Rust frontend currently always lowers structured-output +/// requests to guidance, while ignoring any user-supplied `_backend` value. +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum StructuredOutputBackend { + Xgrammar, + #[default] + Guidance, + Outlines, + LmFormatEnforcer, +} + /// The stop reason associated with a finished output. /// /// Python models this as the union-typed `stop_reason: int | str | None` @@ -209,6 +224,17 @@ pub struct StructuredOutputsParams { pub whitespace_pattern: Option, /// Structural tag configuration (JSON-encoded string). pub structural_tag: Option, + /// Structured-output backend, mirroring Python's internal `_backend`. + /// + /// User-supplied values are ignored during deserialization. This matches + /// Python's request boundary, where `_backend` is set by validation rather + /// than accepted as a request-level backend selector. + #[serde( + default, + rename = "_backend", + deserialize_with = "serde_with::rust::deserialize_ignore_any" + )] + pub backend: StructuredOutputBackend, } /// Engine-core-facing sampling parameters for text generation. @@ -600,4 +626,18 @@ mod tests { expect_test::expect![[r#"messagepack decode failed for u64: wrong msgpack marker FixMap(1); value fallback: {"status": "READY"}"#]].assert_eq(&error.to_report_string()); } + + #[test] + fn structured_outputs_backend_ignores_deserialized_value() { + let params: StructuredOutputsParams = serde_json::from_value(serde_json::json!({ + "json_object": true, + "_backend": "xgrammar", + })) + .unwrap(); + + assert_eq!(params.backend, StructuredOutputBackend::Guidance); + + let value = serde_json::to_value(params).unwrap(); + assert_eq!(value["_backend"], "guidance"); + } }