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 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
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 * NO MERGE * Do not merge this PR as long as this label is present.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants