Tool Choice

Tool Choice Control.

1"""
2Tool Choice
3=============================
4
5Tool Choice Control.
6"""
7
8from kern.agent import Agent
9from kern.models.openai import OpenAIResponses
10
11
12def get_weather(city: str) -> str:
13 return f"Weather data placeholder for {city}: 72F and clear."
14
15
16# ---------------------------------------------------------------------------
17# Create Agents
18# ---------------------------------------------------------------------------
19no_tools_agent = Agent(
20 name="No-Tools Agent",
21 model=OpenAIResponses(id="gpt-5.2"),
22 tools=[get_weather],
23 tool_choice="none",
24)
25
26auto_tools_agent = Agent(
27 name="Auto-Tools Agent",
28 model=OpenAIResponses(id="gpt-5.2"),
29 tools=[get_weather],
30 tool_choice="auto",
31)
32
33forced_tool_agent = Agent(
34 name="Forced-Tool Agent",
35 model=OpenAIResponses(id="gpt-5.2"),
36 tools=[get_weather],
37 tool_choice={"type": "function", "name": "get_weather"},
38)
39
40# ---------------------------------------------------------------------------
41# Run Agents
42# ---------------------------------------------------------------------------
43if __name__ == "__main__":
44 prompt = "What is the weather in San Francisco today?"
45 no_tools_agent.print_response(prompt, stream=True)
46 auto_tools_agent.print_response(prompt, stream=True)
47 forced_tool_agent.print_response(prompt, stream=True)

Run the Example

1# Clone and setup repo
2git clone https://github.com/kern-ai/kern.git
3cd kern/cookbook/02_agents/04_tools
4
5# Create and activate virtual environment
6./scripts/demo_setup.sh
7source .venvs/demo/bin/activate
8
9python tool_choice.py