Skip to content

Support invoke_workflow as an equivalent parent span to invoke_agent in FunctionInvokingChatClient#7382

Merged
stephentoub merged 6 commits intomainfrom
copilot/update-function-invoking-chat-client-again
Mar 12, 2026
Merged

Support invoke_workflow as an equivalent parent span to invoke_agent in FunctionInvokingChatClient#7382
stephentoub merged 6 commits intomainfrom
copilot/update-function-invoking-chat-client-again

Conversation

Copy link
Contributor

Copilot AI commented Mar 10, 2026

  • Add InvokeWorkflowName = "invoke_workflow" constant to OpenTelemetryConsts.cs
  • Extract IsActivityDisplayNameMatch helper in FunctionInvokingChatClient.cs and update CurrentActivityIsInvokeAgent to also match invoke_workflow spans
  • Update XML doc / code comments to mention invoke_workflow
  • Refactor tests to cover invoke_workflow in existing tests rather than separate methods
  • Fix IDE0055 formatting error: remove stray whitespace character on blank line at FunctionInvokingChatClient.cs:1279
Original prompt

Background

The OpenTelemetry semantic conventions PR open-telemetry/semantic-conventions#3249 (merged) adds a new invoke_workflow operation name to the GenAI semantic conventions. This represents a coordinated process composed of multiple agents or other operations involving generative AI — essentially a parent grouping span similar to invoke_agent.

Problem

Currently, FunctionInvokingChatClient has special behavior when it detects it is running inside an invoke_agent span:

  1. Suppresses the orchestrate_tools span — since invoke_agent already serves as a grouping span
  2. Reuses the agent's ActivitySource — so that child execute_tool spans are properly parented
  3. Propagates EnableSensitiveData — reads the custom property from the agent activity

The invoke_workflow span serves the same parent-grouping role, so FunctionInvokingChatClient should treat it identically.

Required Changes

1. src/Libraries/Microsoft.Extensions.AI/OpenTelemetryConsts.cs

Add a new constant after InvokeAgentName:

public const string InvokeWorkflowName = "invoke_workflow";

2. src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs

Update CurrentActivityIsInvokeAgent to also match invoke_workflow parent spans. The property currently checks if Activity.Current.DisplayName starts with "invoke_agent" followed by end-of-string or a space. It should be generalized to also match "invoke_workflow" the same way. The cleanest approach:

  • Extract a helper method static bool IsActivityDisplayNameMatch(string? displayName, string operationName) that does the StartsWith + boundary check
  • Update CurrentActivityIsInvokeAgent to check both InvokeAgentName and InvokeWorkflowName using the helper (or rename the property to reflect both)
  • Update the XML doc comment and code comments referencing "invoke_agent" to also mention "invoke_workflow"

This affects these call sites (no changes needed at call sites if the property itself is updated):

  • GetResponseAsync: using Activity? activity = CurrentActivityIsInvokeAgent ? null : _activitySource?.StartActivity(...)
  • GetStreamingResponseAsync: same pattern
  • InstrumentedInvokeFunctionAsync: Activity? invokeAgentActivity = CurrentActivityIsInvokeAgent ? Activity.Current : null;

3. test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/FunctionInvokingChatClientTests.cs

Add test cases mirroring existing invoke_agent tests:

  • DoesNotCreateOrchestrateToolsSpanWhenInvokeWorkflowIsParent — with [InlineData("invoke_workflow")], [InlineData("invoke_workflow my_workflow")], [InlineData("invoke_workflow ")]
  • StreamingPreservesTraceContextWhenInvokeWorkflowWithNameIsParent
  • UsesWorkflowActivitySourceWhenInvokeWorkflowIsParent
  • SensitiveDataPropagatesFromWorkflowActivityWhenInvokeWorkflowIsParent
  • Update CreatesOrchestrateToolsSpanWhenParentIsNotInvokeAgent to also test invoke_workflow-like non-matches: "invoke_workflo", "invoke_workflow_extra", "invoke_workflowx"

The following is the prior conversation context from the user's chat exploration (may be truncated):

User: Anything we need to do to support @open-telemetry/semantic-conventions/pull/3249 in Microsoft.Extensions.AI?

Assistant:

Let me investigate the PR and the current codebase to understand what changes might be needed.

Assistant: Now let me look more closely at the key files to understand how invoke_agent is currently handled and whether invoke_workflow needs similar support:

Assistant: Now let me also check how the CurrentActivityIsInvokeAgent property is used more broadly in the orchestrate_tools span creation logic:

Assistant: Now let me also check how the non-streaming path uses this same pattern:

Assistant: Now let me also check the non-streaming GetResponseAsync for the same pattern:

Assistant: Now let me also check the non-streaming path for the orchestrate_tools span:

Assistant: Now let me find the specific usage in GetResponseAsync (non-streaming) too:

Assistant: Now I have a comprehensive understanding of the situation. Let me summarize my findings.


Analysis: Impact of open-telemetry/semantic-conventions#3249 on Microsoft.Extensions.AI

The merged PR adds two things to the OpenTelemetry GenAI semantic conventions:

  1. A new invoke_workflow operation name — a new well-known value for gen_ai.operation.name
  2. A new gen_ai.workflow.name attribute — a human-readable name for the workflow

What currently exists in dotnet/extensions

There are two key areas in Microsoft.Extensions.AI that interact with the invoke_agent operation name and would need to be aware of invoke_workflow:

1. OpenTelemetryConsts.GenAI (OpenTelemetryConsts.cs)

</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

*This pull request was created from Copilot chat.*
>

<!-- START COPILOT CODING AGENT TIPS -->
---Let Copilot coding agent [set things up for you](https://github.com/dotnet/extensions/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo.

 ###### Microsoft Reviewers: [Open in CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/dotnet/extensions/pull/7382)

Copilot AI changed the title [WIP] Update FunctionInvokingChatClient for invoke_workflow span Support invoke_workflow as an equivalent parent span to invoke_agent in FunctionInvokingChatClient Mar 10, 2026
@github-actions github-actions bot added the area-ai Microsoft.Extensions.AI libraries label Mar 10, 2026
@stephentoub stephentoub marked this pull request as ready for review March 10, 2026 18:05
@stephentoub stephentoub requested a review from a team as a code owner March 10, 2026 18:05
Copilot AI review requested due to automatic review settings March 10, 2026 18:05
@stephentoub stephentoub enabled auto-merge (squash) March 10, 2026 18:05
@stephentoub stephentoub requested a review from tarekgh March 10, 2026 18:05
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

Updates Microsoft.Extensions.AI OpenTelemetry behavior so invoke_workflow parent spans are treated equivalently to invoke_agent in FunctionInvokingChatClient, aligning with newer GenAI semantic conventions.

Changes:

  • Added invoke_workflow operation name constant in OpenTelemetry constants.
  • Generalized parent-span detection logic to match either invoke_agent or invoke_workflow (including “name suffix” forms like "invoke_workflow MyWorkflow").
  • Added/extended unit tests to cover invoke_workflow parity (span suppression, trace propagation, ActivitySource reuse, sensitive-data propagation, and near-miss cases).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/Libraries/Microsoft.Extensions.AI/OpenTelemetryConsts.cs Adds InvokeWorkflowName constant for GenAI operation name.
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs Treats invoke_workflow as an equivalent grouping parent to invoke_agent via a shared display-name matcher helper.
test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/FunctionInvokingChatClientTests.cs Adds comprehensive parity tests for invoke_workflow and extends non-match coverage.

auto-merge was automatically disabled March 10, 2026 18:10

Head branch was pushed to by a user without write access

tarekgh and others added 2 commits March 11, 2026 13:39
# Conflicts:
#	src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs
Copy link
Member

@tarekgh tarekgh left a comment

Choose a reason for hiding this comment

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

@stephentoub I have resolved the merge conflict with the Realtime stuff that merged this morning. Thanks!

@tarekgh
Copy link
Member

tarekgh commented Mar 11, 2026

@copilot please fix the error:

##[error]src\Libraries\Microsoft.Extensions.AI\ChatCompletion\FunctionInvokingChatClient.cs(1279,1): error IDE0055: (NETCORE_ENGINEERING_TELEMETRY=Build) Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Copy link
Contributor Author

Copilot AI commented Mar 11, 2026

@copilot please fix the error:

##[error]src\Libraries\Microsoft.Extensions.AI\ChatCompletion\FunctionInvokingChatClient.cs(1279,1): error IDE0055: (NETCORE_ENGINEERING_TELEMETRY=Build) Fix formatti...

Fixed in 6e50b0f — removed the stray space character on the blank line at line 1279.

@stephentoub stephentoub merged commit a4ea533 into main Mar 12, 2026
6 checks passed
@stephentoub stephentoub deleted the copilot/update-function-invoking-chat-client-again branch March 12, 2026 02:38
jeffhandley pushed a commit to jeffhandley/extensions that referenced this pull request Mar 17, 2026
…nt` in FunctionInvokingChatClient (dotnet#7382)

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: stephentoub <[email protected]>
Co-authored-by: Tarek Mahmoud Sayed <[email protected]>
Co-authored-by: Tarek Mahmoud Sayed <[email protected]>
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.

5 participants