Agentic User Input with Control Flow
This example demonstrates how to use UserControlFlowTools to allow agents to dynamically request user input when they need additional information to complete tasks.
Create a Python file
1from typing import Any, Dict, List23from kern.agent import Agent4from kern.db.sqlite import SqliteDb5from kern.models.openai import OpenAIResponses6from kern.tools import Toolkit7from kern.tools.function import UserInputField8from kern.tools.user_control_flow import UserControlFlowTools9from kern.utils import pprint101112class EmailTools(Toolkit):13 def __init__(self, *args, **kwargs):14 super().__init__(15 name="EmailTools", tools=[self.send_email, self.get_emails], *args, **kwargs16 )1718 def send_email(self, subject: str, body: str, to_address: str) -> str:19 """Send an email to the given address with the given subject and body.2021 Args:22 subject (str): The subject of the email.23 body (str): The body of the email.24 to_address (str): The address to send the email to.25 """26 return f"Sent email to {to_address} with subject {subject} and body {body}"2728 def get_emails(self, date_from: str, date_to: str) -> list[dict[str, str]]:29 """Get all emails between the given dates.3031 Args:32 date_from (str): The start date (in YYYY-MM-DD format).33 date_to (str): The end date (in YYYY-MM-DD format).34 """35 return [36 {37 "subject": "Hello",38 "body": "Hello, world!",39 "to_address": "test@test.com",40 "date": date_from,41 },42 {43 "subject": "Random other email",44 "body": "This is a random other email",45 "to_address": "john@doe.com",46 "date": date_to,47 },48 ]495051agent = Agent(52 model=OpenAIResponses(id="gpt-5.2"),53 tools=[EmailTools(), UserControlFlowTools()],54 markdown=True,55 db=SqliteDb(db_file="tmp/agentic_user_input.db"),56)5758run_response = agent.run("Send an email with the body 'What is the weather in Tokyo?'")5960# We use a while loop to continue the running until the agent is satisfied with the user input61while run_response.is_paused:62 for requirement in run_response.active_requirements:63 if requirement.needs_user_input:64 input_schema: List[UserInputField] = requirement.user_input_schema # type: ignore6566 for field in input_schema:67 # Get user input for each field in the schema68 field_type = field.field_type # type: ignore69 field_description = field.description # type: ignore7071 # Display field information to the user72 print(f"\nField: {field.name}") # type: ignore73 print(f"Description: {field_description}")74 print(f"Type: {field_type}")7576 # Get user input77 if field.value is None: # type: ignore78 user_value = input(f"Please enter a value for {field.name}: ") # type: ignore79 else:80 print(f"Value: {field.value}") # type: ignore81 user_value = field.value # type: ignore8283 # Update the field value84 field.value = user_value # type: ignore8586 run_response = agent.continue_run(87 run_id=run_response.run_id,88 requirements=run_response.requirements,89 )90 if not run_response.is_paused:91 pprint.pprint_run_response(run_response)92 break939495run_response = agent.run("Get me all my emails")9697while run_response.is_paused:98 for requirement in run_response.active_requirements:99 if requirement.needs_user_input:100 input_schema: Dict[str, Any] = requirement.user_input_schema # type: ignore101102 for field in input_schema:103 # Get user input for each field in the schema104 field_type = field.field_type # type: ignore105 field_description = field.description # type: ignore106107 # Display field information to the user108 print(f"\nField: {field.name}") # type: ignore109 print(f"Description: {field_description}")110 print(f"Type: {field_type}")111112 # Get user input113 if field.value is None: # type: ignore114 user_value = input(f"Please enter a value for {field.name}: ") # type: ignore115 else:116 print(f"Value: {field.value}") # type: ignore117 user_value = field.value # type: ignore118119 # Update the field value120 field.value = user_value # type: ignore121122 run_response = agent.continue_run(123 run_id=run_response.run_id,124 requirements=run_response.requirements,125 )126127 if not run_response.is_paused:128 pprint.pprint_run_response(run_response)129 breakSet 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 agentic_user_input.py