User Input Required All Fields

This example demonstrates how to use the requires_user_input parameter to collect input for all fields in a tool. It shows how to handle user input schema and collect values for each required field.

Create a Python file

1from typing import List
2
3from kern.agent import Agent
4from kern.db.sqlite import SqliteDb
5from kern.models.openai import OpenAIResponses
6from kern.tools import tool
7from kern.tools.function import UserInputField
8from kern.utils import pprint
9
10
11@tool(requires_user_input=True)
12def send_email(subject: str, body: str, to_address: str) -> str:
13 """
14 Send an email.
15
16 Args:
17 subject (str): The subject of the email.
18 body (str): The body of the email.
19 to_address (str): The address to send the email to.
20 """
21 return f"Sent email to {to_address} with subject {subject} and body {body}"
22
23
24agent = Agent(
25 model=OpenAIResponses(id="gpt-5.2"),
26 tools=[send_email],
27 markdown=True,
28 db=SqliteDb(db_file="tmp/example.db"),
29)
30
31run_response = agent.run("Send an email please")
32if run_response.is_paused: # Or agent.run_response.is_paused
33 for requirement in run_response.active_requirements:
34 if requirement.needs_user_input:
35 input_schema: List[UserInputField] = requirement.user_input_schema # type: ignore
36
37 for field in input_schema:
38 # Get user input for each field in the schema
39 field_type = field.field_type
40 field_description = field.description
41
42 # Display field information to the user
43 print(f"\nField: {field.name}")
44 print(f"Description: {field_description}")
45 print(f"Type: {field_type}")
46
47 # Get user input
48 if field.value is None:
49 user_value = input(f"Please enter a value for {field.name}: ")
50
51 # Update the field value
52 field.value = user_value
53
54 run_response = agent.continue_run(
55 run_id=run_response.run_id,
56 requirements=run_response.requirements,
57 )
58 pprint.pprint_run_response(run_response)
59
60# Or for simple debug flow
61# agent.print_response("Send an email please")

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Install dependencies

1uv pip install -U kern-ai openai

Export 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_all_fields.py