@@ -220,30 +220,41 @@ async def create_azure_ai_agent(
220220 if client is None :
221221 client = self .get_ai_project_client ()
222222
223- # First try to get an existing agent with this name as assistant_id
224- try :
225-
226- existing_definition = await client .agents .get_agent (agent_name )
227- # Create the agent instance directly with project_client and existing definition
228- agent = AzureAIAgent (
229- client = client ,
230- definition = existing_definition ,
231- plugins = tools ,
232- )
233-
234- return agent
235- except Exception as e :
236- # The Azure AI Projects SDK throws an exception when the agent doesn't exist
237- # (not returning None), so we catch it and proceed to create a new agent
238- if "ResourceNotFound" in str (e ) or "404" in str (e ):
239- logging .info (
240- f"Agent with ID { agent_name } not found. Will create a new one."
241- )
242- else :
243- # Log unexpected errors but still try to create a new agent
244- logging .warning (
245- f"Unexpected error while retrieving agent { agent_name } : { str (e )} . Attempting to create new agent."
246- )
223+ # # ToDo: This is the fixed code but commenting it out as agent clean up is no happening yet
224+ # # and there are multiple versions of agents due to testing
225+ # # First try to get an existing agent with this name as assistant_id
226+ # try:
227+ # agent_id = None
228+ # agent_list = await client.agents.list_agents()
229+ # for agent in agent_list.data:
230+ # if agent.name == agent_name:
231+ # agent_id = agent.id
232+ # break
233+ # # If the agent already exists, we can use it directly
234+ # # Get the existing agent definition
235+ # existing_definition = await client.agents.get_agent(agent_id)
236+ # # Create the agent instance directly with project_client and existing definition
237+ # agent = AzureAIAgent(
238+ # client=client,
239+ # definition=existing_definition,
240+ # plugins=tools,
241+ # )
242+
243+ # client.agents.list_agents()
244+
245+ # return agent
246+ # except Exception as e:
247+ # # The Azure AI Projects SDK throws an exception when the agent doesn't exist
248+ # # (not returning None), so we catch it and proceed to create a new agent
249+ # if "ResourceNotFound" in str(e) or "404" in str(e):
250+ # logging.info(
251+ # f"Agent with ID {agent_name} not found. Will create a new one."
252+ # )
253+ # else:
254+ # # Log unexpected errors but still try to create a new agent
255+ # logging.warning(
256+ # f"Unexpected error while retrieving agent {agent_name}: {str(e)}. Attempting to create new agent."
257+ # )
247258
248259 # Create the agent using the project client with the agent_name as both name and assistantId
249260 agent_definition = await client .agents .create_agent (
0 commit comments