Streaming: Member Tool with User Input

This example demonstrates how a team pauses when a member agent's tool.

This example demonstrates how a team pauses when a member agent's tool needs additional information from the user before it can be executed in streaming mode.

1"""Team HITL Streaming: Member agent tool requiring user input.
2
3This example demonstrates how a team pauses when a member agent's tool
4needs additional information from the user before it can be executed
5in streaming mode.
6
7Note: When streaming with member agents, use isinstance() with TeamRunPausedEvent
8to distinguish the team's pause from member agent pauses.
9"""
10
11from kern.agent import Agent
12from kern.db.sqlite import SqliteDb
13from kern.models.openai import OpenAIResponses
14from kern.run.team import RunPausedEvent as TeamRunPausedEvent
15from kern.team.team import Team
16from kern.tools import tool
17from kern.utils import pprint
18
19# ---------------------------------------------------------------------------
20# Setup
21# ---------------------------------------------------------------------------
22db = SqliteDb(db_file="tmp/team_hitl_stream.db")
23
24
25# ---------------------------------------------------------------------------
26# Tools
27# ---------------------------------------------------------------------------
28@tool(requires_user_input=True, user_input_fields=["passenger_name"])
29def book_flight(destination: str, date: str, passenger_name: str) -> str:
30 """Book a flight to a destination.
31
32 Args:
33 destination (str): The destination city
34 date (str): Travel date
35 passenger_name (str): Full name of the passenger
36 """
37 return f"Booked flight to {destination} on {date} for {passenger_name}"
38
39
40# ---------------------------------------------------------------------------
41# Create Members
42# ---------------------------------------------------------------------------
43booking_agent = Agent(
44 name="Booking Agent",
45 role="Books travel arrangements",
46 model=OpenAIResponses(id="gpt-5.2-mini"),
47 tools=[book_flight],
48 db=db,
49)
50
51
52# ---------------------------------------------------------------------------
53# Create Team
54# ---------------------------------------------------------------------------
55team = Team(
56 name="Travel Team",
57 members=[booking_agent],
58 model=OpenAIResponses(id="gpt-5.2-mini"),
59 db=db,
60)
61
62
63# ---------------------------------------------------------------------------
64# Run Team
65# ---------------------------------------------------------------------------
66if __name__ == "__main__":
67 for run_event in team.run("Book a flight to Tokyo for next Friday", stream=True):
68 # Use isinstance to check for team's pause event (not the member agent's)
69 if isinstance(run_event, TeamRunPausedEvent):
70 print("Team paused - requires user input")
71 for req in run_event.active_requirements:
72 if req.needs_user_input:
73 print(f" Tool: {req.tool_execution.tool_name}")
74 for field in req.user_input_schema or []:
75 print(f" Field needed: {field.name} - {field.description}")
76
77 req.provide_user_input({"passenger_name": "John Smith"})
78
79 response = team.continue_run(
80 run_id=run_event.run_id,
81 session_id=run_event.session_id,
82 requirements=run_event.requirements,
83 stream=True,
84 )
85 pprint.pprint_run_response(response)

Run the Example

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