Python: Fixed instructions duplication in model clients (#1332)

* Small fix

* Updated instruction handling in chat clients
This commit is contained in:
Dmytro Struk
2025-10-09 14:49:03 +00:00
committed by GitHub
parent 8967269d3e
commit a02b82f022
11 changed files with 105 additions and 6 deletions
@@ -830,7 +830,11 @@ class AzureAIAgentClient(BaseChatClient):
run_options["additional_messages"] = additional_messages
# Add instruction from existing agent at the beginning
if agent_definition is not None and agent_definition.instructions:
if (
agent_definition is not None
and agent_definition.instructions
and agent_definition.instructions not in instructions
):
instructions.insert(0, agent_definition.instructions)
if len(instructions) > 0:
@@ -554,6 +554,19 @@ async def test_azure_ai_chat_client_create_run_options_with_messages(mock_ai_pro
assert len(run_options["additional_messages"]) == 1 # Only user message
async def test_azure_ai_chat_client_instructions_sent_once(mock_ai_project_client: MagicMock) -> None:
"""Ensure instructions are only sent once for AzureAIAgentClient."""
chat_client = create_test_azure_ai_chat_client(mock_ai_project_client)
instructions = "You are a helpful assistant."
chat_options = ChatOptions(instructions=instructions)
messages = chat_client.prepare_messages([ChatMessage(role=Role.USER, text="Hello")], chat_options)
run_options, _ = await chat_client._create_run_options(messages, chat_options) # type: ignore
assert run_options.get("instructions") == instructions
async def test_azure_ai_chat_client_inner_get_response(mock_ai_project_client: MagicMock) -> None:
"""Test _inner_get_response method."""
chat_client = create_test_azure_ai_chat_client(mock_ai_project_client, agent_id="test-agent")