test: Add failing test for BaseCallbackManager.merge (#32040)

This pull request introduces a failing unit test to reproduce the bug
reported in issue #32028.
The test asserts the expected behavior: `BaseCallbackManager.merge()`
should combine `handlers` and `inheritable_handlers` independently,
without mixing them. This test will fail on the current codebase and is
intended to guide the fix and prevent future regressions.

---------

Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
Co-authored-by: Mason Daugherty <mason@langchain.dev>
This commit is contained in:
Shibayan003
2025-09-11 01:56:18 +08:00
committed by GitHub
parent 450870c9ac
commit f08dfb6f49
2 changed files with 41 additions and 25 deletions

View File

@@ -1,3 +1,5 @@
import pytest
from langchain_core.callbacks.base import BaseCallbackHandler, BaseCallbackManager
@@ -13,3 +15,29 @@ def test_remove_handler() -> None:
manager = BaseCallbackManager([handler1], inheritable_handlers=[handler2])
manager.remove_handler(handler1)
manager.remove_handler(handler2)
@pytest.mark.xfail(
reason="TODO: #32028 merge() incorrectly mixes handlers and inheritable_handlers"
)
def test_merge_preserves_handler_distinction() -> None:
"""Test that merging managers preserves the distinction between handlers.
This test verifies the correct behavior of the BaseCallbackManager.merge()
method. When two managers are merged, their handlers and
inheritable_handlers should be combined independently.
Currently, it is expected to xfail until the issue is resolved.
"""
h1 = BaseCallbackHandler()
h2 = BaseCallbackHandler()
ih1 = BaseCallbackHandler()
ih2 = BaseCallbackHandler()
m1 = BaseCallbackManager(handlers=[h1], inheritable_handlers=[ih1])
m2 = BaseCallbackManager(handlers=[h2], inheritable_handlers=[ih2])
merged = m1.merge(m2)
assert set(merged.handlers) == {h1, h2}
assert set(merged.inheritable_handlers) == {ih1, ih2}