Skip to content

Commit 7804260

Browse files
fix(langchain): Make span_map an instance variable (#4476)
`span_map` should be an instance variable; otherwise, separate instances of the `SentryLangchainCallback` share the same `span_map` object, which is clearly not intended here. Also, remove the `max_span_map_size` class variable, it is always set on the instance, and so not needed. Ref #4443 Co-authored-by: Cursor Agent <[email protected]>
1 parent 0a2d858 commit 7804260

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

sentry_sdk/integrations/langchain.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,9 @@ def __init__(self, span):
8989
class SentryLangchainCallback(BaseCallbackHandler): # type: ignore[misc]
9090
"""Base callback handler that can be used to handle callbacks from langchain."""
9191

92-
span_map = OrderedDict() # type: OrderedDict[UUID, WatchedSpan]
93-
94-
max_span_map_size = 0
95-
9692
def __init__(self, max_span_map_size, include_prompts, tiktoken_encoding_name=None):
9793
# type: (int, bool, Optional[str]) -> None
94+
self.span_map = OrderedDict() # type: OrderedDict[UUID, WatchedSpan]
9895
self.max_span_map_size = max_span_map_size
9996
self.include_prompts = include_prompts
10097

tests/integrations/langchain/test_langchain.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,15 @@ def _identifying_params(self):
416416

417417
# Verify the callback ID matches our manual callback
418418
assert id(manual_callback) in tracked_callback_instances
419+
420+
421+
def test_span_map_is_instance_variable():
422+
"""Test that each SentryLangchainCallback instance has its own span_map."""
423+
# Create two separate callback instances
424+
callback1 = SentryLangchainCallback(max_span_map_size=100, include_prompts=True)
425+
callback2 = SentryLangchainCallback(max_span_map_size=100, include_prompts=True)
426+
427+
# Verify they have different span_map instances
428+
assert (
429+
callback1.span_map is not callback2.span_map
430+
), "span_map should be an instance variable, not shared between instances"

0 commit comments

Comments
 (0)