-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add support for GPT-5 Free-Form Function Calling and Context Free Grammar constraints over tools #2572
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
Add support for GPT-5 Free-Form Function Calling and Context Free Grammar constraints over tools #2572
Changes from 64 commits
c713a6e
de3bc18
e7ca5ca
f0a5cbe
68fc7cf
5d9af16
3687580
61f7291
7a869b9
5e8cef9
79b519a
991c01d
c70bc1d
9586e6c
0b47135
92db07a
3e605e0
ab45262
3982f32
f3595b3
bef5dec
21e1a0b
cfcf7cf
1c5c500
e0017b4
307b011
131ab91
b386eb6
01988a5
88b8b28
3a46eea
c084523
ef1a696
e3f514d
0dbcdaf
4533df1
fc477bb
4cacf9b
185c929
a81e7b9
4a6e540
8714253
b55c9ab
b347edc
997d2ac
afdd6ef
175eee9
19eb167
6e259c8
742fb91
d69daad
d78a5c2
dc1c182
d1fb3a4
97b4d82
c54f26e
e206e3e
c483fda
5265211
f337820
2c7367f
f3a4afd
c4665a2
b86d2b1
d713c29
d0c346c
3037e6e
4e2264d
e28836b
b49cd81
a7112f4
7c96803
5d2b372
ec057c8
c949c83
36a0759
ae64d6b
f5ca42e
e7bea60
637774f
9cf0931
8c6c976
febe88d
3106219
673ef1e
2581873
9ec5b69
3927cf0
5990d8b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -143,6 +143,149 @@ As of 7:48 AM on Wednesday, April 2, 2025, in Tokyo, Japan, the weather is cloud | |
|
|
||
| You can learn more about the differences between the Responses API and Chat Completions API in the [OpenAI API docs](https://platform.openai.com/docs/guides/responses-vs-chat-completions). | ||
|
|
||
| ### Freeform Function Calling | ||
|
|
||
| GPT‑5 can now send raw text payloads - anything from Python scripts to SQL queries - to your custom tool without wrapping the data in JSON using freeform function calling. This differs from classic structured function calls, giving you greater flexibility when interacting with external runtimes such as: | ||
|
|
||
| * code execution with sandboxes (Python, C++, Java, …) | ||
| * SQL databases | ||
| * Shell environments | ||
| * Configuration generators | ||
|
|
||
| Note that freeform function calling does NOT support parallel tool calling. | ||
|
|
||
| You can enable freeform function calling for a tool using the `text_format` parameter when creating your tool. To use this the tool must take a single string argument (other than the runtime context) and the model must be one of the GPT-5 responses models. For example: | ||
|
|
||
| ```python | ||
| from pydantic_ai import Agent | ||
| from pydantic_ai.models.openai import OpenAIResponsesModel | ||
|
|
||
| model = OpenAIResponsesModel('gpt-5') # (1)! | ||
| agent = Agent(model) | ||
|
|
||
| @agent.tool_plain(text_format='text') # (2)! | ||
| def freeform_tool(sql: str): ... | ||
| ``` | ||
|
|
||
| 1. The GPT-5 family (`gpt-5`, `gpt-5-mini`, `gpt-5-nano`) all support freeform function calling. | ||
| 2. If the tool or model cannot be used with freeform function calling then it will be invoked in the normal way. | ||
|
|
||
| You can read more about this function calling style in the [openai documentation](https://cookbook.openai.com/examples/gpt-5/gpt-5_new_params_and_tools#2-freeform-function-calling). | ||
|
|
||
| #### Context Free Grammar | ||
|
|
||
| Invoking tools using freeform function calling can result in errors when the tool expectations are not met. For example, a tool that queries an SQL database can only accept valid SQL. The freeform function calling of GPT-5 supports generation of valid SQL for this situation by constraining the generated text using a context free grammar. | ||
|
||
|
|
||
| A context‑free grammar is a collection of production rules that define which strings belong to a language. Each rule rewrites a non‑terminal symbol into a sequence of terminals (literal tokens) and/or other non‑terminals, independent of surrounding context—hence context‑free. CFGs can capture the syntax of most programming languages and, in OpenAI custom tools, serve as contracts that force the model to emit only strings that the grammar accepts. | ||
|
|
||
| The grammar can be written as either a regular expression: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's have headings for Regular Expressions and LARK, so they're shown in the ToC on the right
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. addressed in b49cd81 |
||
|
|
||
|
|
||
| ```python | ||
| from pydantic_ai import Agent | ||
| from pydantic_ai.models.openai import OpenAIResponsesModel | ||
| from pydantic_ai.tools import FunctionTextFormat | ||
|
||
|
|
||
| model = OpenAIResponsesModel('gpt-5') # (1)! | ||
| agent = Agent(model) | ||
|
|
||
| timestamp_grammar_definition = r'^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]) (?:[01]\d|2[0-3]):[0-5]\d$' | ||
|
|
||
| @agent.tool_plain(text_format=FunctionTextFormat(syntax='regex', grammar=timestamp_grammar_definition)) # (2)! | ||
| def timestamp_accepting_tool(timestamp: str): ... | ||
| ``` | ||
|
|
||
| 1. The GPT-5 family (`gpt-5`, `gpt-5-mini`, `gpt-5-nano`) all support freeform function calling with context free grammar constraints. Unfortunately gpt-5-nano often struggles with these calls. | ||
| 2. If the tool or model cannot be used with freeform function calling then it will be invoked in the normal way, which may lead to invalid input. | ||
|
|
||
| Or as a [LARK](https://lark-parser.readthedocs.io/en/latest/how_to_use.html) grammar: | ||
|
|
||
| ```python | ||
| from pydantic_ai import Agent | ||
| from pydantic_ai.models.openai import OpenAIResponsesModel | ||
| from pydantic_ai.tools import FunctionTextFormat | ||
|
|
||
| model = OpenAIResponsesModel('gpt-5') # (1)! | ||
| agent = Agent(model) | ||
|
|
||
| timestamp_grammar_definition = r''' | ||
| start: timestamp | ||
|
|
||
| timestamp: YEAR "-" MONTH "-" DAY " " HOUR ":" MINUTE | ||
|
|
||
| %import common.DIGIT | ||
|
|
||
| YEAR: DIGIT DIGIT DIGIT DIGIT | ||
| MONTH: /(0[1-9]|1[0-2])/ | ||
| DAY: /(0[1-9]|[12]\d|3[01])/ | ||
| HOUR: /([01]\d|2[0-3])/ | ||
| MINUTE: /[0-5]\d/ | ||
| ''' | ||
|
|
||
| @agent.tool_plain(text_format=FunctionTextFormat(syntax='lark', grammar=timestamp_grammar_definition)) # (2)! | ||
| def i_like_iso_dates(date: str): ... | ||
| ``` | ||
|
|
||
| 1. The GPT-5 family (`gpt-5`, `gpt-5-mini`, `gpt-5-nano`) all support freeform function calling with context free grammar constraints. Unfortunately gpt-5-nano often struggles with these calls. | ||
| 2. If the tool or model cannot be used with freeform function calling then it will be invoked in the normal way, which may lead to invalid input. | ||
|
|
||
| There is a limit to the grammar complexity that GPT-5 supports, as such it is important to test your grammar. | ||
|
|
||
| Freeform function calling, with or without a context free grammar, can be used with the output tool for the agent: | ||
|
|
||
| ```python | ||
| from pydantic_ai import Agent | ||
| from pydantic_ai.models.openai import OpenAIResponsesModel | ||
| from pydantic_ai.output import ToolOutput | ||
| from pydantic_ai.tools import FunctionTextFormat | ||
|
|
||
| sql_grammar_definition = r''' | ||
| start: select_stmt | ||
| select_stmt: "SELECT" select_list "FROM" table ("WHERE" condition ("AND" condition)*)? | ||
| select_list: "*" | column ("," column)* | ||
| table: "users" | "orders" | ||
| column: "id" | "user_id" | "name" | "age" | ||
| condition: column ("=" | ">" | "<") (NUMBER | STRING) | ||
| %import common.NUMBER | ||
| %import common.ESCAPED_STRING -> STRING | ||
| %import common.WS | ||
| %ignore WS | ||
| ''' # (1)! | ||
| def database_query(sql: str) -> str: | ||
| return sql # (2)! | ||
|
|
||
| output_tool = ToolOutput(database_query, text_format=FunctionTextFormat(syntax='lark', grammar=sql_grammar_definition)) | ||
| model = OpenAIResponsesModel('gpt-5') | ||
| agent = Agent(model, output_type=output_tool) | ||
| ``` | ||
|
|
||
| 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. | ||
| 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. | ||
|
||
|
|
||
| ##### Best Practices | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to link to OpenAI's docs instead of having to keep this up to date |
||
|
|
||
| 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. | ||
|
|
||
| * Keep terminals bounded – use /[^.\n]{0,10}*\./ rather than /.*\./. Limit matches both by content (negated character class) and by length ({M,N} quantifier). | ||
|
||
| * Prefer explicit char‑classes over . wildcards. | ||
| * Thread whitespace explicitly, e.g. using SP = " ", instead of a global %ignore. | ||
| * Describe your tool: tell the model exactly what the CFG accepts and instruct it to reason heavily about compliance. | ||
|
|
||
| Troubleshooting | ||
|
|
||
| * API rejects the grammar because it is too complex ➜ Simplify rules and terminals, remove %ignore.*. | ||
| * Unexpected tokens ➜ Confirm terminals aren't overlapping; check greedy lexer. | ||
| * When the model drifts "out‑of‑distribution" (shows up as the model producing excessively long or repetitive outputs, it is syntactically valid but is semantically wrong): | ||
| - Tighten the grammar. | ||
| - Iterate on the prompt (add few-shot examples) and tool description (explain the grammar and instruct the model to reason to conform to it). | ||
| - Experiment with a higher reasoning effort (e.g, bump from medium to high). | ||
|
|
||
| Resources: | ||
|
|
||
| * [Lark Docs](https://lark-parser.readthedocs.io/en/stable/) | ||
| * [Lark IDE](https://www.lark-parser.org/ide/) | ||
| * [OpenAI Cookbook on CFG](https://cookbook.openai.com/examples/gpt-5/gpt-5_new_params_and_tools#3-contextfree-grammar-cfg) | ||
|
|
||
| ## OpenAI-compatible Models | ||
|
|
||
| Many providers and models are compatible with the OpenAI API, and can be used with `OpenAIChatModel` in Pydantic AI. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,7 @@ | |
| ToolOutput, | ||
| _OutputSpecItem, # type: ignore[reportPrivateUsage] | ||
| ) | ||
| from .tools import GenerateToolJsonSchema, ObjectJsonSchema, ToolDefinition | ||
| from .tools import FunctionTextFormat, GenerateToolJsonSchema, ObjectJsonSchema, ToolDefinition | ||
| from .toolsets.abstract import AbstractToolset, ToolsetTool | ||
|
|
||
| if TYPE_CHECKING: | ||
|
|
@@ -587,6 +587,7 @@ class OutputObjectDefinition: | |
| name: str | None = None | ||
| description: str | None = None | ||
| strict: bool | None = None | ||
| text_format: Literal['text'] | FunctionTextFormat | None = None | ||
|
||
|
|
||
|
|
||
| @dataclass(init=False) | ||
|
|
@@ -617,6 +618,7 @@ def __init__( | |
| name: str | None = None, | ||
| description: str | None = None, | ||
| strict: bool | None = None, | ||
| text_format: Literal['text'] | FunctionTextFormat | None = None, | ||
|
||
| ): | ||
| if inspect.isfunction(output) or inspect.ismethod(output): | ||
| self._function_schema = _function_schema.function_schema(output, GenerateToolJsonSchema) | ||
|
|
@@ -659,6 +661,7 @@ def __init__( | |
| description=description, | ||
| json_schema=json_schema, | ||
| strict=strict, | ||
| text_format=text_format, | ||
| ) | ||
|
|
||
| async def process( | ||
|
|
@@ -916,19 +919,23 @@ def build( | |
| name = None | ||
| description = None | ||
| strict = None | ||
| text_format = None | ||
| if isinstance(output, ToolOutput): | ||
| # do we need to error on conflicts here? (DavidM): If this is internal maybe doesn't matter, if public, use overloads | ||
| name = output.name | ||
| description = output.description | ||
| strict = output.strict | ||
| text_format = output.text_format | ||
|
|
||
| output = output.output | ||
|
|
||
| description = description or default_description | ||
| if strict is None: | ||
| strict = default_strict | ||
|
|
||
| processor = ObjectOutputProcessor(output=output, description=description, strict=strict) | ||
| processor = ObjectOutputProcessor( | ||
| output=output, description=description, strict=strict, text_format=text_format | ||
| ) | ||
| object_def = processor.object_def | ||
|
|
||
| if name is None: | ||
|
|
@@ -953,6 +960,7 @@ def build( | |
| description=description, | ||
| parameters_json_schema=object_def.json_schema, | ||
| strict=object_def.strict, | ||
| text_format=object_def.text_format, | ||
| outer_typed_dict_key=processor.outer_typed_dict_key, | ||
| kind='output', | ||
| ) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.