Skip to content

Commit fdb0787

Browse files
authored
[None][chore] Support json_schema in response_format (NVIDIA#8934)
Signed-off-by: Junyi Xu <[email protected]>
1 parent 44d1c75 commit fdb0787

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

tensorrt_llm/serve/openai_protocol.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ class ModelList(OpenAIBaseModel):
9292

9393

9494
class ResponseFormat(OpenAIBaseModel):
95-
# type must be one of "text", "json", "json_object", or "structural_tag"
96-
type: Literal["text", "json", "json_object", "regex", "ebnf",
95+
type: Literal["text", "json", "json_schema", "json_object", "regex", "ebnf",
9796
"structural_tag"]
9897
schema: Optional[dict] = None
98+
json_schema: Optional[dict] = None
9999
regex: Optional[str] = None
100100
ebnf: Optional[str] = None
101101
format: Optional[xgrammar.structural_tag.Format] = None
@@ -193,6 +193,12 @@ def _response_format_to_guided_decoding_params(
193193
"The 'schema' field is required when response_format.type is 'json'."
194194
)
195195
return GuidedDecodingParams(json=response_format.schema)
196+
elif response_format.type == "json_schema":
197+
if response_format.json_schema is None:
198+
raise ValueError(
199+
"The 'json_schema' field is required when response_format.type is 'json_schema'."
200+
)
201+
return GuidedDecodingParams(json=response_format.json_schema)
196202
elif response_format.type == "json_object":
197203
return GuidedDecodingParams(json_object=True)
198204
elif response_format.type == "regex":

tests/unittest/llmapi/apps/_test_openai_chat_guided_decoding.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,48 @@ def test_json_schema(client: openai.OpenAI, model_name: str):
102102
jsonschema.validate(json.loads(message.content), json_schema)
103103

104104

105+
def test_openai_compatible_json_schema(client: openai.OpenAI, model_name: str):
106+
json_schema = {
107+
"type": "object",
108+
"properties": {
109+
"name": {
110+
"type": "string",
111+
"pattern": "^[\\w]+$"
112+
},
113+
"population": {
114+
"type": "integer"
115+
},
116+
},
117+
"required": ["name", "population"],
118+
}
119+
messages = [
120+
{
121+
"role": "system",
122+
"content": "You are a helpful assistant.",
123+
},
124+
{
125+
"role":
126+
"user",
127+
"content":
128+
"Give me the information of the capital of France in the JSON format.",
129+
},
130+
]
131+
chat_completion = client.chat.completions.create(
132+
model=model_name,
133+
messages=messages,
134+
max_completion_tokens=256,
135+
response_format={
136+
"type": "json_schema",
137+
"json_schema": json_schema
138+
},
139+
)
140+
141+
message = chat_completion.choices[0].message
142+
assert message.content is not None
143+
assert message.role == "assistant"
144+
jsonschema.validate(json.loads(message.content), json_schema)
145+
146+
105147
def test_json_schema_user_profile(client: openai.OpenAI, model_name: str):
106148
json_schema = {
107149
"type": "object",

0 commit comments

Comments
 (0)