Skip to content

Commit 2c7367f

Browse files
Add section on output tool use
1 parent f337820 commit 2c7367f

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

docs/models/openai.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,25 @@ def i_like_iso_dates(date: str): ...
231231

232232
There is a limit to the grammar complexity that GPT-5 supports, as such it is important to test your grammar.
233233

234+
Freeform function calling, with or without a context free grammar, can be used with the output tool for the agent:
235+
236+
```python
237+
from pydantic_ai import Agent, Tool
238+
from pydantic_ai.models.openai import OpenAIResponsesModel
239+
from pydantic_ai.tools import FunctionTextFormat
240+
241+
sql_grammar_definition = "..." # (1)!
242+
def database_query(sql: str) -> str:
243+
return sql # (2)!
244+
245+
output_tool = Tool(database_query, text_format=FunctionTextFormat(syntax='lark', grammar=sql_grammar_definition))
246+
model = OpenAIResponsesModel('gpt-5')
247+
agent = Agent(model, output_type=output_tool)
248+
```
249+
250+
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).
251+
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.
252+
234253
##### Best Practices
235254

236255
Lark grammars can be tricky to perfect. While simple grammars perform most reliably, complex grammars often require iteration on the grammar definition itself, the prompt, and the tool description to ensure that the model does not go out of distribution.

0 commit comments

Comments
 (0)