Feat: Implement JSON Mode (response_format.type="json_object") (#4733)

Co-authored-by: Kyle Pena <kylepena@kyles-macbook-pro.turkey-marlin.ts.net>
This commit is contained in:
kyle-pena-kuzco
2025-04-20 20:41:22 -04:00
committed by GitHub
parent 8de53da989
commit 9f3bd2ad39
3 changed files with 140 additions and 0 deletions

View File

@@ -158,6 +158,7 @@ class XGrammarGrammarBackend(BaseGrammarBackend):
def dispatch_json(self, key_string: str) -> Optional[XGrammarGrammar]:
try:
if key_string == "$$ANY$$":
# Note: This builtin JSON grammar includes *all* valid JSON (including, for example, arrays at the root)
ctx = self.grammar_compiler.compile_builtin_json_grammar()
else:
ctx = self.grammar_compiler.compile_json_schema(schema=key_string)

View File

@@ -1105,6 +1105,8 @@ def v1_chat_generate_request(
sampling_params["json_schema"] = convert_json_schema_to_str(
request.response_format.json_schema.schema_
)
elif request.response_format and request.response_format.type == "json_object":
sampling_params["json_schema"] = '{"type": "object"}'
elif (
request.response_format and request.response_format.type == "structural_tag"
):