Observed since 20 Oct, 21:17 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
Persona-based agents for AG2 (AutoGen) - enabling distinct character embodiment
AG2 (AutoGen) agents currently mix role, purpose, and context into a single unstructured system_message, which is fantastic flexibility but does not propose a common pattern for agents to authentically embody distinct personas. PersonaAgent enables agents to adopt rich, well-defined characters through explicit role, goal, and backstory components.
The real power comes from the PersonaBuilder Markdown library pattern: Load expert personas from configuration files, enabling domain experts to define agent behavior while developers handle runtime integration. This separation provides:
Perfect for teams where domain experts define the "who" while developers handle the "how".
Current AG2 agent creation:
agent = ConversableAgent(
name="reviewer",
system_message="You are a senior software engineer with 10 years of experience. Your task is to review code for quality issues, security vulnerabilities, and suggest improvements. Focus on Python best practices and ensure the code follows PEP-8 standards. You should be constructive in your feedback."
)
Issues:
PersonaAgent enables authentic character embodiment (inspired by CrewAI's backstory/goal structure):
agent = PersonaAgent(
name="reviewer",
role="Senior Software Engineer",
goal="Review code for quality issues and suggest improvements",
backstory="You have 10 years of experience in Python development",
constraints=["Focus on Python best practices", "Follow PEP-8 standards"],
llm_config={"model": "gpt-4"}
)
Benefits:
pip install ag2-persona
# or with modern uv:
uv add ag2-persona
📖 Full Documentation - Complete guides, API reference, and examples
from ag2_persona import PersonaAgent
# Create an agent with a distinct persona
expert = PersonaAgent(
name="data_analyst",
role="Data Analysis Expert",
goal="Analyze the provided dataset and identify key insights",
backstory="You specialize in statistical analysis and data visualization",
llm_config={"model": "gpt-4", "temperature": 0.7}
)
# Use like any AG2 agent
response = expert.generate_reply(messages=[{"content": "Analyze this sales data"}])
Build multi-agent teams by loading expert personas from Markdown files. This enables domain experts to define agent behavior while developers handle runtime integration:
from ag2_persona import PersonaBuilder
from autogen import GroupChat, GroupChatManager
# Load domain expert personas from Markdown library (sync version)
lead_scientist = (PersonaBuilder("lead_scientist")
.from_markdown("library/research_team_lead.md")
.llm_config({"model": "gpt-4", "temperature": 0.7})
.build())
data_analyst = (PersonaBuilder("data_analyst")
.from_markdown("library/senior_data_engineer.md")
.llm_config({"model": "gpt-4", "temperature": 0.5})
.build())
# Manual construction when Markdown doesn't exist yet
lab_technician = (PersonaBuilder("lab_tech")
.role("Laboratory Technician")
.goal("Execute experiments and ensure quality control")
.constraints(["Follow safety protocols", "Document all procedures"])
.llm_config({"model": "gpt-4", "temperature": 0.3})
.build())
# Use in GroupChat - personas are consistent and reusable
groupchat = GroupChat(
agents=[lead_scientist, data_analyst, lab_technician],
messages=[],
max_round=10
)
manager = GroupChatManager(groupchat)
Benefits:
For async applications, use AsyncPersonaBuilder to avoid blocking the event loop:
import asyncio
from ag2_persona import AsyncPersonaBuilder
from autogen import GroupChat, GroupChatManager
async def create_async_team():
# Load personas asynchronously with true fluent chaining - no blocking I/O
lead_agent = await (AsyncPersonaBuilder("lead_scientist")
.from_markdown("library/research_team_lead.md")
.llm_config({"model": "gpt-4", "temperature": 0.7})
.build())
analyst_agent = await (AsyncPersonaBuilder("data_analyst")
.from_markdown("library/senior_data_engineer.md")
.llm_config({"model": "gpt-4", "temperature": 0.5})
.build())
return GroupChat(agents=[lead_agent, analyst_agent], messages=[], max_round=10)
# Usage in async context
async def main():
team = await create_async_team()
# Use team in async AG2 workflows...
# Markdown and async support now included by default
Extends ConversableAgent with persona-based prompt components.
PersonaAgent(
name: str,
role: str,
goal: str,
backstory: str = "",
constraints: Optional[List[str]] = None,
**kwargs # All standard ConversableAgent parameters
)
Parameters:
name (str): Agent identifierrole (str): The agent's role or title (e.g., "Code Reviewer", "Data Scientist")goal (str): What the agent should accomplishbackstory (str, optional): Background, expertise, or contextconstraints (List[str], optional): Limitations or rules to follow**kwargs: Any additional ConversableAgent parametersupdate_goal(new_goal: str) -> NoneDynamically update the agent's goal and regenerate the system message.
agent.update_goal("Review only the security aspects of the code")
role (str): The agent's rolegoal (str): The agent's current goalbackstory (str): The agent's backstoryconstraints (List[str]): The agent's constraintssystem_message (str): The composed system message (inherited)PersonaBuilder supports three construction patterns for different use cases:
from ag2_persona import PersonaBuilder
# Option 1 (Recommended): Load expert personas from Markdown library
expert = (PersonaBuilder("bioinformatics_specialist")
.from_markdown("library/bioinformatics_specialist.md")
.llm_config({"model": "gpt-4", "temperature": 0.5})
.build())
# Option 2: Construct from dictionary
persona_config = {
"role": "Bioinformatics Specialist",
"goal": "Analyze genomic sequences and identify patterns",
"backstory": "PhD in computational biology with expertise in sequence analysis",
"constraints": ["Use validated algorithms", "Ensure reproducibility"]
}
agent = (PersonaBuilder("bioinformatics_specialist")
.from_dict(persona_config)
.llm_config({"model": "gpt-4", "temperature": 0.5})
.build())
# Option 3: Manual construction
agent = (PersonaBuilder("bioinformatics_specialist")
.role("Bioinformatics Specialist")
.goal("Analyze genomic sequences and identify patterns")
.backstory("PhD in computational biology with expertise in sequence analysis")
.constraints(["Use validated algorithms", "Ensure reproducibility"])
.llm_config({"model": "gpt-4", "temperature": 0.5})
.build())
# Async Markdown loading (for async applications)
async def create_async_agent():
return await (AsyncPersonaBuilder("bioinformatics_specialist")
.from_markdown("library/bioinformatics_specialist.md")
.llm_config({"model": "gpt-4", "temperature": 0.5})
.build())
Markdown and async support included by default.
Before:
agent = ConversableAgent(
name="researcher",
system_message="""You are a marine biology researcher with 8 years
of experience studying coral reef ecosystems. Analyze oceanographic
data to identify patterns in coral health. Focus on temperature and
pH correlations. Always cite relevant scientific literature and
follow peer review standards.""",
llm_config=config
)
After:
agent = (PersonaBuilder("researcher")
.role("Marine Biology Researcher")
.goal("Analyze oceanographic data to identify coral health patterns")
.backstory("You have 8 years of experience studying coral reef ecosystems")
.constraints([
"Focus on temperature and pH correlations",
"Always cite relevant scientific literature",
"Follow peer review standards"
])
.llm_config(config)
.build())
CrewAI:
from crewai import Agent
agent = Agent(
role="Climate Scientist",
goal="Analyze climate data and predict weather patterns",
backstory="You are a climate scientist with expertise in atmospheric modeling",
llm={"model": "gpt-4"}
)
AG2 PersonaAgent:
agent = (PersonaBuilder("climate_scientist")
.role("Climate Scientist")
.goal("Analyze climate data and predict weather patterns")
.backstory("You are a climate scientist with expertise in atmospheric modeling")
.llm_config({"model": "gpt-4"})
.build())
A comprehensive collection of pre-built persona agents across various domains is available in examples/library/. These Markdown files provide ready-to-use personas that you can load with PersonaBuilder:
See examples/README.md for complete documentation.
The examples/construction_team.py demonstrates a complete multi-agent system where:
This example showcases the hybrid conversation pattern: human problems → expert AI collaboration.
# Run the construction team example
cd examples/
python construction_team.py
No, PersonaAgent extends ConversableAgent. All existing ConversableAgent features work exactly as before, including function calling, human input modes, and group chats.
Yes! PersonaAgents are fully compatible with regular ConversableAgents in group chats and conversations. You can gradually adopt PersonaAgent without changing your existing setup.
Pass additional system_message content via kwargs - it gets appended to the generated persona message:
agent = PersonaAgent(
name="reviewer",
role="Code Reviewer",
goal="Review code quality",
system_message="Additional custom instructions here",
llm_config=config
)
Use PersonaBuilder with Markdown files for maximum flexibility:
# Recommended: Markdown library pattern
agent = (PersonaBuilder("expert")
.from_markdown("library/domain_expert.md")
.llm_config({"model": "gpt-4", "temperature": 0.7})
.build())
This separates persona definition (stable, edited by domain experts) from runtime configuration (variable, handled by developers).
Submit pull requests that enhance functionality while maintaining backward compatibility. Focus on:
Download counts and popularity will appear here as the collector receives them.
| Version | Published | Direct dependencies | Open version |
|---|---|---|---|
| 0.1.3 | 27 Sept 2025 | 14 |
This describes our collected data; it is not a security assessment.
Observed since 20 Oct, 21:17 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package:eaac1df607b8ede0d2a51bc3ad4e82f9c5d189a1404bc80baf7e3a69443a0932