Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions python/packages/azure-ai/agent_framework_azure_ai/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions python/packages/azure-ai/tests/test_azure_ai_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down