Tool Choice
Tool Choice Control.
1"""2Tool Choice3=============================45Tool Choice Control.6"""78from kern.agent import Agent9from kern.models.openai import OpenAIResponses101112def get_weather(city: str) -> str:13 return f"Weather data placeholder for {city}: 72F and clear."141516# ---------------------------------------------------------------------------17# Create Agents18# ---------------------------------------------------------------------------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)2526auto_tools_agent = Agent(27 name="Auto-Tools Agent",28 model=OpenAIResponses(id="gpt-5.2"),29 tools=[get_weather],30 tool_choice="auto",31)3233forced_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)3940# ---------------------------------------------------------------------------41# Run Agents42# ---------------------------------------------------------------------------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 repo2git clone https://github.com/kern-ai/kern.git3cd kern/cookbook/02_agents/04_tools45# Create and activate virtual environment6./scripts/demo_setup.sh7source .venvs/demo/bin/activate89python tool_choice.py