Skip to content

Commit 9903b26

Browse files
test(langchain): Consolidate available tools assertion (#5721)
1 parent 9d1d748 commit 9903b26

File tree

1 file changed

+5
-79
lines changed

1 file changed

+5
-79
lines changed

tests/integrations/langchain/test_langchain.py

Lines changed: 5 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,11 @@ def test_langchain_agent(
305305

306306
# Verify that available tools are always recorded regardless of PII settings
307307
for chat_span in chat_spans:
308-
span_data = chat_span.get("data", {})
309-
if SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS in span_data:
310-
tools_data = span_data[SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS]
311-
assert tools_data is not None, (
312-
"Available tools should always be recorded regardless of PII settings"
313-
)
308+
tools_data = chat_span["data"][SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS]
309+
assert tools_data is not None, (
310+
"Available tools should always be recorded regardless of PII settings"
311+
)
312+
assert "get_word_length" in tools_data
314313

315314

316315
def test_langchain_error(sentry_init, capture_events):
@@ -718,79 +717,6 @@ def test_langchain_callback_list_existing_callback(sentry_init):
718717
assert handler is sentry_callback
719718

720719

721-
def test_tools_integration_in_spans(sentry_init, capture_events):
722-
"""Test that tools are properly set on spans in actual LangChain integration."""
723-
global llm_type
724-
llm_type = "openai-chat"
725-
726-
sentry_init(
727-
integrations=[LangchainIntegration(include_prompts=False)],
728-
traces_sample_rate=1.0,
729-
)
730-
events = capture_events()
731-
732-
prompt = ChatPromptTemplate.from_messages(
733-
[
734-
("system", "You are a helpful assistant"),
735-
("user", "{input}"),
736-
MessagesPlaceholder(variable_name="agent_scratchpad"),
737-
]
738-
)
739-
740-
global stream_result_mock
741-
stream_result_mock = Mock(
742-
side_effect=[
743-
[
744-
ChatGenerationChunk(
745-
type="ChatGenerationChunk",
746-
message=AIMessageChunk(content="Simple response"),
747-
),
748-
]
749-
]
750-
)
751-
752-
llm = MockOpenAI(
753-
model_name="gpt-3.5-turbo",
754-
temperature=0,
755-
openai_api_key="badkey",
756-
)
757-
agent = create_openai_tools_agent(llm, [get_word_length], prompt)
758-
agent_executor = AgentExecutor(agent=agent, tools=[get_word_length], verbose=True)
759-
760-
with start_transaction():
761-
list(agent_executor.stream({"input": "Hello"}))
762-
763-
# Check that events were captured and contain tools data
764-
if events:
765-
tx = events[0]
766-
spans = tx.get("spans", [])
767-
768-
# Look for spans that should have tools data
769-
tools_found = False
770-
for span in spans:
771-
span_data = span.get("data", {})
772-
if SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS in span_data:
773-
tools_found = True
774-
tools_data = span_data[SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS]
775-
# Verify tools are in the expected format
776-
assert isinstance(tools_data, (str, list)) # Could be serialized
777-
if isinstance(tools_data, str):
778-
# If serialized as string, should contain tool name
779-
assert "get_word_length" in tools_data
780-
else:
781-
# If still a list, verify structure
782-
assert len(tools_data) >= 1
783-
names = [
784-
tool.get("name")
785-
for tool in tools_data
786-
if isinstance(tool, dict)
787-
]
788-
assert "get_word_length" in names
789-
790-
# Ensure we found at least one span with tools data
791-
assert tools_found, "No spans found with tools data"
792-
793-
794720
def test_langchain_integration_with_langchain_core_only(sentry_init, capture_events):
795721
"""Test that the langchain integration works when langchain.agents.AgentExecutor
796722
is not available or langchain is not installed, but langchain-core is.

0 commit comments

Comments
 (0)