feat(agentscope): support AgentScope v2#208
Open
sipercai wants to merge 2 commits into
Open
Conversation
ralf0131
approved these changes
Jun 17, 2026
ralf0131
left a comment
Collaborator
There was a problem hiding this comment.
Review by github-manager-bot
Summary
Makes the AgentScope instrumentor version-aware: detects agentscope major version via importlib.metadata, preserves the v1 monkey-patch path for agentscope >= 1.0.0, < 2.0.0, and adds v2 middleware-based instrumentation for agentscope >= 2.0.0. The v2 approach hooks into AgentScope's own middleware system (before_agent_reply, after_model_invoked, before_tool_execution, etc.) instead of monkey-patching.
Findings
- [Info]
__init__.py:145-165—_instrument_v1correctly uses lazy imports (from ._wrapper import ...) inside the method, avoiding import errors when only v2 is installed. Thedel kwargs+self._agentscope_majortracking in_uninstrumentensures consistent teardown. - [Info]
_v2_middleware.py:54-79—append_loongsuite_middlewareand_append_oncecorrectly handle both kwargs and positional args for themiddlewaresparameter. The_append_onceguard prevents duplicate LoongSuite middleware instances ifAgent.__init__is called multiple times. - [Info]
_v2_middleware.py— The middleware class covers all telemetry signals: agent invocation (ENTRY/AGENT), LLM calls (LLM), tool execution (TOOL), and ReAct steps (STEP). The streaming first-token detection via_FIRST_TOKEN_EVENT_TYPESis a good approach. - [Warning]
_v2_middleware.py:118— TheAgentScopeV2Middlewareconstructor takeshandleras aCallable[[], ExtendedTelemetryHandler]. This is a factory pattern to avoid capturing a stale handler reference. However, in_instrument_v2(line 360), the handler islambda: self._handlerwhich capturesself(the instrumentor instance). If_uninstrumentis called and then_instrumentis called again, the lambda will return the newself._handler. This is correct but worth documenting to prevent confusion. - [Info]
pyproject.toml— Addingpackaging >= 18.0as a runtime dependency for version parsing, and bumpingvcrpy >= 8.1.1for v2 test cassettes. Theagentscopeversion constraint< 3.0.0is forward-looking. - [Info] Tests — VCR-recorded cassettes for non-streaming, streaming, and concurrent e2e tests. Good coverage.
LGTM. Clean, well-architected version-aware instrumentation with excellent test coverage.
Automated review by github-manager-bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #207
This PR makes the AgentScope instrumentation version-aware and adds AgentScope v2 support while preserving the existing AgentScope v1 path.
The v1 monkey-patch implementation is kept behind lazy imports and continues to run for
agentscope >= 1.0.0, < 2.0.0. AgentScope v2 uses the framework's Agent middleware hooks to emit LoongSuite GenAI telemetry for agent replies, model calls, and tool execution throughopentelemetry.util.genaihelpers.It also adds a dedicated v2 tox environment with VCR-recorded e2e coverage for non-streaming, streaming, and concurrent Agent calls.
Type of change
How Has This Been Tested?
tox -e precommituvx tox -c tox-loongsuite.ini -e py311-test-loongsuite-instrumentation-agentscopev2uvx tox -c tox-loongsuite.ini -e py311-test-loongsuite-instrumentation-agentscope-latestuvx tox -c tox-loongsuite.ini -e lint-loongsuite-instrumentation-agentscopeDoes This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.