Streaming: Reject Member Tool Call.

Handle rejection of a tool call by member agent in streaming mode.

This example demonstrates how the team handles rejection of a tool call in streaming mode. After rejection, the team continues and the model responds acknowledging the rejection.

1"""Team HITL Streaming: Rejecting a member agent tool call.
2
3This example demonstrates how the team handles rejection of a tool
4call in streaming mode. After rejection, the team continues and the
5model responds acknowledging the rejection.
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_confirmation=True)
29def delete_user_account(username: str) -> str:
30 """Permanently delete a user account and all associated data.
31
32 Args:
33 username (str): Username of the account to delete
34 """
35 return f"Account {username} has been permanently deleted"
36
37
38# ---------------------------------------------------------------------------
39# Create Members
40# ---------------------------------------------------------------------------
41admin_agent = Agent(
42 name="Admin Agent",
43 role="Handles account administration tasks",
44 model=OpenAIResponses(id="gpt-5.2-mini"),
45 tools=[delete_user_account],
46 db=db,
47)
48
49
50# ---------------------------------------------------------------------------
51# Create Team
52# ---------------------------------------------------------------------------
53team = Team(
54 name="Admin Team",
55 members=[admin_agent],
56 model=OpenAIResponses(id="gpt-5.2-mini"),
57 db=db,
58)
59
60
61# ---------------------------------------------------------------------------
62# Run Team
63# ---------------------------------------------------------------------------
64if __name__ == "__main__":
65 for run_event in team.run("Delete the account for user 'jsmith'", stream=True):
66 # Use isinstance to check for team's pause event (not the member agent's)
67 if isinstance(run_event, TeamRunPausedEvent):
68 print("Team paused - requires confirmation")
69 for req in run_event.active_requirements:
70 if req.needs_confirmation:
71 print(f" Tool: {req.tool_execution.tool_name}")
72 print(f" Args: {req.tool_execution.tool_args}")
73
74 # Reject the dangerous operation
75 req.reject(note="Account deletion requires manager approval first")
76
77 response = team.continue_run(
78 run_id=run_event.run_id,
79 session_id=run_event.session_id,
80 requirements=run_event.requirements,
81 stream=True,
82 )
83 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 confirmation_rejected_stream.py