MS Foundry X Agent Framework: GetAgentAsync fails with "The requested operation requires an element of type 'Object', but the target element has type 'Array'"

Jason Lee 226 Reputation points
2025-12-03T15:41:13.8433333+00:00

Hi,

I'm using a simplified version of the official code example for using a MS Foundry agent from a Web API. More specifically, my end goal is to get a MS Foundry Agent to work with Agent Framework as a function tool like in this example which is for the older AI Foundry. When I try to retrieve the agent using GetAgentAsync (i.e. first line in my GetFunction method), I get the error System.InvalidOperationException: 'The requested operation requires an element of type 'Object', but the target element has type 'Array'.' Here's my code. Am I doing this properly?

public class WeekendPlannerAgent
{
    private readonly IConfiguration _configuration;
    private readonly ILogger<WeekendPlannerAgent> _logger;
    private readonly AIProjectClient _projectClient;

    public WeekendPlannerAgent(IConfiguration configuration, ILogger<WeekendPlannerAgent> logger)
    {
        _configuration = configuration;
        _logger = logger;

        _projectClient = new AIProjectClient(
        new Uri("https://---.services.ai.azure.com/api/projects/----"),
        new DefaultAzureCredential());
    }

    public async Task<AIFunction> GetFunction()
    {
        AgentRecord agentRecord = await _projectClient.Agents.GetAgentAsync("weekend-planner");
        var agentVersion = agentRecord.Versions.Latest;
        var responseClient = _projectClient.OpenAI.GetProjectResponsesClientForAgent(agentVersion);
        var chatClient = responseClient.AsIChatClient();
        var agent = chatClient.CreateAIAgent();
        return agent.AsAIFunction();
    }
}

I have deployed the agent as shown here.User's image

Here's relevant dependencies in my .csproj.

    <PackageReference Include="Azure.AI.Projects" Version="1.2.0-beta.4" />
    <PackageReference Include="Microsoft.Agents.AI.AzureAI" Version="1.0.0-preview.251125.1" />

The official code example doesn't show what I should use for agent ID, is mine correct? In the deprecated AI Foundry, it was a random series of numbers prefixed by "asst_", but I don't see that in the new MS Foundry.

This may be related to the same root cause (bug?) as in my other post since the error is the same, but in completely different context/scenario.

Also feel free to tell me I'm integrating this MS Foundry agent incorrectly with Agent Framework.

Thanks in advance!

Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Gowtham CP 6,920 Reputation points Volunteer Moderator
    2025-12-04T06:47:52.1533333+00:00

    Hi Jason Lee ,

    Thank you for reaching out on the Microsoft Q&A forum.

    To retrieve your agent using GetAgentAsync, please ensure you are using the Agent ID (GUID) from the Azure AI Foundry portal:

    Azure AI Foundry → Project → Agents → Select Agent → Agent Details → Agent ID

    Then update your code as follows:

    string agentId = "<your-agent-id>";
    AgentRecord agentRecord = await _projectClient.Agents.GetAgentAsync(agentId);
    
    var agentVersion = agentRecord.Versions.Latest;
    var responseClient = _projectClient.OpenAI.GetProjectResponsesClientForAgent(agentVersion);
    
    var chatClient = responseClient.AsIChatClient();
    var agent = chatClient.CreateAIAgent();
    return agent.AsAIFunction();
    

    Please also confirm that the agent version is published and deployed within your AI Project.

    References:

    Azure AI Foundry — Agents Overview https://learn.microsoft.com/en-us/azure/ai-foundry/agents/overview

    Quickstart — Create an Agent in Azure AI Foundry https://learn.microsoft.com/en-us/azure/ai-foundry/agents/quickstart

    Microsoft Agent Framework — Azure AI Foundry Agent https://learn.microsoft.com/en-us/agent-framework/user-guide/agents/agent-types/azure-ai-foundry-agent

    I hope this helps!

    If the information is useful, please accept the answer and upvote it to close the thread. Thanks.


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.