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 b70cdeafdc..58fb0e0397 100644 --- a/python/packages/azure-ai/agent_framework_azure_ai/_client.py +++ b/python/packages/azure-ai/agent_framework_azure_ai/_client.py @@ -352,9 +352,10 @@ async def _get_agent_reference_or_create( args["text"] = PromptAgentDefinitionText(format=create_text_format_config(response_format)) # Combine instructions from messages and options + # instructions is accessed from chat_options since the base class excludes it from run_options combined_instructions = [ instructions - for instructions in [messages_instructions, run_options.get("instructions")] + for instructions in [messages_instructions, chat_options.get("instructions") if chat_options else None] if instructions ] if combined_instructions: 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 6277c52e9e..694bcb6604 100644 --- a/python/packages/azure-ai/tests/test_azure_ai_client.py +++ b/python/packages/azure-ai/tests/test_azure_ai_client.py @@ -695,16 +695,37 @@ async def test_agent_creation_with_instructions( mock_agent.version = "1.0" mock_project_client.agents.create_version = AsyncMock(return_value=mock_agent) - run_options = {"model": "test-model", "instructions": "Option instructions. "} + run_options = {"model": "test-model"} + chat_options = {"instructions": "Option instructions. "} messages_instructions = "Message instructions. " - await client._get_agent_reference_or_create(run_options, messages_instructions) # type: ignore + await client._get_agent_reference_or_create(run_options, messages_instructions, chat_options) # type: ignore # Verify agent was created with combined instructions call_args = mock_project_client.agents.create_version.call_args assert call_args[1]["definition"].instructions == "Message instructions. Option instructions. " +async def test_agent_creation_with_instructions_from_chat_options( + mock_project_client: MagicMock, +) -> None: + """Test agent creation with instructions passed only via chat_options.""" + client = create_test_azure_ai_client(mock_project_client, agent_name="test-agent") + + mock_agent = MagicMock() + mock_agent.name = "test-agent" + mock_agent.version = "1.0" + mock_project_client.agents.create_version = AsyncMock(return_value=mock_agent) + + run_options = {"model": "test-model"} + chat_options = {"instructions": "Chat options instructions."} + + await client._get_agent_reference_or_create(run_options, None, chat_options) # type: ignore + + call_args = mock_project_client.agents.create_version.call_args + assert call_args[1]["definition"].instructions == "Chat options instructions." + + async def test_agent_creation_with_additional_args( mock_project_client: MagicMock, ) -> None: