From 637201fe23dd77d1fcbaefbaa709387edeffe408 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Fri, 20 Mar 2026 14:13:02 +0100 Subject: [PATCH 1/3] test(langchain): Consolidate available tools assertion --- .../integrations/langchain/test_langchain.py | 90 ++++--------------- 1 file changed, 17 insertions(+), 73 deletions(-) diff --git a/tests/integrations/langchain/test_langchain.py b/tests/integrations/langchain/test_langchain.py index 132da0a9a0..2c4db478ae 100644 --- a/tests/integrations/langchain/test_langchain.py +++ b/tests/integrations/langchain/test_langchain.py @@ -20,6 +20,7 @@ from langchain_core.runnables import RunnableConfig from langchain_core.language_models.chat_models import BaseChatModel + import sentry_sdk from sentry_sdk import start_transaction from sentry_sdk.integrations.langchain import ( @@ -304,14 +305,30 @@ def test_langchain_agent( assert chat_spans[1]["data"][SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == ["stop"] # Verify that available tools are always recorded regardless of PII settings + tools_found = False for chat_span in chat_spans: span_data = chat_span.get("data", {}) if SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS in span_data: + tools_found = True tools_data = span_data[SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS] assert tools_data is not None, ( "Available tools should always be recorded regardless of PII settings" ) + if isinstance(tools_data, str): + # If serialized as string, should contain tool name + assert "get_word_length" in tools_data + else: + # If still a list, verify structure + assert len(tools_data) >= 1 + names = [ + tool.get("name") for tool in tools_data if isinstance(tool, dict) + ] + assert "get_word_length" in names + + # Ensure we found at least one span with tools data + assert tools_found, "No spans found with tools data" + def test_langchain_error(sentry_init, capture_events): sentry_init( @@ -718,79 +735,6 @@ def test_langchain_callback_list_existing_callback(sentry_init): assert handler is sentry_callback -def test_tools_integration_in_spans(sentry_init, capture_events): - """Test that tools are properly set on spans in actual LangChain integration.""" - global llm_type - llm_type = "openai-chat" - - sentry_init( - integrations=[LangchainIntegration(include_prompts=False)], - traces_sample_rate=1.0, - ) - events = capture_events() - - prompt = ChatPromptTemplate.from_messages( - [ - ("system", "You are a helpful assistant"), - ("user", "{input}"), - MessagesPlaceholder(variable_name="agent_scratchpad"), - ] - ) - - global stream_result_mock - stream_result_mock = Mock( - side_effect=[ - [ - ChatGenerationChunk( - type="ChatGenerationChunk", - message=AIMessageChunk(content="Simple response"), - ), - ] - ] - ) - - llm = MockOpenAI( - model_name="gpt-3.5-turbo", - temperature=0, - openai_api_key="badkey", - ) - agent = create_openai_tools_agent(llm, [get_word_length], prompt) - agent_executor = AgentExecutor(agent=agent, tools=[get_word_length], verbose=True) - - with start_transaction(): - list(agent_executor.stream({"input": "Hello"})) - - # Check that events were captured and contain tools data - if events: - tx = events[0] - spans = tx.get("spans", []) - - # Look for spans that should have tools data - tools_found = False - for span in spans: - span_data = span.get("data", {}) - if SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS in span_data: - tools_found = True - tools_data = span_data[SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS] - # Verify tools are in the expected format - assert isinstance(tools_data, (str, list)) # Could be serialized - if isinstance(tools_data, str): - # If serialized as string, should contain tool name - assert "get_word_length" in tools_data - else: - # If still a list, verify structure - assert len(tools_data) >= 1 - names = [ - tool.get("name") - for tool in tools_data - if isinstance(tool, dict) - ] - assert "get_word_length" in names - - # Ensure we found at least one span with tools data - assert tools_found, "No spans found with tools data" - - def test_langchain_integration_with_langchain_core_only(sentry_init, capture_events): """Test that the langchain integration works when langchain.agents.AgentExecutor is not available or langchain is not installed, but langchain-core is. From d1d816088fb2456bab1cf7a34b5465b6d6e58351 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Fri, 20 Mar 2026 14:13:32 +0100 Subject: [PATCH 2/3] whitespace change --- tests/integrations/langchain/test_langchain.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/integrations/langchain/test_langchain.py b/tests/integrations/langchain/test_langchain.py index 2c4db478ae..3efb241d01 100644 --- a/tests/integrations/langchain/test_langchain.py +++ b/tests/integrations/langchain/test_langchain.py @@ -20,7 +20,6 @@ from langchain_core.runnables import RunnableConfig from langchain_core.language_models.chat_models import BaseChatModel - import sentry_sdk from sentry_sdk import start_transaction from sentry_sdk.integrations.langchain import ( From fccd72f883b8efc2cca35b7d10700b6dd5cc88d5 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Fri, 20 Mar 2026 14:39:13 +0100 Subject: [PATCH 3/3] simplify --- .../integrations/langchain/test_langchain.py | 24 ++----------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/tests/integrations/langchain/test_langchain.py b/tests/integrations/langchain/test_langchain.py index 3efb241d01..65e3073c5d 100644 --- a/tests/integrations/langchain/test_langchain.py +++ b/tests/integrations/langchain/test_langchain.py @@ -304,29 +304,9 @@ def test_langchain_agent( assert chat_spans[1]["data"][SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == ["stop"] # Verify that available tools are always recorded regardless of PII settings - tools_found = False for chat_span in chat_spans: - span_data = chat_span.get("data", {}) - if SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS in span_data: - tools_found = True - tools_data = span_data[SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS] - assert tools_data is not None, ( - "Available tools should always be recorded regardless of PII settings" - ) - - if isinstance(tools_data, str): - # If serialized as string, should contain tool name - assert "get_word_length" in tools_data - else: - # If still a list, verify structure - assert len(tools_data) >= 1 - names = [ - tool.get("name") for tool in tools_data if isinstance(tool, dict) - ] - assert "get_word_length" in names - - # Ensure we found at least one span with tools data - assert tools_found, "No spans found with tools data" + tools_data = chat_span["data"][SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS] + assert "get_word_length" in tools_data def test_langchain_error(sentry_init, capture_events):