Overview
TheGroupChat class enables collaborative conversations among multiple agents (or callables) with flexible speaker selection strategies, conversation history management, and interactive terminal sessions. Agents can @mention each other to request input or delegate tasks.
Class Definition
Parameters
str
default:"generate_api_key(prefix='swarms-')"
Unique identifier for the group chat
str
default:"GroupChat"
Name of the group chat
str
default:"A group chat for multiple agents"
Description of the group chat’s purpose
List[Union[Agent, Callable]]
default:"[]"
List of agent objects or callables that will participate in the conversation
int
default:"1"
Maximum number of conversation loops per run
str
default:"dict"
Output format for conversation history. Options: “dict”, “str”, “list”, “json”
bool
default:"False"
If True, enables interactive terminal REPL session for human-in-the-loop conversations
Optional[Union[str, Callable]]
Speaker selection strategy. Options: “round-robin-speaker”, “random-speaker”, “priority-speaker”, “random-dynamic-speaker”, or custom callable
Optional[dict]
State/configuration for the speaker function (e.g., priorities for priority-speaker)
str
default:""
Rules for the conversation that will be added to conversation history
bool
default:"True"
Whether to enable timestamps in conversation messages
Speaker Functions
Built-in Speaker Functions
round-robin-speaker
Cycles through agents in order, ensuring each agent gets a turn sequentially.random-speaker
Randomly selects exactly one agent to respond to each user query.priority-speaker
Selects agents based on priority weights. Requires priority configuration in speaker_state.random-dynamic-speaker
Randomly selects the first agent, then follows @mentions in responses for subsequent speakers. Supports both sequential and parallel execution strategies.Custom Speaker Functions
You can provide custom speaker selection logic:Direct Function Imports
The string identifiers above ("round-robin-speaker", etc.) dispatch to plain functions in swarms.structs.groupchat. You can import them directly when you want to compose them or pass them as the speaker_function= argument without the string indirection.
expertise_based is shaped differently from the others — it’s a per-agent filter rather than a selector. Use it when you want every agent to self-decide whether to participate based on the topic of the last message.
Methods
run()
str
required
The user input or task to process. Can include @mentions to target specific agents
str
Optional image input for the agents
List[str]
Optional list of images for the agents
str
The formatted conversation history (format depends on output_type)
GroupChatError: If an unexpected error occursAgentNotFoundError: If a mentioned agent is not found
start_interactive_session()
- Chat with agents using @mentions (optional)
- See available agents and descriptions
- Change speaker function mid-conversation
- Exit using ‘exit’ or ‘quit’
- Get help using ‘help’ or ’?’
GroupChatError: If interactive mode is not enabled
set_speaker_function()
Union[str, Callable]
required
Either a string name of predefined function or custom callable
dict
Optional state for the speaker function
InvalidSpeakerFunctionError: If the speaker function is invalid
get_available_speaker_functions()
List[str]
List of available speaker function names
get_current_speaker_function()
str
Name of current speaker function, or “custom” if it’s a custom function
Collaboration Features
@Mentions
Agents can mention other agents using @agent_name syntax:Automatic Prompt Augmentation
GroupChat automatically adds collaboration instructions to each agent’s system prompt:- How to use @mentions
- Collaboration guidelines
- Task completion protocols
- Response structure recommendations
Conversation Context
Each agent receives the complete conversation history, enabling:- Context-aware responses
- Building upon others’ contributions
- Acknowledging previous agents’ work
Usage Examples
Basic GroupChat
Interactive Session
Priority-Based Selection
Exception Classes
GroupChatError: Base exception for GroupChat errorsAgentNotFoundError: Raised when a mentioned agent is not foundInvalidTaskFormatError: Raised when task format is invalidInvalidSpeakerFunctionError: Raised when invalid speaker function is provided