Share via

ConnectedAgentTool not work

桂學文 Kevin Kuei 155 Reputation points
2025-06-03T09:56:22.7333333+00:00

Hi,

I tried to follow the sample code to pack two agents (with AISearch as tool) as ConnectedAgentTool.
https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-agents/samples/agents_tools/sample_agents_multiple_connected_agents.py

Then I construct a main agent to use the two ConnectedAgentTools.

It looks like both ConnectedAgentTools not work.

According to the display output as below, I can see the main agent did call connected_agent twice but the result are always wrong.

# python 7_Connected_Agent_Tool.py 
Created agent, ID: asst_9iiI0I1P4cNrAUGdVuf2IXcS
Created thread, ID: thread_0IU98KyKEqGf1zgbQnosjeTj
Created message, ID: msg_VsrpqOL72iVqSG9iYqmn058k
Run finished with status: completed
Step step_nCDwiQqc65Ahw1bU7fd4b89Y status: completed
Step step_JNbnCkqKeRx0W73zhbISrOA3 status: completed
  Tool calls:
    Tool Call ID: call_efppy7ZUtiPTnQX8IpmdlTXQ
    Type: connected_agent
Step step_5DtBkfoKMk32SiEvssHaVAXz status: completed
  Tool calls:
    Tool Call ID: call_AFOaJ0sh3xVyd3tX6Y6aZBHG
    Type: connected_agent
Deleted agent
user: 請問鐵道補助案補助金額最高多少? 然後請問銀英傳中同盟軍第四艦隊司令官是誰?
assistant: 關於您的提問:
1. 鐵道補助案的最高補助金額因地區與政策的不同而異,建議查詢當地最新的相關政策或詢問主管機關以獲得確定的金額範圍。
   
2. 在《銀河英雄傳說》中,同盟軍第四艦隊的司令官是**帕斯特拉·魯格**,他在亞斯提星會戰中曾率領艦隊執行任務。

The python code is here:

from azure.ai.agents import AgentsClient
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
from azure.ai.agents.models import ConnectedAgentTool, AzureAISearchQueryType, AzureAISearchTool, BingGroundingTool, ListSortOrder, MessageRole, ToolSet, FunctionTool
project_endpoint = "https://pj-kk-xxx-resource.services.ai.azure.com/api/projects/pj-kk-xxx"
project_client = AIProjectClient(
    endpoint=project_endpoint,
    credential=DefaultAzureCredential(),
)
# agents_client
agents_client = AgentsClient(
    endpoint=project_endpoint,
    credential=DefaultAzureCredential(),
)
# railway_agent with tool
aisearch_railway = AzureAISearchTool(
    index_connection_id="aisearchkkxxx", index_name="idx-kk-xxx", query_type=AzureAISearchQueryType.SIMPLE, top_k=3, filter=""
)
railway_agent = agents_client.create_agent(
    model="gpt-4o",
    name="railway_agent",
    instructions=(
        "你的工作是處理鐵道方面的問題."
    ),
    tools=aisearch_railway.definitions,
    tool_resources=aisearch_railway.resources,
)
# galaxy_agent with tool
aisearch_galaxy = AzureAISearchTool(
    index_connection_id="aisearchkkxxx", index_name="idx-kk-yyy", query_type=AzureAISearchQueryType.SIMPLE, top_k=3, filter=""
)
galaxy_agent = agents_client.create_agent(
    model="gpt-4o",
    name="galaxy_agent",
    instructions=(
        "你的工作是處理關於銀河英雄傳說(銀英傳)的問題."
    ),
    tools=aisearch_galaxy.definitions,
    tool_resources=aisearch_galaxy.resources,
)
# Initialize Connected Agent tools with the agent id, name, and description
connected_railway_agent = ConnectedAgentTool(
    id=railway_agent.id, name="railway_bot", description="專門處理鐵道方面事物"
)
connected_galaxy_agent = ConnectedAgentTool(
    id=galaxy_agent.id, name="galaxy_bot", description="專門處理銀河英雄傳說(銀英傳)"
)
# ToolSet
#toolset = ToolSet()
#toolset.add(connected_railway_agent)
#toolset.add(connected_galaxy_agent)
# To enable tool calls executed automatically
#agents_client.enable_auto_function_calls(toolset)
# Create agent with AI search tool and process agent run
with agents_client:
    agent = agents_client.create_agent(
        model="gpt-4o",
        name="agent-sdk-kk-with-toolset",
        instructions="You are a helpful agent. To answer user question, you can use tools to retrieve information from AI Search service or use Bing search to search Internet",
        #toolset=toolset,
        tools = [
            connected_railway_agent.definitions[0],
            connected_galaxy_agent.definitions[0],
        ],
    )
    # [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="請問鐵道補助案補助金額最高多少? 然後請問銀英傳中同盟軍第四艦隊司令官是誰?",
    )
    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)
    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", {})
        tool_calls = step_details.get("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]
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  

Azure AI Search
Azure AI Search

An Azure search service with built-in artificial intelligence capabilities that enrich information to help identify and explore relevant content at scale.


Answer accepted by question author

Shree Hima Bindu Maganti 7,420 Reputation points Microsoft External Staff Moderator
2025-06-04T11:46:32.1966667+00:00

Hi 桂學文 Kevin Kuei
Thanks for your Update
I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this!

Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to "Accept " the answer.

Issue:
ConnectedAgentTool not work
Solution:
The instructions is tuple in the sample code. https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-agents/samples/agents_tools/sample_agents_multiple_connected_agents.py#L51C1-L53C7

PythonCopy

weather_agent = agents_client.create_agent(
    model=os.environ["MODEL_DEPLOYMENT_NAME"],
    name=weather_agent_name,
    instructions=(
        "Your job is to get the weather for a given location. If asked for the weather in Seattle, always return 60 degrees and cloudy."
    ),
)

I modified it to be a simple string, then it worked.
Please click Accept Answer and kindly upvote it so that other people who faces similar issue may get benefitted from it.

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. 桂學文 Kevin Kuei 155 Reputation points
    2025-06-04T03:27:31.71+00:00

    I've solved my problem.

    The instructions is tuple in the sample code.
    https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-agents/samples/agents_tools/sample_agents_multiple_connected_agents.py#L51C1-L53C7

    weather_agent = agents_client.create_agent(
        model=os.environ["MODEL_DEPLOYMENT_NAME"],
        name=weather_agent_name,
        instructions=(
            "Your job is to get the weather for a given location. If asked for the weather in Seattle, always return 60 degrees and cloudy."
        ),
    )
    

    I modified it to be a simple string, then it worked.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.