azure-sdk-for-python icon indicating copy to clipboard operation
azure-sdk-for-python copied to clipboard

Unable to connect the agent with AI Search Tool

Open Yugal41735 opened this issue 4 months ago • 8 comments

  • Package Name: Azure ai
  • Package Version: Latest
  • Operating System: Windows 11
  • Python Version: 3.13

Describe the bug Tried to connect the agent with the ai search tool using the template present in the github. But getting the following error: Run failed: {'code': 'tool_user_error', 'message': 'Error: search_service_request_error; Unable to connect to Azure AI Search Resource. Please ensure the Azure AI Search Connection has the correct endpoint and the search resouce has appropriate network settings for the agents setup. Cannot connect to host azureaiservice.search.windows.net:443 ssl:default [DNS server returned answer with no data]'}

To Reproduce Steps to reproduce the behavior: code:

pylint: disable=line-too-long,useless-suppression

------------------------------------

Copyright (c) Microsoft Corporation.

Licensed under the MIT License.

------------------------------------

""" DESCRIPTION: This sample demonstrates how to use agent operations with the Azure AI Search tool from the Azure agents service using a synchronous client.

PREREQUISITES: You will need an Azure AI Search Resource. If you already have one, you must create an agent that can use an existing Azure AI Search index: https://learn.microsoft.com/azure/ai-services/agents/how-to/tools/azure-ai-search?tabs=azurecli%2Cpython&pivots=overview-azure-ai-search

If you do not already have an agent Setup with an Azure AI Search resource, follow the guide for a Standard agent setup:
https://learn.microsoft.com/azure/ai-services/agents/quickstart?pivots=programming-language-python-azure

USAGE: python sample_agents_azure_ai_search.py

Before running the sample:

pip install azure-ai-projects azure-ai-projects azure-identity

Set these environment variables with your own values:
1) PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
                      page of your Azure AI Foundry portal.
2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
   the "Models + endpoints" tab in your Azure AI Foundry project.
3) AI_SEARCH_CONNECTION_NAME - The connection name of the AI Search connection to your Foundry project,
   as found under the "Name" column in the "Connected Resources" tab in your Azure AI Foundry project.

"""

import os from azure.ai.projects import AIProjectClient from azure.ai.projects.models import ConnectionType from azure.identity import DefaultAzureCredential from azure.ai.agents.models import AzureAISearchQueryType, AzureAISearchTool, ListSortOrder, MessageRole, AgentsNamedToolChoice

with AIProjectClient( endpoint=os.environ["PROJECT_ENDPOINT"], credential=DefaultAzureCredential(), ) as project_client:

# [START create_agent_with_azure_ai_search_tool]
conn_id = project_client.connections.get_default(ConnectionType.AZURE_AI_SEARCH).id

print(conn_id)

# Initialize agent AI search tool and add the search index connection id
ai_search = AzureAISearchTool(
    index_connection_id=conn_id,
    index_name="sample-index",
    query_type=AzureAISearchQueryType.SIMPLE,
    top_k=3,
    filter="",
)

# Create agent with AI search tool and process agent run
agents_client = project_client.agents

agent = agents_client.create_agent(
    model=os.environ["MODEL_DEPLOYMENT_NAME"],
    name="my-agent",
    instructions="You are a helpful agent. Always make call to AI Search Tool to answer the user.",
    tools=ai_search.definitions,
    tool_resources=ai_search.resources,
)
# [END create_agent_with_azure_ai_search_tool]
print(f"Created agent, ID: {agent.id}")

# Create thread for communication
thread = agents_client.threads.create()
print(f"Created thread, ID: {thread.id}")

# Create message to thread
message = agents_client.messages.create(
    thread_id=thread.id,
    role="user",
    content="What features does Premium include? Use the ai search tool to answer this question",
)
print(f"Created message, ID: {message.id}")

# Create and process agent run in thread with tools
run = agents_client.runs.create_and_process(
    thread_id=thread.id, 
    agent_id=agent.id
    # tool_choice=AgentsNamedToolChoice(type="azure_ai_search")
)
print(f"Run finished with status: {run.status}")

if run.status == "failed":
    print(f"Run failed: {run.last_error}")

# Fetch run steps to get the details of the agent run
run_steps = agents_client.run_steps.list(thread_id=thread.id, run_id=run.id)
for step in run_steps:
    print(f"Step {step['id']} status: {step['status']}")
    step_details = step.get("step_details", {})
    print(f"  Tool choice: {step_details}")
    tool_calls = step_details.get("tool_calls", [])
    # print(f"  Tool calls : {tool_calls}")

    if tool_calls:
        print("  Tool calls:")
        for call in tool_calls:
            print(f"    Tool Call ID: {call.get('id')}")
            print(f"    Type: {call.get('type')}")

            azure_ai_search_details = call.get("azure_ai_search", {})
            if azure_ai_search_details:
                print(f"    azure_ai_search input: {azure_ai_search_details.get('input')}")
                print(f"    azure_ai_search output: {azure_ai_search_details.get('output')}")
    print()  # add an extra newline between steps

# Delete the agent when done
agents_client.delete_agent(agent.id)
print("Deleted agent")

# [START populate_references_agent_with_azure_ai_search_tool]
# Fetch and log all messages
messages = agents_client.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING)
for message in messages:
    if message.role == MessageRole.AGENT and message.url_citation_annotations:
        placeholder_annotations = {
            annotation.text: f" [see {annotation.url_citation.title}] ({annotation.url_citation.url})"
            for annotation in message.url_citation_annotations
        }
        for message_text in message.text_messages:
            message_str = message_text.text.value
            for k, v in placeholder_annotations.items():
                message_str = message_str.replace(k, v)
            print(f"{message.role}: {message_str}")
    else:
        for message_text in message.text_messages:
            print(f"{message.role}: {message_text.text.value}")
# [END populate_references_agent_with_azure_ai_search_tool]

Expected behavior Expected it to give result by searching the ai search tool bar.

Screenshots If applicable, add screenshots to help explain your problem.

Additional context Add any other context about the problem here.

Yugal41735 avatar Oct 01 '25 15:10 Yugal41735

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @dargilco @glharper @howieleung @jhakulin @nick863 @trangevi.

github-actions[bot] avatar Oct 01 '25 15:10 github-actions[bot]

Hi @Yugal41735 , are you using Agents and Azure AI Search in a private networking scenario, or public networking?

If private networking, please make sure to follow the docs to provision the vnet and azure search resource properly: https://learn.microsoft.com/en-us/azure/ai-foundry/agents/how-to/virtual-networks

If public networking, please remove any private endpoints on the Azure AI Search resource. Mixing a public network agent setup with private endpoints on search is not supported, and has caused this kind of DNS issue before.

sarah-widder avatar Oct 01 '25 18:10 sarah-widder

Hey @sarah-widder , Thanks for the response. Is this only for the Azure Ai Search or i have to keep this in mind for azure foundry project too? And would i have to disable the public network completely ai search, or would it work if i select the option of Selected IP addresses?

Image

And lets say i have a private endpoint already there in the ai search resource, so how do i connect to the ai search through my agent?

Yugal41735 avatar Oct 01 '25 18:10 Yugal41735

We are facing the same problem, is there anyway to make this work. Because the network injection for the agent is not working even after contacting Microsoft support. We need at all cost to keep Ai Search private for security reasons, it seems weird that Ai Agent doesn't support this simple configuration.

midokate avatar Oct 03 '25 11:10 midokate

hello @dargilco and team can you please look into this.

Yugal41735 avatar Oct 06 '25 05:10 Yugal41735

Hi @Yugal41735 @midokate to use Azure AI Search in private networks, you need to put the whole foundry project in the bring-your-own-vnet setup: https://learn.microsoft.com/en-us/azure/ai-foundry/agents/how-to/virtual-networks

This is the only way to use Azure AI Search with private networks with the foundry agent service. Selected IP addresses or manually creating private endpoints will not work. This is due to the system architecture of agent service, you need to configure additional things in the foundry control plane which the vnet documentation will guide you to do (generally using our provided bicep template).

sarah-widder avatar Oct 06 '25 22:10 sarah-widder

Hello @sarah-widder . Thanks for responding. One thing more i need to know lets say i am using databricks which is a resource present in my azure portal. I am running my code on the databricks, and lets say we make changes which you suggested above to make the whole foundry project in my own vnet setup, and vn of my databricks is different from that vn of this whole new setup, so would it still work if i try to run the code on my databricks?

and one more thing, i want to know lets say i have private endpoint enabled in my ai search resource, and if by vnet peering i connect my db vn with the ai search private endpoint vn, would it work?

Yugal41735 avatar Oct 07 '25 10:10 Yugal41735

@sarah-widder Would you kindly offer a solution? We are dealing with similar problems when integrating Azure AI Search with AI Foundry.

Akashmax12 avatar Dec 11 '25 09:12 Akashmax12