Skip to content

Python: Add agent invocation spans#10255

Merged
moonbox3 merged 4 commits intomicrosoft:mainfrom
TaoChenOSU:taochen/python-agent-diagnostics
Jan 22, 2025
Merged

Python: Add agent invocation spans#10255
moonbox3 merged 4 commits intomicrosoft:mainfrom
TaoChenOSU:taochen/python-agent-diagnostics

Conversation

@TaoChenOSU
Copy link
Copy Markdown
Contributor

@TaoChenOSU TaoChenOSU commented Jan 22, 2025

Motivation and Context

Address feature request: #10174

Description

This pull request introduces a new decorator for tracing agent invocations and applies it to various methods in the ChatCompletionAgent and OpenAIAssistantAgent classes. Additionally, it includes new unit tests to ensure the decorator is correctly applied and functions as expected.

Key Changes:

Decorator Implementation:

  • Added a new trace_agent_invocation decorator in python/semantic_kernel/utils/telemetry/agent_diagnostics/decorators.py to trace agent invocations using OpenTelemetry.

Decorator Application:

  • Applied the trace_agent_invocation decorator to the invoke and invoke_stream methods in ChatCompletionAgent (python/semantic_kernel/agents/chat_completion/chat_completion_agent.py). [1] [2] [3]
  • Applied the trace_agent_invocation decorator to the invoke and invoke_stream methods in OpenAIAssistantAgent (python/semantic_kernel/agents/open_ai/open_ai_assistant_base.py). [1] [2] [3]

Contribution Checklist

@TaoChenOSU TaoChenOSU added python Pull requests for the Python Semantic Kernel agents labels Jan 22, 2025
@TaoChenOSU TaoChenOSU self-assigned this Jan 22, 2025
@TaoChenOSU TaoChenOSU requested a review from a team as a code owner January 22, 2025 01:20
@github-actions github-actions Bot changed the title Add agent invocation spans Python: Add agent invocation spans Jan 22, 2025
@markwallace-microsoft
Copy link
Copy Markdown
Contributor

markwallace-microsoft commented Jan 22, 2025

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
semantic_kernel/agents/open_ai
   open_ai_assistant_base.py483799%256, 342–343, 754, 922, 996, 1060
TOTAL16731177789% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
3010 4 💤 0 ❌ 0 🔥 1m 12s ⏱️

@TaoChenOSU TaoChenOSU enabled auto-merge January 22, 2025 01:26
Copy link
Copy Markdown
Contributor

@esttenorio esttenorio left a comment

Choose a reason for hiding this comment

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

should there be a readme or a demo mentioning/explaining the use of the new decorator?

Comment thread python/tests/unit/utils/agent_diagnostics/test_trace_open_ai_assistant_agent.py Outdated
@TaoChenOSU TaoChenOSU added this pull request to the merge queue Jan 22, 2025
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jan 22, 2025
@moonbox3 moonbox3 added this pull request to the merge queue Jan 22, 2025
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jan 22, 2025
@moonbox3 moonbox3 added this pull request to the merge queue Jan 22, 2025
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jan 22, 2025
@moonbox3 moonbox3 added this pull request to the merge queue Jan 22, 2025
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jan 22, 2025
@moonbox3 moonbox3 added this pull request to the merge queue Jan 22, 2025
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jan 22, 2025
@moonbox3 moonbox3 merged commit 6ecfdcf into microsoft:main Jan 22, 2025
jcruzmot-te pushed a commit to thousandeyes/aia-semantic-kernel that referenced this pull request Sep 15, 2025
### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->
Address feature request:
microsoft#10174

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
This pull request introduces a new decorator for tracing agent
invocations and applies it to various methods in the
`ChatCompletionAgent` and `OpenAIAssistantAgent` classes. Additionally,
it includes new unit tests to ensure the decorator is correctly applied
and functions as expected.

### Key Changes:

**Decorator Implementation:**
* Added a new `trace_agent_invocation` decorator in
`python/semantic_kernel/utils/telemetry/agent_diagnostics/decorators.py`
to trace agent invocations using OpenTelemetry.

**Decorator Application:**
* Applied the `trace_agent_invocation` decorator to the `invoke` and
`invoke_stream` methods in `ChatCompletionAgent`
(`python/semantic_kernel/agents/chat_completion/chat_completion_agent.py`).
[[1]](diffhunk://#diff-0f0a27c107368504c4347c88528b7b4234dcead1919005bcae13f5d16d6cf26dR19)
[[2]](diffhunk://#diff-0f0a27c107368504c4347c88528b7b4234dcead1919005bcae13f5d16d6cf26dR80)
[[3]](diffhunk://#diff-0f0a27c107368504c4347c88528b7b4234dcead1919005bcae13f5d16d6cf26dR134)
* Applied the `trace_agent_invocation` decorator to the `invoke` and
`invoke_stream` methods in `OpenAIAssistantAgent`
(`python/semantic_kernel/agents/open_ai/open_ai_assistant_base.py`).
[[1]](diffhunk://#diff-70e75c57136d3a90d045af4b8d59fb8e8101251d7c5126458bb5d9e6c36556a4R46)
[[2]](diffhunk://#diff-70e75c57136d3a90d045af4b8d59fb8e8101251d7c5126458bb5d9e6c36556a4R608)
[[3]](diffhunk://#diff-70e75c57136d3a90d045af4b8d59fb8e8101251d7c5126458bb5d9e6c36556a4R861)

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄

---------

Co-authored-by: Evan Mattson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents python Pull requests for the Python Semantic Kernel

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

New Feature: Agent Names captured in traces and logs to aggregate per agent stats

4 participants