Skip to content

Commit b86d2b1

Browse files
actually use a lark grammar
the example tests do check it!
1 parent c4665a2 commit b86d2b1

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

docs/models/openai.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,18 @@ from pydantic_ai.models.openai import OpenAIResponsesModel
239239
from pydantic_ai.output import ToolOutput
240240
from pydantic_ai.tools import FunctionTextFormat
241241

242-
sql_grammar_definition = '...' # (1)!
242+
sql_grammar_definition = r'''
243+
start: select_stmt
244+
select_stmt: "SELECT" select_list "FROM" table ("WHERE" condition ("AND" condition)*)?
245+
select_list: "*" | column ("," column)*
246+
table: "users" | "orders"
247+
column: "id" | "user_id" | "name" | "age"
248+
condition: column ("=" | ">" | "<") (NUMBER | STRING)
249+
%import common.NUMBER
250+
%import common.ESCAPED_STRING -> STRING
251+
%import common.WS
252+
%ignore WS
253+
''' # (1)!
243254
def database_query(sql: str) -> str:
244255
return sql # (2)!
245256

@@ -248,7 +259,7 @@ model = OpenAIResponsesModel('gpt-5')
248259
agent = Agent(model, output_type=output_tool)
249260
```
250261

251-
1. An inline SQL grammar definition would be quite extensive and so it has been omitted, you can find an example SQL grammar [in the openai example](https://cookbook.openai.com/examples/gpt-5/gpt-5_new_params_and_tools#33-example---sql-dialect--ms-sql-vs-postgresql). There are also example grammars in the [lark repo](https://github.com/lark-parser/lark/blob/master/examples/composition/json.lark).
262+
1. An inline SQL grammar definition would be quite extensive and so this simplified version has been written, you can find an example SQL grammar [in the openai example](https://cookbook.openai.com/examples/gpt-5/gpt-5_new_params_and_tools#33-example---sql-dialect--ms-sql-vs-postgresql). There are also example grammars in the [lark repo](https://github.com/lark-parser/lark/blob/master/examples/composition/json.lark). Remember that a simpler grammar that matches your DDL will be easier for GPT-5 to work with and will result in fewer semantically invalid results.
252263
2. Returning the input directly might seem odd, remember that it has been constrained to the provided grammar. This can be useful if you want GPT-5 to generate content according to a grammar that you then use extensively through your program.
253264

254265
##### Best Practices

0 commit comments

Comments
 (0)