OpenAI Tool Call Schema¶
This page contains the API reference for tool and tool choice schema models used by builtin structural tag APIs.
- class xgrammar.openai_tool_call_schema.FunctionDefinition(*, name: str, description: Optional[str] = None, parameters: Optional[Dict[str, Any]] = None, strict: Optional[bool] = None)[source]¶
Bases:
BaseModelA JSON-Schema-based function definition.
Corresponds to
openai.types.shared_params.FunctionDefinition.In Chat Completions this object is nested under
tools[].function. In the Responses API, the function tool fields are flat on the tool object itself and correspond toopenai.types.responses.FunctionToolParam.- field name: str [Required]¶
The name of the function to be called.
Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.
- field description: Optional[str] = None¶
A description of what the function does, used by the model to choose when and how to call the function.
- field parameters: Optional[Dict[str, Any]] = None¶
The parameters the functions accepts, described as a JSON Schema object.
If omitted or set to
None, the generated function arguments will be unconstrained.
- field strict: Optional[bool] = None¶
Whether to enable strict schema adherence when generating the function call.
If set to true, the model will follow the exact schema defined in the
parametersfield.
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class xgrammar.openai_tool_call_schema.FunctionToolParam(*, type: Literal['function'] = 'function', function: FunctionDefinition)[source]¶
Bases:
BaseModelA function tool that can be used to generate a response.
Corresponds to
openai.types.chat.ChatCompletionFunctionToolParam.Chat Completions shape:
{"type": "function", "function": {"name": "...", "parameters": {...}}}
Responses API shape is flat and corresponds to
openai.types.responses.FunctionToolParam:{"type": "function", "name": "...", "parameters": {...}}
- field type: Literal['function'] = 'function'¶
The type of the tool. Currently, only
functionis supported.
- field function: FunctionDefinition [Required]¶
The function definition.
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class xgrammar.openai_tool_call_schema.BuiltinToolParam(*, type: str, name: Optional[str] = None, parameters: Optional[Dict[str, Any]] = None)[source]¶
Bases:
BaseModelA builtin tool whose output should be constrained.
This mirrors hosted/server tool declarations used by APIs such as OpenAI Responses or Anthropic Messages.
typeis the provider-facing builtin tool type.nameis the tool name that appears in the model output; when omitted, callers should usetypeas the output name.parametersis the JSON schema required by XGrammar and serving engines for constrained decoding.- field name: Optional[str] = None¶
The output tool name for model. XGrammar-specific field.
Used to constrain the tool name. Use this when the model emits a tool name that differs from the provider
type. For example, an OpenAI-styleweb_search_previewbuiltin may be emitted asbrowser.searchby a Harmony-style model. Defaults totypewhen omitted.
- field parameters: Optional[Dict[str, Any]] = None¶
Argument schema for the builtin tool. XGrammar-specific field.
Hosted/server tool APIs often do not require users to provide this schema, but XGrammar and serving engines need it to constrain the arguments emitted by the model.
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- xgrammar.openai_tool_call_schema.ToolParam(*args, **kwargs)¶
A function or builtin tool accepted by builtin structural tag APIs.
alias of
Union[FunctionToolParam,BuiltinToolParam]
- class xgrammar.openai_tool_call_schema.NamedToolChoiceParam(*, type: Literal['function'] = 'function', function: NamedToolChoiceFunction)[source]¶
Bases:
BaseModelSpecifies a tool the model should use.
Use to force the model to call a specific function.
Corresponds to
openai.types.chat.ChatCompletionNamedToolChoiceParam.Chat Completions shape:
{"type": "function", "function": {"name": "..."}}
Responses API shape is flat and corresponds to
openai.types.responses.ToolChoiceFunctionParam:{"type": "function", "name": "..."}
- field function: NamedToolChoiceFunction [Required]¶
The function to call.
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class xgrammar.openai_tool_call_schema.BuiltinToolChoiceParam(*, type: str, name: Optional[str] = None)[source]¶
Bases:
BaseModelSpecifies a builtin tool the model should use.
typematches the builtin tool type intools. Matching is based ontype;nameis accepted for API compatibility but is not used for matching.- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class xgrammar.openai_tool_call_schema.AllowedToolChoiceParam(*, type: Literal['allowed_tools'] = 'allowed_tools', allowed_tools: AllowedToolsParam)[source]¶
Bases:
BaseModelConstrains the tools available to the model to a pre-defined set.
Corresponds to
openai.types.chat.ChatCompletionAllowedToolChoiceParam.Chat Completions shape:
{"type": "allowed_tools", "allowed_tools": {"mode": "...", "tools": [...]}}
Responses API shape is flat and corresponds to
openai.types.responses.ToolChoiceAllowedParam:{"type": "allowed_tools", "mode": "...", "tools": [...]}
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- field type: Literal['allowed_tools'] = 'allowed_tools'¶
Allowed tool configuration type. Always
allowed_tools.
- field allowed_tools: AllowedToolsParam [Required]¶
Constrains the tools available to the model to a pre-defined set.
- xgrammar.openai_tool_call_schema.ToolChoiceOptionParam(*args, **kwargs)¶
Controls which (if any) tool is called by the model.
nonemeans the model will not call any tool and instead generates a message.automeans the model can pick between generating a message or calling one or more tools.requiredmeans the model must call one or more tools.Specifying a particular tool via
{"type": "function", "function": {"name": "my_function"}}forces the model to call that tool.Corresponds to openai.types.chat.ChatCompletionToolChoiceOptionParam.
alias of
Union[Literal[‘none’, ‘auto’, ‘required’],NamedToolChoiceParam,AllowedToolChoiceParam,BuiltinToolChoiceParam]