Implementation:Openai Openai python Tool Choice Allowed
| Knowledge Sources | |
|---|---|
| Domains | API_Types, Responses_API |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete type for representing a tool choice configuration that constrains the model to a pre-defined set of allowed tools, provided by the openai-python SDK.
Description
ToolChoiceAllowed is a Pydantic model that constrains which tools the model is allowed to use during response generation. It has a mode field set to either "auto" (model may choose from allowed tools or generate a message) or "required" (model must call one or more of the allowed tools). The tools field is a list of tool definition dictionaries specifying which tools are permitted, and the type field is always "allowed_tools".
Usage
Import this type when you need to inspect or type-check a tool choice configuration that restricts the model to specific tools, as returned in API responses.
Code Reference
Source Location
- Repository: openai-python
- File: src/openai/types/responses/tool_choice_allowed.py
Signature
class ToolChoiceAllowed(BaseModel):
"""Constrains the tools available to the model to a pre-defined set."""
mode: Literal["auto", "required"]
tools: List[Dict[str, object]]
type: Literal["allowed_tools"]
Import
from openai.types.responses import ToolChoiceAllowed
I/O Contract
Fields
| Name | Type | Required | Description |
|---|---|---|---|
| mode | Literal["auto", "required"] | Yes | auto allows the model to pick from allowed tools and generate a message. required requires the model to call one or more of the allowed tools.
|
| tools | List[Dict[str, object]] | Yes | A list of tool definitions that the model is allowed to call. Each dict specifies tool type and identifying info (e.g., {"type": "function", "name": "get_weather"}).
|
| type | Literal["allowed_tools"] | Yes | Allowed tool configuration type. Always allowed_tools.
|
Usage Examples
import openai
client = openai.OpenAI()
response = client.responses.create(
model="gpt-4o",
input="What is the weather in Paris?",
tools=[
{"type": "function", "name": "get_weather", "parameters": {
"type": "object",
"properties": {"location": {"type": "string"}},
"required": ["location"],
}},
{"type": "web_search_preview"},
],
tool_choice={
"type": "allowed_tools",
"mode": "auto",
"tools": [
{"type": "function", "name": "get_weather"},
],
},
)