Skip to content

Add openai.api.type telemetry attribute to OpenAI IChatClient implementations#7316

Merged
stephentoub merged 3 commits intomainfrom
copilot/implement-opentelemetry-tags
Feb 17, 2026
Merged

Add openai.api.type telemetry attribute to OpenAI IChatClient implementations#7316
stephentoub merged 3 commits intomainfrom
copilot/implement-opentelemetry-tags

Conversation

Copy link
Contributor

Copilot AI commented Feb 17, 2026

Implements the openai.api.type semantic convention from open-telemetry/semantic-conventions#3337. Each OpenAI IChatClient checks for an active Activity from OpenTelemetryChatClient and tags it with the API type.

  • OpenAIClientExtensions: Shared AddOpenAIApiType helper that checks Activity.Current for a "chat" span (matching OpenTelemetryChatClient's naming pattern) and adds the openai.api.type tag when IsAllDataRequested is true
  • OpenAIChatClient: Tags with chat_completions in both GetResponseAsync and GetStreamingResponseAsync
  • OpenAIResponsesChatClient: Tags with responses in both methods
  • Tests: Theory tests (streaming × non-streaming) for each client verifying the tag value via ActivityListener

The detection pattern follows the same convention as FunctionInvokingChatClient.CurrentActivityIsInvokeAgent—matching DisplayName starting with "chat" followed by EOS or space.

OpenAIAssistantsChatClient is not modified as the Assistants API is not one of the well-known enum values in the convention.


💡 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

@github-actions github-actions bot added the area-ai Microsoft.Extensions.AI libraries label Feb 17, 2026
Copilot AI and others added 2 commits February 17, 2026 21:02
Implements the OpenTelemetry semantic convention for distinguishing
OpenAI API types (open-telemetry/semantic-conventions#3337).

- OpenAIChatClient adds openai.api.type=chat_completions
- OpenAIResponsesChatClient adds openai.api.type=responses
- Both check Activity.Current for a matching "chat" span from OpenTelemetryChatClient
- Added tests for both streaming and non-streaming scenarios

Co-authored-by: stephentoub <[email protected]>
Copilot AI changed the title [WIP] Update OpenAI IChatClient implementations with OpenTelemetry tags Add openai.api.type telemetry attribute to OpenAI IChatClient implementations Feb 17, 2026
Copilot AI requested a review from stephentoub February 17, 2026 21:12
@stephentoub stephentoub marked this pull request as ready for review February 17, 2026 21:22
@stephentoub stephentoub requested a review from a team as a code owner February 17, 2026 21:22
Copilot AI review requested due to automatic review settings February 17, 2026 21:22
@stephentoub stephentoub enabled auto-merge (squash) February 17, 2026 21:23
@stephentoub stephentoub requested a review from tarekgh February 17, 2026 21:23
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

Implements the OpenTelemetry openai.api.type semantic attribute for OpenAI IChatClient implementations so that spans created by OpenTelemetryChatClient are annotated with the specific OpenAI API flavor being used.

Changes:

  • Added a shared OpenAIClientExtensions.AddOpenAIApiType helper to tag the current "chat" Activity with openai.api.type when appropriate.
  • Updated OpenAIChatClient and OpenAIResponsesChatClient to tag "chat" spans with chat_completions and responses respectively (streaming and non-streaming).
  • Added theory tests validating the tag is emitted for both streaming and non-streaming calls via an ActivityListener.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIClientExtensions.cs Adds constants + helper to set openai.api.type on the active "chat" span.
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIChatClient.cs Tags OpenTelemetry "chat" activity with chat_completions.
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIResponsesChatClient.cs Tags OpenTelemetry "chat" activity with responses.
test/Libraries/Microsoft.Extensions.AI.OpenAI.Tests/OpenAIChatClientTests.cs Adds streaming/non-streaming coverage asserting openai.api.type=chat_completions.
test/Libraries/Microsoft.Extensions.AI.OpenAI.Tests/OpenAIResponseClientTests.cs Adds streaming/non-streaming coverage asserting openai.api.type=responses.

@tarekgh tarekgh added this to the 11.0 milestone Feb 17, 2026
@stephentoub stephentoub merged commit 2cd073a into main Feb 17, 2026
12 checks passed
@stephentoub stephentoub deleted the copilot/implement-opentelemetry-tags branch February 17, 2026 22:25
@github-actions github-actions bot locked and limited conversation to collaborators Mar 20, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-ai Microsoft.Extensions.AI libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants