User Input Required for Tool Execution
This example demonstrates how to create tools that require user input before execution, allowing for dynamic data collection during agent runs.
Create a Python file
1from typing import List23from kern.agent import Agent4from kern.db.sqlite import SqliteDb5from kern.models.openai import OpenAIResponses6from kern.tools import tool7from kern.tools.function import UserInputField8from kern.utils import pprint91011# You can either specify the user_input_fields leave empty for all fields to be provided by the user12@tool(requires_user_input=True, user_input_fields=["to_address"])13def send_email(subject: str, body: str, to_address: str) -> str:14 """15 Send an email.1617 Args:18 subject (str): The subject of the email.19 body (str): The body of the email.20 to_address (str): The address to send the email to.21 """22 return f"Sent email to {to_address} with subject {subject} and body {body}"232425agent = Agent(26 model=OpenAIResponses(id="gpt-5.2"),27 tools=[send_email],28 markdown=True,29 db=SqliteDb(db_file="tmp/example.db"),30)3132run_response = agent.run(33 "Send an email with the subject 'Hello' and the body 'Hello, world!'"34)3536for requirement in run_response.active_requirements:37 if requirement.needs_user_input:38 input_schema: List[UserInputField] = requirement.user_input_schema # type: ignore3940 for field in input_schema:41 # Get user input for each field in the schema42 field_type = field.field_type43 field_description = field.description4445 # Display field information to the user46 print(f"\nField: {field.name}")47 print(f"Description: {field_description}")48 print(f"Type: {field_type}")4950 # Get user input51 if field.value is None:52 user_value = input(f"Please enter a value for {field.name}: ")53 else:54 print(f"Value: {field.value}")55 user_value = field.value5657 # Update the field value58 field.value = user_value5960run_response = agent.continue_run(61 run_id=run_response.run_id,62 requirements=run_response.requirements,63) # or agent.continue_run(run_response=run_response)64pprint.pprint_run_response(run_response)6566# Or for simple debug flow67# agent.print_response("Send an email with the subject 'Hello' and the body 'Hello, world!'")Set up your virtual environment
1uv venv --python 3.122source .venv/bin/activate1uv venv --python 3.122.venv\Scripts\activateInstall dependencies
1uv pip install -U kern-ai openaiExport your OpenAI API key
1export OPENAI_API_KEY="your_openai_api_key_here"1$Env:OPENAI_API_KEY="your_openai_api_key_here"Run Agent
1python user_input_required.py