Skip to content

[V1] Update structured output #16812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions benchmarks/benchmark_serving_structured_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,17 @@ def get_schema(index: int):

elif args.dataset == "grammar":
schema = """
?start: select_statement
root ::= select_statement

?select_statement: "SELECT " column_list " FROM " table_name
select_statement ::= "SELECT " column " from " table " where " condition

?column_list: column_name ("," column_name)*
column ::= "col_1 " | "col_2 "

?table_name: identifier
table ::= "table_1 " | "table_2 "

?column_name: identifier
condition ::= column "= " number

?identifier: /[a-zA-Z_][a-zA-Z0-9_]*/
number ::= "1 " | "2 "
"""
prompt = "Generate an SQL query to show the 'username' \
and 'email' from the 'users' table."
Expand Down
14 changes: 7 additions & 7 deletions docs/source/features/structured_outputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ completion = client.chat.completions.create(
"content": "Generate an example email address for Alan Turing, who works in Enigma. End in .com and new line. Example result: alan.turing@enigma.com\n",
}
],
extra_body={"guided_regex": "\w+@\w+\.com\n", "stop": ["\n"]},
extra_body={"guided_regex": r"\w+@\w+\.com\n", "stop": ["\n"]},
)
print(completion.choices[0].message.content)
```
Expand Down Expand Up @@ -105,17 +105,17 @@ It works by using a context free EBNF grammar, which for example we can use to d

```python
simplified_sql_grammar = """
?start: select_statement
root ::= select_statement

?select_statement: "SELECT " column_list " FROM " table_name
select_statement ::= "SELECT " column " from " table " where " condition

?column_list: column_name ("," column_name)*
column ::= "col_1 " | "col_2 "

?table_name: identifier
table ::= "table_1 " | "table_2 "

?column_name: identifier
condition ::= column "= " number

?identifier: /[a-zA-Z_][a-zA-Z0-9_]*/
number ::= "1 " | "2 "
"""

completion = client.chat.completions.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"content": prompt,
}],
extra_body={
"guided_regex": "\w+@\w+\.com\n",
"guided_regex": r"\w+@\w+\.com\n",
"stop": ["\n"]
},
)
Expand Down Expand Up @@ -70,17 +70,17 @@ class CarDescription(BaseModel):

# Guided decoding by Grammar
simplified_sql_grammar = """
?start: select_statement
root ::= select_statement

?select_statement: "SELECT " column_list " FROM " table_name
select_statement ::= "SELECT " column " from " table " where " condition

?column_list: column_name ("," column_name)*
column ::= "col_1 " | "col_2 "

?table_name: identifier
table ::= "table_1 " | "table_2 "

?column_name: identifier
condition ::= column "= " number

?identifier: /[a-zA-Z_][a-zA-Z0-9_]*/
number ::= "1 " | "2 "
"""

prompt = ("Generate an SQL query to show the 'username' and 'email'"
Expand Down Expand Up @@ -110,7 +110,7 @@ class CarDescription(BaseModel):
"content": prompt,
}],
extra_body={
"guided_regex": "\w+@\w+\.com\n",
"guided_regex": r"\w+@\w+\.com\n",
"stop": ["\n"],
"guided_decoding_backend": "xgrammar:no-fallback"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,17 @@ class CarDescription(BaseModel):

# Guided decoding by Grammar
simplified_sql_grammar = """
?start: select_statement
root ::= select_statement

?select_statement: "SELECT " column_list " FROM " table_name
select_statement ::= "SELECT " column " from " table " where " condition

?column_list: column_name ("," column_name)*
column ::= "col_1 " | "col_2 "

?table_name: identifier
table ::= "table_1 " | "table_2 "

?column_name: identifier
condition ::= column "= " number

?identifier: /[a-zA-Z_][a-zA-Z0-9_]*/
number ::= "1 " | "2 "
"""

# This may be very slow https://github.com/vllm-project/vllm/issues/12122
Expand Down