Skip to content

Commit c483fda

Browse files
test the examples manually, fix errors
1 parent e206e3e commit c483fda

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

docs/models/openai.md

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ agent = Agent(model)
191191

192192
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$'
193193

194-
@agent.tool_plain(text_format=FunctionTextFormat(syntax='regex', grammar=timestamp_grammar_definition) # (2)!
195-
def timestamp_accepting_tool(timestamp: str) -> ...
194+
@agent.tool_plain(text_format=FunctionTextFormat(syntax='regex', grammar=timestamp_grammar_definition)) # (2)!
195+
def timestamp_accepting_tool(timestamp: str): ...
196196
```
197197

198198
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
208208
model = OpenAIResponsesModel('gpt-5') # (1)!
209209
agent = Agent(model)
210210

211-
timestamp_grammar_definition = '''
211+
timestamp_grammar_definition = r'''
212212
start: timestamp
213213
214214
timestamp: YEAR "-" MONTH "-" DAY " " HOUR ":" MINUTE
@@ -222,8 +222,8 @@ HOUR: /([01]\d|2[0-3])/
222222
MINUTE: /[0-5]\d/
223223
'''
224224

225-
@agent.tool_plain(text_format=FunctionTextFormat(syntax='lark', grammar=timestamp_grammar_definition) # (2)!
226-
def i_like_iso_dates(date: str) -> ...
225+
@agent.tool_plain(text_format=FunctionTextFormat(syntax='lark', grammar=timestamp_grammar_definition)) # (2)!
226+
def i_like_iso_dates(date: str): ...
227227
```
228228

229229
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
235235

236236
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.
237237

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.
242242

243243
Troubleshooting
244244

245-
API rejects the grammar because it is too complex ➜ Simplify rules and terminals, remove %ignore.*.
246-
Unexpected tokens ➜ Confirm terminals aren’t overlapping; check greedy lexer.
247-
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):
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.*.
246+
* Unexpected tokens ➜ Confirm terminals aren’t overlapping; check greedy lexer.
247+
* 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):
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).
251251

252252
Resources:
253253

254-
Lark Docs – https://lark-parser.readthedocs.io/en/stable/
255-
Lark IDE – https://www.lark-parser.org/ide/
256-
LLGuidance Syntax – https://github.com/guidance-ai/llguidance/blob/main/docs/syntax.md
257-
Regex (Rust crate) – https://docs.rs/regex/latest/regex/#syntax
258-
259-
260-
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).
254+
* [Lark Docs](https://lark-parser.readthedocs.io/en/stable/)
255+
* [Lark IDE](https://www.lark-parser.org/ide/)
256+
* [OpenAI Cookbook on CFG](https://cookbook.openai.com/examples/gpt-5/gpt-5_new_params_and_tools#3-contextfree-grammar-cfg)
261257

262258
## OpenAI-compatible Models
263259

0 commit comments

Comments
 (0)