Skip to main content

constrain_tool_calls

Function constrain_tool_calls 

Source
pub fn constrain_tool_calls(body: &mut Value, ctx: &ModelContext) -> bool
Expand description

Originate a decode-time grammar for a demanded dialect tool call.

Engages only when all of the following hold, and is a no-op otherwise:

  • GGLIB_DISABLE_GRAMMAR is not set to a truthy value;
  • the model resolved from the catalog with a dialect spec whose codec list includes JSON (see module docs for why the native path is excluded — and a grammar can only originate JSON-shaped bodies);
  • the request carries a non-empty tools array whose function names are expressible in a GBNF literal;
  • the client sent none of grammar / json_schema / response_format — a client that constrains its own decode is always respected;
  • tool_choice demands a call: "required", or a named function.

Returns true when a grammar was installed.