Skip to content

fix(langchain): Make span_map an instance variable #4476

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

Merged
merged 1 commit into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions sentry_sdk/integrations/langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,9 @@ def __init__(self, span):
class SentryLangchainCallback(BaseCallbackHandler): # type: ignore[misc]
"""Base callback handler that can be used to handle callbacks from langchain."""

span_map = OrderedDict() # type: OrderedDict[UUID, WatchedSpan]

max_span_map_size = 0

def __init__(self, max_span_map_size, include_prompts, tiktoken_encoding_name=None):
# type: (int, bool, Optional[str]) -> None
self.span_map = OrderedDict() # type: OrderedDict[UUID, WatchedSpan]
self.max_span_map_size = max_span_map_size
self.include_prompts = include_prompts

Expand Down
12 changes: 12 additions & 0 deletions tests/integrations/langchain/test_langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,15 @@ def _identifying_params(self):

# Verify the callback ID matches our manual callback
assert id(manual_callback) in tracked_callback_instances


def test_span_map_is_instance_variable():
"""Test that each SentryLangchainCallback instance has its own span_map."""
# Create two separate callback instances
callback1 = SentryLangchainCallback(max_span_map_size=100, include_prompts=True)
callback2 = SentryLangchainCallback(max_span_map_size=100, include_prompts=True)

# Verify they have different span_map instances
assert (
callback1.span_map is not callback2.span_map
), "span_map should be an instance variable, not shared between instances"
Loading