Team Callable Members

Pass a function as `members` to a Team.

Pass a function as members to a Team. The team composition is decided at run time based on session_state.

1"""
2Team Callable Members
3=====================
4Pass a function as `members` to a Team. The team composition
5is decided at run time based on session_state.
6"""
7
8from kern.agent import Agent
9from kern.models.openai import OpenAIResponses
10from kern.team import Team
11
12# ---------------------------------------------------------------------------
13# Create the Team Members
14# ---------------------------------------------------------------------------
15
16writer = Agent(
17 name="Writer",
18 role="Content writer",
19 model=OpenAIResponses(id="gpt-5-mini"),
20 instructions=["Write clear, concise content."],
21)
22
23researcher = Agent(
24 name="Researcher",
25 role="Research analyst",
26 model=OpenAIResponses(id="gpt-5-mini"),
27 instructions=["Research topics and summarize findings."],
28)
29
30
31def pick_members(session_state: dict):
32 """Include the researcher only when needed."""
33 needs_research = session_state.get("needs_research", False)
34 print(f"--> needs_research={needs_research}")
35
36 if needs_research:
37 return [researcher, writer]
38 return [writer]
39
40
41# ---------------------------------------------------------------------------
42# Create the Team
43# ---------------------------------------------------------------------------
44
45team = Team(
46 name="Content Team",
47 model=OpenAIResponses(id="gpt-5-mini"),
48 members=pick_members,
49 cache_callables=False,
50 instructions=["Coordinate the team to complete the task."],
51)
52
53
54# ---------------------------------------------------------------------------
55# Run the Team
56# ---------------------------------------------------------------------------
57
58if __name__ == "__main__":
59 print("=== Writer only ===")
60 team.print_response(
61 "Write a haiku about Python",
62 session_state={"needs_research": False},
63 stream=True,
64 )
65
66 print("\n=== Researcher + Writer ===")
67 team.print_response(
68 "Research the history of Python and write a short summary",
69 session_state={"needs_research": True},
70 stream=True,
71 )

Run the Example

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