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 Members3=====================4Pass a function as `members` to a Team. The team composition5is decided at run time based on session_state.6"""78from kern.agent import Agent9from kern.models.openai import OpenAIResponses10from kern.team import Team1112# ---------------------------------------------------------------------------13# Create the Team Members14# ---------------------------------------------------------------------------1516writer = Agent(17 name="Writer",18 role="Content writer",19 model=OpenAIResponses(id="gpt-5-mini"),20 instructions=["Write clear, concise content."],21)2223researcher = Agent(24 name="Researcher",25 role="Research analyst",26 model=OpenAIResponses(id="gpt-5-mini"),27 instructions=["Research topics and summarize findings."],28)293031def 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}")3536 if needs_research:37 return [researcher, writer]38 return [writer]394041# ---------------------------------------------------------------------------42# Create the Team43# ---------------------------------------------------------------------------4445team = 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)525354# ---------------------------------------------------------------------------55# Run the Team56# ---------------------------------------------------------------------------5758if __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 )6566 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 repo2git clone https://github.com/kern-ai/kern.git3cd kern/cookbook/02_agents/04_tools45# Create and activate virtual environment6./scripts/demo_setup.sh7source .venvs/demo/bin/activate89python 03_team_callable_members.py