-
Notifications
You must be signed in to change notification settings - Fork 941
fix(langchain): Use instance-level wrapping for AgentMiddleware hooks #4000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MaxStenklyft
wants to merge
3
commits into
traceloop:main
Choose a base branch
from
MaxStenklyft:fix/middleware-hook-wrapping-identity-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
102 changes: 102 additions & 0 deletions
102
packages/opentelemetry-instrumentation-langchain/tests/test_middleware_identity.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| """Test that middleware hook instrumentation preserves base class identity checks. | ||
|
|
||
| LangGraph's create_agent uses identity checks like: | ||
| m.__class__.before_agent is not AgentMiddleware.before_agent | ||
| to decide whether a middleware overrides a hook. Class-level wrapping with wrapt | ||
| breaks this by replacing base class methods with FunctionWrapper descriptors. | ||
| """ | ||
|
|
||
| import pytest | ||
| from langchain.agents.middleware.types import AgentMiddleware | ||
| from opentelemetry.instrumentation.langchain import LangchainInstrumentor | ||
| from opentelemetry.sdk.trace import TracerProvider | ||
| from opentelemetry.sdk.trace.export import SimpleSpanProcessor | ||
| from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter | ||
|
|
||
|
|
||
| ALL_HOOKS = [ | ||
| "before_model", "after_model", "before_agent", "after_agent", | ||
| "abefore_model", "aafter_model", "abefore_agent", "aafter_agent", | ||
| ] | ||
|
|
||
|
|
||
| class MyMiddleware(AgentMiddleware): | ||
| """Subclass that only overrides before_agent.""" | ||
|
|
||
| def before_agent(self, state, runtime): | ||
| return {"custom": True} | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def _instrument(): | ||
| exporter = InMemorySpanExporter() | ||
| provider = TracerProvider() | ||
| provider.add_span_processor(SimpleSpanProcessor(exporter)) | ||
|
|
||
| instrumentor = LangchainInstrumentor() | ||
| instrumentor.instrument(tracer_provider=provider) | ||
| yield instrumentor, exporter | ||
| instrumentor.uninstrument() | ||
|
|
||
|
|
||
| def test_base_class_identity_preserved_after_instrumentation(_instrument): | ||
| """Base class methods must remain identical via `is` after instrumentation. | ||
|
|
||
| This is the exact check LangGraph's factory.py uses to decide whether | ||
| to add graph nodes for middleware hooks. | ||
| """ | ||
| for hook_name in ALL_HOOKS: | ||
| base_method = getattr(AgentMiddleware, hook_name) | ||
| sub_method = getattr(MyMiddleware, hook_name) | ||
| if hook_name == "before_agent": | ||
| # MyMiddleware overrides this — should NOT be identical | ||
| assert sub_method is not base_method, ( | ||
| f"MyMiddleware.{hook_name} should differ from base" | ||
| ) | ||
| else: | ||
| # Not overridden — must be identical | ||
| assert sub_method is base_method, ( | ||
| f"MyMiddleware.{hook_name} should be identical to " | ||
| f"AgentMiddleware.{hook_name} but is not — " | ||
| f"class-level wrapping likely broke identity" | ||
| ) | ||
|
|
||
|
|
||
| def test_instance_hooks_are_instrumented(_instrument): | ||
| """Instance-level hooks should be wrapped for tracing after construction.""" | ||
| m = MyMiddleware() | ||
| for hook_name in ALL_HOOKS: | ||
| assert hook_name in m.__dict__, ( | ||
| f"{hook_name} should be in instance __dict__ (instrumented)" | ||
| ) | ||
|
|
||
|
|
||
| def test_uninstrument_removes_instance_patches(_instrument): | ||
| """After uninstrument(), pre-existing instances must stop emitting spans.""" | ||
| instrumentor, exporter = _instrument | ||
|
|
||
| m = MyMiddleware() | ||
| # Verify hooks are patched | ||
| assert "before_model" in m.__dict__, "Hook should be in instance __dict__" | ||
|
|
||
| # Call a hook — should produce a span | ||
| m.before_model({}, None) | ||
| spans_before = exporter.get_finished_spans() | ||
| assert len(spans_before) == 1 | ||
|
|
||
| exporter.clear() | ||
|
|
||
| # Uninstrument — should clean up instance patches | ||
| instrumentor.uninstrument() | ||
|
|
||
| # Instance __dict__ should no longer shadow the hooks | ||
| assert "before_model" not in m.__dict__, ( | ||
| "Hook should be removed from instance __dict__ after uninstrument" | ||
| ) | ||
|
|
||
| # Calling the hook now goes to the unpatched class method — no span | ||
| m.before_model({}, None) | ||
| spans_after = exporter.get_finished_spans() | ||
| assert len(spans_after) == 0, ( | ||
| "No spans should be emitted after uninstrument()" | ||
| ) |
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.
Uh oh!
There was an error while loading. Please reload this page.