Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 12, 2025

FunctionInvokingChatClient was attempting to invoke every FunctionCallContent it encountered, even when a corresponding FunctionResultContent with the same CallId was already present in the response. This broke scenarios with multiple FunctionInvokingChatClients in the pipeline or inner clients that handle function invocation internally.

Changes

  • Filter already-handled FCCs: Added RemoveAlreadyHandledFunctionCalls methods that use a HashSet<string> for O(1) CallId lookups to identify and exclude FCCs with matching FRCs
  • Non-streaming path: Filter FCCs after copying them from the response
  • Streaming path: Collect all updates before filtering/converting to approval requests, ensuring FRCs that appear later in the stream are considered
  • Approval-required functions: Skip converting FCCs to approval requests if they already have matching FRCs

Example

// Inner client returns both FCC and FRC (e.g., it already handled the function call)
var response = new ChatResponse([
    new ChatMessage(ChatRole.Assistant, [
        new FunctionCallContent("callId1", "Func1"),
        new FunctionResultContent("callId1", result: "Already handled by inner client")
    ])
]);

// FunctionInvokingChatClient now correctly ignores this FCC
// instead of trying to invoke Func1 again

Tests Added

  • Inner client producing both FCC and FRC (streaming and non-streaming)
  • Multiple FunctionInvokingChatClients in pipeline
  • Partial matches (some FCCs with FRCs, some without)
  • Approval-required functions with already-handled FCCs

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • securitytools.pkgs.visualstudio.com
    • Triggering command: /opt/hostedtoolcache/CodeQL/2.23.7/x64/codeql/csharp/tools/linux64/Semmle.Autobuild.CSharp /opt/hostedtoolcache/CodeQL/2.23.7/x64/codeql/csharp/tools/linux64/Semmle.Autobuild.CSharp (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

Currently FunctionInvokingChatClient looks for FunctionCallContent and tries to handle any FunctionCallContent. However, this fails for scenarios where the FunctionCallContent represents a function call request that was already handled upstream. For example, maybe there are multiple FunctionInvokingChatClients in the pipeline, or maybe the IChatClient itself handles function invocation and just sends out the FunctionCallContent as an indication of what it did and not what needs to be done by the caller. In general, the caller should consider any FunctionCallContent as already handled if there's a FunctionResultContent available for it after it in the response (or response stream). Please update FunctionInvokingChatClient in that regard, not just always handling any FunctionCallContent and instead ignoring any FunctionCallContent that has a paired FunctionResultContent (one with the same call id). Pay special attention to algorithmic complexity; we want to avoid situations that could result in O(N^2) algorithms as we're processing messages and AIContent. Also please make sure this is well tested, covering cases where the inner IChatClient produces both FCC and FRC, streaming and non-streaming, cases involving approval and not, etc.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Microsoft Reviewers: Open in CodeFlow

@stephentoub stephentoub added * NO MERGE * Do not merge this PR as long as this label is present. area-ai Microsoft.Extensions.AI libraries labels Dec 12, 2025
Copilot AI and others added 3 commits December 12, 2025 17:08
…unctionInvokingChatClient

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot AI changed the title [WIP] Update FunctionInvokingChatClient to ignore handled FunctionCallContent Ignore FunctionCallContent with paired FunctionResultContent in FunctionInvokingChatClient Dec 12, 2025
Copilot AI requested a review from stephentoub December 12, 2025 17:18
@stephentoub stephentoub removed the * NO MERGE * Do not merge this PR as long as this label is present. label Jan 5, 2026
@stephentoub stephentoub marked this pull request as ready for review January 6, 2026 16:34
@stephentoub stephentoub requested a review from a team as a code owner January 6, 2026 16:34
Copilot AI review requested due to automatic review settings January 6, 2026 16:34
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes FunctionInvokingChatClient to avoid re-invoking function calls that have already been handled (indicated by the presence of a matching FunctionResultContent with the same CallId). This resolves issues with multiple FunctionInvokingChatClients in a pipeline or inner clients that handle function invocation internally.

Key Changes

  • Added filtering logic to identify and exclude FunctionCallContent items that have corresponding FunctionResultContent with matching CallIds
  • Implemented O(1) lookup using HashSet<string> to efficiently filter already-handled function calls
  • Extended approval-required function handling to skip FCCs with matching FRCs

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs Added two RemoveAlreadyHandledFunctionCalls methods for filtering; updated non-streaming and streaming paths to filter FCCs with matching FRCs; updated approval-request logic to exclude already-handled FCCs
test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/FunctionInvokingChatClientTests.cs Added comprehensive tests for non-streaming and streaming scenarios with FCCs and matching FRCs, including partial matches and multiple clients in pipeline
test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/FunctionInvokingChatClientApprovalsTests.cs Updated existing test expectations for buffering behavior; added tests for approval-required functions with already-handled FCCs

Comment on lines +1622 to +1660
[Fact]
public async Task MultipleFunctionInvokingClientsInPipeline_NonStreaming()
{
// When there are multiple FunctionInvokingChatClients in the pipeline,
// the outer one should not re-invoke functions already handled by the inner one.
int func1InvocationCount = 0;

var func1 = AIFunctionFactory.Create(() => { func1InvocationCount++; return "func1 result"; }, "Func1");

var options = new ChatOptions { Tools = [func1] };

int outerCallIndex = 0;
using var baseClient = new TestChatClient
{
GetResponseAsyncCallback = (chatContents, chatOptions, cancellationToken) =>
{
outerCallIndex++;
if (outerCallIndex == 1)
{
// First call: return FCC
return Task.FromResult(new ChatResponse(new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1")])));
}

// Second call: return final response
return Task.FromResult(new ChatResponse(new ChatMessage(ChatRole.Assistant, "done")));
}
};

// Create a pipeline with two FunctionInvokingChatClients
using var innerFunctionInvokingClient = new FunctionInvokingChatClient(baseClient);
using var outerFunctionInvokingClient = new FunctionInvokingChatClient(innerFunctionInvokingClient);

var result = await outerFunctionInvokingClient.GetResponseAsync([new ChatMessage(ChatRole.User, "hello")], options);

// The function should have been invoked exactly ONCE (by the inner FunctionInvokingChatClient)
// The outer one should see the FRC and not re-invoke
Assert.Equal(1, func1InvocationCount);
Assert.Equal("done", result.Text);
}
Copy link

Copilot AI Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test coverage gap: There's a test for multiple FunctionInvokingChatClients in the pipeline for non-streaming (MultipleFunctionInvokingClientsInPipeline_NonStreaming), but no corresponding streaming test. Consider adding a MultipleFunctionInvokingClientsInPipeline_Streaming test to ensure the filtering logic works correctly for this scenario in the streaming path.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-ai Microsoft.Extensions.AI libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants