diff --git a/python/packages/azure-ai/agent_framework_azure_ai/_client.py b/python/packages/azure-ai/agent_framework_azure_ai/_client.py index 774349a85d..2fa54ea565 100644 --- a/python/packages/azure-ai/agent_framework_azure_ai/_client.py +++ b/python/packages/azure-ai/agent_framework_azure_ai/_client.py @@ -73,7 +73,7 @@ def __init__( Keyword Args: project_client: An existing AIProjectClient to use. If not provided, one will be created. - agent_name: The name to use when creating new agents. + agent_name: The name to use when creating new agents or using existing agents. agent_version: The version of the agent to use. conversation_id: Default conversation ID to use for conversations. Can be overridden by conversation_id property when making a request. @@ -194,17 +194,21 @@ async def _get_agent_reference_or_create( """Determine which agent to use and create if needed. Returns: - str: The agent_name to use + dict[str, str]: The agent reference to use. """ - agent_name = self.agent_name or "UnnamedAgent" + # Agent name must be explicitly provided by the user. + if self.agent_name is None: + raise ServiceInitializationError( + "Agent name is required. Provide 'agent_name' when initializing AzureAIClient " + "or 'name' when initializing ChatAgent." + ) # If no agent_version is provided, either use latest version or create a new agent: if self.agent_version is None: # Try to use latest version if requested and agent exists if self.use_latest_version: try: - existing_agent = await self.project_client.agents.get(agent_name) - self.agent_name = existing_agent.name + existing_agent = await self.project_client.agents.get(self.agent_name) self.agent_version = existing_agent.versions.latest.version return {"name": self.agent_name, "version": self.agent_version, "type": "agent_reference"} except ResourceNotFoundError: @@ -241,13 +245,12 @@ async def _get_agent_reference_or_create( args["instructions"] = "".join(combined_instructions) created_agent = await self.project_client.agents.create_version( - agent_name=agent_name, definition=PromptAgentDefinition(**args) + agent_name=self.agent_name, definition=PromptAgentDefinition(**args) ) - self.agent_name = created_agent.name self.agent_version = created_agent.version - return {"name": agent_name, "version": self.agent_version, "type": "agent_reference"} + return {"name": self.agent_name, "version": self.agent_version, "type": "agent_reference"} async def _close_client_if_needed(self) -> None: """Close project_client session if we created it.""" @@ -276,6 +279,7 @@ def _prepare_input(self, messages: MutableSequence[ChatMessage]) -> tuple[list[C async def prepare_options( self, messages: MutableSequence[ChatMessage], chat_options: ChatOptions ) -> dict[str, Any]: + """Take ChatOptions and create the specific options for Azure AI.""" chat_options.store = bool(chat_options.store or chat_options.store is None) prepared_messages, instructions = self._prepare_input(messages) run_options = await super().prepare_options(prepared_messages, chat_options) diff --git a/python/packages/azure-ai/tests/test_azure_ai_client.py b/python/packages/azure-ai/tests/test_azure_ai_client.py index 576218f270..ead970785d 100644 --- a/python/packages/azure-ai/tests/test_azure_ai_client.py +++ b/python/packages/azure-ai/tests/test_azure_ai_client.py @@ -161,6 +161,16 @@ async def test_azure_ai_client_get_agent_reference_or_create_existing_version( assert agent_ref == {"name": "existing-agent", "version": "1.0", "type": "agent_reference"} +async def test_azure_ai_client_get_agent_reference_or_create_missing_agent_name( + mock_project_client: MagicMock, +) -> None: + """Test _get_agent_reference_or_create raises when agent_name is missing.""" + client = create_test_azure_ai_client(mock_project_client, agent_name=None) + + with pytest.raises(ServiceInitializationError, match="Agent name is required"): + await client._get_agent_reference_or_create({}, None) # type: ignore + + async def test_azure_ai_client_get_agent_reference_or_create_new_agent( mock_project_client: MagicMock, azure_ai_unit_test_env: dict[str, str], diff --git a/python/samples/getting_started/agents/azure_ai/azure_ai_with_code_interpreter.py b/python/samples/getting_started/agents/azure_ai/azure_ai_with_code_interpreter.py index a2ea4aafe3..b81a2add85 100644 --- a/python/samples/getting_started/agents/azure_ai/azure_ai_with_code_interpreter.py +++ b/python/samples/getting_started/agents/azure_ai/azure_ai_with_code_interpreter.py @@ -22,6 +22,7 @@ async def main() -> None: async with ( AzureCliCredential() as credential, AzureAIClient(async_credential=credential).create_agent( + name="MyCodeInterpreterAgent", instructions="You are a helpful assistant that can write and execute Python code to solve problems.", tools=HostedCodeInterpreterTool(), ) as agent,