Skip to content

Instantly share code, notes, and snippets.

@dstoc
Last active September 6, 2025 06:03
Show Gist options
  • Select an option

  • Save dstoc/ab58a1829b3f504c64f08bee5e8c6ea6 to your computer and use it in GitHub Desktop.

Select an option

Save dstoc/ab58a1829b3f504c64f08bee5e8c6ea6 to your computer and use it in GitHub Desktop.
guided generation for openai/harmony - improve precision of tool call generation
root ::= harmony-default # or prefill- as needed
harmony-default ::= thinking (content | preamble? tool-call)
harmony-prefill-thinking ::= not-end* "<|end|><|start|>assistant" (content | preamble? tool-call)
harmony-prefill-content ::= not-return*
thinking ::= "<|channel|>analysis<|message|>" not-end* "<|end|><|start|>assistant"
preamble ::= "<|channel|>commentary<|message|>" not-end* "<|end|><|start|>assistant"
content ::= "<|channel|>final<|message|>" not-return*
tool-call ::= preamble? "<|channel|>commentary to=functions." tool-name ws "<|constrain|>json<|message|>" ws json ws
tool-name ::= [A-Za-z0-9_.]+
not-end ::=
[^<] |
"<" [^|] |
"<|" [^e] |
"<|e" [^n] |
"<|en" [^d] |
"<|end" [^|] |
"<|end|" [^>]
not-return ::=
[^<] |
"<" [^|] |
"<|" [^r] |
"<|r" [^e] |
"<|re" [^t] |
"<|ret" [^u] |
"<|retu" [^r] |
"<|retur" [^n] |
"<|return" [^|] |
"<|return|" [^>]
# From llama.cpp:grammars/json.gbnf
json ::= object
value ::= object | array | string | number | ("true" | "false" | "null") ws
object ::=
"{" ws (
string ":" ws value
("," ws string ":" ws value)*
)? "}" ws
array ::=
"[" ws (
value
("," ws value)*
)? "]" ws
string ::=
"\"" (
[^"\\\x7F\x00-\x1F] |
"\\" (["\\bfnrt] | "u" [0-9a-fA-F]{4}) # escapes
)* "\"" ws
number ::= ("-"? ([0-9] | [1-9] [0-9]{0,15})) ("." [0-9]+)? ([eE] [-+]? [0-9] [1-9]{0,15})? ws
# Optional space: by convention, applied in this grammar after literal chars when allowed
ws ::= | " " | "\n" [ \t]{0,20}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment