Skip to content

Commit f9c6616

Browse files
committed
Improve agent client initialization and error logging
Added checks for agent_id presence before returning or creating AzureAIAgentClient instances. Enhanced logging for client creation and error handling, and prevented saving database team agent when agent_id is None.
1 parent 4b1d309 commit f9c6616

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/backend/v4/magentic_agents/common/lifecycle.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,14 @@ def get_chat_client(self, chat_client) -> AzureAIAgentClient:
139139
"""Return the underlying ChatClientProtocol (AzureAIAgentClient)."""
140140
if chat_client:
141141
return chat_client
142-
if self._agent:
142+
if self._agent and self._agent.chat_client and self._agent.chat_client.agent_id is not None:
143143
return self._agent.chat_client # type: ignore
144144
chat_client = AzureAIAgentClient(
145145
project_endpoint=self.project_endpoint,
146146
model_deployment_name=self.model_deployment_name,
147147
async_credential=self.creds,
148148
)
149+
self.logger.info("Created new AzureAIAgentClient for get chat client", extra={"agent_id": chat_client.agent_id})
149150
return chat_client
150151

151152
async def get_database_team_agent(self) -> Optional[AzureAIAgentClient]:
@@ -158,21 +159,25 @@ async def get_database_team_agent(self) -> Optional[AzureAIAgentClient]:
158159
)
159160
if currentAgent and currentAgent.agent_foundry_id:
160161
agent = await self.client.get_agent(agent_id=currentAgent.agent_foundry_id)
161-
if agent:
162+
if agent and agent.agent_id is not None:
162163
chat_client = AzureAIAgentClient(
163164
project_endpoint=self.project_endpoint,
164-
agent_id=currentAgent.agent_foundry_id,
165+
agent_id=agent.agent_id,
165166
model_deployment_name=self.model_deployment_name,
166167
async_credential=self.creds,
167168
)
168169

169170
except Exception as ex: # Consider narrowing this to specific exceptions if possible
170-
self.logger.error("Failed to initialize ReasoningAgentTemplate: %s", ex)
171+
self.logger.error("Failed to initialize Get database team agent: %s", ex)
171172
return chat_client
172173

173174
async def save_database_team_agent(self) -> None:
174175
"""Save current team agent to database."""
175176
try:
177+
if self._agent.chat_client.agent_id is None:
178+
self.logger.error("Cannot save database team agent: agent_id is None")
179+
return
180+
176181
currentAgent = CurrentTeamAgent(
177182
team_id=self.team_config.team_id,
178183
team_name=self.team_config.name,

0 commit comments

Comments
 (0)