@@ -152,29 +152,37 @@ async def create_agent(
152152 agent_type_str = cls ._agent_type_strings .get (agent_type , agent_type .value .lower ())
153153 tools = await cls ._load_tools_for_agent (kernel , agent_type_str )
154154
155- # Build the agent definition (functions schema) if tools exist
155+ # Build the agent definition (functions schema)
156156 definition = None
157157 client = None
158+
158159 try :
159160 client = config .get_ai_project_client ()
160161 except Exception as client_exc :
161162 logger .error (f"Error creating AIProjectClient: { client_exc } " )
162- raise
163+ if agent_type == AgentType .GROUP_CHAT_MANAGER :
164+ logger .info (f"Continuing with GroupChatManager creation despite AIProjectClient error" )
165+ else :
166+ raise
167+
163168 try :
164- if tools :
165- # Create the agent definition using the AIProjectClient (project-based pattern)
169+ # Create the agent definition using the AIProjectClient (project-based pattern)
170+ # For GroupChatManager, create a definition with minimal configuration
171+ if client is not None :
166172 definition = await client .agents .create_agent (
167173 model = config .AZURE_OPENAI_DEPLOYMENT_NAME ,
168174 name = agent_type_str ,
169175 instructions = system_message ,
170176 temperature = temperature ,
171177 response_format = None # Add response_format if required
172178 )
179+ logger .info (f"Successfully created agent definition for { agent_type_str } " )
173180 except Exception as agent_exc :
174- logger .error (f"Error creating agent definition with AIProjectClient: { agent_exc } " )
175- raise
176- if definition is None :
177- raise RuntimeError ("Failed to create agent definition from Azure AI Project. Check your Azure configuration, permissions, and network connectivity." )
181+ logger .error (f"Error creating agent definition with AIProjectClient for { agent_type_str } : { agent_exc } " )
182+ if agent_type == AgentType .GROUP_CHAT_MANAGER :
183+ logger .info (f"Continuing with GroupChatManager creation despite definition error" )
184+ else :
185+ raise
178186
179187 # Create the agent instance using the project-based pattern
180188 try :
@@ -215,7 +223,7 @@ async def create_agent(
215223 cls ._agent_cache [session_id ][agent_type ] = agent
216224
217225 return agent
218-
226+
219227 @classmethod
220228 async def create_azure_ai_agent (
221229 cls ,
@@ -272,7 +280,7 @@ async def _load_tools_for_agent(cls, kernel: Kernel, agent_type: str) -> List[Ke
272280 """Load tools for an agent from the tools directory.
273281
274282 This tries to load tool configurations from JSON files. If that fails,
275- it creates a simple helper function as a fallback .
283+ it returns an empty list for agents that don't need tools .
276284
277285 Args:
278286 kernel: The semantic kernel instance
@@ -286,9 +294,20 @@ async def _load_tools_for_agent(cls, kernel: Kernel, agent_type: str) -> List[Ke
286294 tools = BaseAgent .get_tools_from_config (kernel , agent_type )
287295 logger .info (f"Successfully loaded { len (tools )} tools for { agent_type } " )
288296 return tools
297+ except FileNotFoundError :
298+ # No tool configuration file found - this is expected for some agents
299+ logger .info (f"No tools defined for agent type '{ agent_type } '. Returning empty list." )
300+ return []
289301 except Exception as e :
290- logger .warning (f"Failed to load tools for { agent_type } , using fallback : { e } " )
302+ logger .warning (f"Error loading tools for { agent_type } : { e } " )
291303
304+ # Return an empty list for agents without tools rather than attempting a fallback
305+ # Special handling for group_chat_manager which typically doesn't need tools
306+ if "group_chat_manager" in agent_type :
307+ logger .info (f"No tools needed for { agent_type } . Returning empty list." )
308+ return []
309+
310+ # For other agent types, try to create a simple fallback tool
292311 try :
293312 # Use PromptTemplateConfig to create a simple tool
294313 from semantic_kernel .prompt_template .prompt_template_config import PromptTemplateConfig
@@ -319,7 +338,7 @@ async def _load_tools_for_agent(cls, kernel: Kernel, agent_type: str) -> List[Ke
319338 return [function ]
320339 except Exception as fallback_error :
321340 logger .error (f"Failed to create fallback tool for { agent_type } : { fallback_error } " )
322- # Return an empty list if everything fails
341+ # Return an empty list if everything fails - the agent can still function without tools
323342 return []
324343
325344 @classmethod
@@ -343,16 +362,56 @@ async def create_all_agents(
343362 if session_id in cls ._agent_cache and len (cls ._agent_cache [session_id ]) == len (cls ._agent_classes ):
344363 return cls ._agent_cache [session_id ]
345364
346- # Create each agent type
365+ # Create each agent type in two phases
366+ # First, create all agents except PlannerAgent and GroupChatManager
347367 agents = {}
348- for agent_type in cls ._agent_classes .keys ():
368+ planner_agent_type = AgentType .PLANNER
369+ group_chat_manager_type = AgentType .GROUP_CHAT_MANAGER
370+
371+ # Initialize cache for this session if it doesn't exist
372+ if session_id not in cls ._agent_cache :
373+ cls ._agent_cache [session_id ] = {}
374+
375+ # Phase 1: Create all agents except planner and group chat manager
376+ for agent_type in [at for at in cls ._agent_classes .keys ()
377+ if at != planner_agent_type and at != group_chat_manager_type ]:
349378 agents [agent_type ] = await cls .create_agent (
350379 agent_type = agent_type ,
351380 session_id = session_id ,
352381 user_id = user_id ,
353382 temperature = temperature
354383 )
355-
384+
385+ # Create agent name to instance mapping for the planner
386+ agent_instances = {}
387+ for agent_type , agent in agents .items ():
388+ agent_name = cls ._agent_type_strings .get (agent_type ).replace ("_" , "" ) + "Agent"
389+ agent_name = agent_name [0 ].upper () + agent_name [1 :] # Capitalize first letter
390+ agent_instances [agent_name ] = agent
391+
392+ # Log the agent instances for debugging
393+ logger .debug (f"Created { len (agent_instances )} agent instances for planner: { ', ' .join (agent_instances .keys ())} " )
394+
395+ # Phase 2: Create the planner agent with agent_instances
396+ planner_agent = await cls .create_agent (
397+ agent_type = planner_agent_type ,
398+ session_id = session_id ,
399+ user_id = user_id ,
400+ temperature = temperature ,
401+ agent_instances = agent_instances # Pass agent instances to the planner
402+ )
403+ agents [planner_agent_type ] = planner_agent
404+
405+ # Phase 3: Create group chat manager with all agents including the planner
406+ group_chat_manager = await cls .create_agent (
407+ agent_type = group_chat_manager_type ,
408+ session_id = session_id ,
409+ user_id = user_id ,
410+ temperature = temperature ,
411+ available_agents = agent_instances # Pass all agents to group chat manager
412+ )
413+ agents [group_chat_manager_type ] = group_chat_manager
414+
356415 return agents
357416
358417 @classmethod
0 commit comments