You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
@@ -208,7 +208,7 @@ from pydantic_ai.tools import FunctionTextFormat
208
208
model= OpenAIResponsesModel('gpt-5') # (1)!
209
209
agent= Agent(model)
210
210
211
-
timestamp_grammar_definition='''
211
+
timestamp_grammar_definition=r'''
212
212
start: timestamp
213
213
214
214
timestamp: YEAR "-" MONTH "-" DAY " " HOUR ":" MINUTE
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.
@@ -235,29 +235,25 @@ There is a limit to the grammar complexity that GPT-5 supports, as such it is im
235
235
236
236
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.
237
237
238
-
Keep terminals bounded – use /[^.\n]{0,10}*\./ rather than /.*\./. Limit matches both by content (negated character class) and by length ({M,N} quantifier).
239
-
Prefer explicit char‑classes over . wildcards.
240
-
Thread whitespace explicitly, e.g. using SP = "", instead of a global%ignore.
241
-
Describe your tool: tell the model exactly what the CFG accepts and instruct it to reason heavily about compliance.
238
+
* Keep terminals bounded – use /[^.\n]{0,10}*\./ rather than /.*\./. Limit matches both by content (negated character class) and by length ({M,N} quantifier).
239
+
* Prefer explicit char‑classes over . wildcards.
240
+
* Thread whitespace explicitly, e.g. using SP = "", instead of a global%ignore.
241
+
* Describe your tool: tell the model exactly what the CFG accepts and instruct it to reason heavily about compliance.
242
242
243
243
Troubleshooting
244
244
245
-
API rejects the grammar because it is too complex ➜ Simplify rules and terminals, remove %ignore.*.
When the model drifts "out‑of‑distribution" (shows up as the model producing excessively longor repetitive outputs, it is syntactically valid but is semantically wrong):
248
-
Tighten the grammar.
249
-
Iterate on the prompt (add few-shot examples) and tool description (explain the grammar and instruct the model to reason to conform to it).
250
-
Experiment with a higher reasoning effort (e.g, bump from medium to high).
245
+
*API rejects the grammar because it is too complex ➜ Simplify rules and terminals, remove %ignore.*.
* When the model drifts "out‑of‑distribution" (shows up as the model producing excessively longor repetitive outputs, it is syntactically valid but is semantically wrong):
248
+
- Tighten the grammar.
249
+
- Iterate on the prompt (add few-shot examples) and tool description (explain the grammar and instruct the model to reason to conform to it).
250
+
- Experiment with a higher reasoning effort (e.g, bump from medium to high).
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#3-contextfree-grammar-cfg).
0 commit comments