From e00703bb2a6346e3188fffef7bb77c60db9a0ddc Mon Sep 17 00:00:00 2001 From: Ting SUN Date: Tue, 23 Jun 2026 05:47:05 +0700 Subject: [PATCH] fix(frontend): return 400 for missing completions json_schema (#28090) Signed-off-by: Ting Sun --- .../srt/entrypoints/openai/serving_completions.py | 10 +++++++--- .../entrypoints/openai/test_serving_completions.py | 11 +++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/python/sglang/srt/entrypoints/openai/serving_completions.py b/python/sglang/srt/entrypoints/openai/serving_completions.py index eba04f463..0a15aaf4d 100644 --- a/python/sglang/srt/entrypoints/openai/serving_completions.py +++ b/python/sglang/srt/entrypoints/openai/serving_completions.py @@ -164,9 +164,13 @@ class OpenAIServingCompletion(OpenAIServingBase): # Handle response_format constraints if request.response_format and request.response_format.type == "json_schema": - sampling_params["json_schema"] = convert_json_schema_to_str( - request.response_format.json_schema.schema_ - ) + json_schema = request.response_format.json_schema + schema = getattr(json_schema, "schema_", None) + if schema is None: + raise ValueError( + "schema_ is required for json_schema response format request." + ) + sampling_params["json_schema"] = convert_json_schema_to_str(schema) elif request.response_format and request.response_format.type == "json_object": sampling_params["json_schema"] = '{"type": "object"}' elif ( diff --git a/test/registered/unit/entrypoints/openai/test_serving_completions.py b/test/registered/unit/entrypoints/openai/test_serving_completions.py index 4817f4719..e3ad6f9c8 100644 --- a/test/registered/unit/entrypoints/openai/test_serving_completions.py +++ b/test/registered/unit/entrypoints/openai/test_serving_completions.py @@ -140,6 +140,17 @@ class ServingCompletionTestCase(unittest.TestCase): self.assertIn("json_schema", sampling_params) self.assertIsInstance(sampling_params["json_schema"], str) + def test_response_format_json_schema_missing_schema(self): + """Test that json_schema response_format without a schema raises a ValueError.""" + req = CompletionRequest( + model="x", + prompt="Generate a JSON object:", + max_tokens=100, + response_format={"type": "json_schema"}, + ) + with self.assertRaises(ValueError): + self.sc._build_sampling_params(req) + def test_response_format_structural_tag(self): """Test that response_format structural_tag is correctly processed in sampling params.""" req = CompletionRequest(