Skip to content

Commit ce10982

Browse files
Fix test_langchain_agent_name_propagation placement
- Correctly place the new test after test_langchain_agent - Remove misplaced assertions that broke the original test
1 parent 80c772d commit ce10982

1 file changed

Lines changed: 40 additions & 86 deletions

File tree

tests/integrations/langchain/test_langchain.py

Lines changed: 40 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -259,92 +259,6 @@ def test_langchain_agent(
259259
},
260260
] == json.loads(chat_spans[0]["data"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS])
261261

262-
263-
def test_langchain_agent_name_propagation(sentry_init, capture_events, monkeypatch):
264-
monkeypatch.setattr(
265-
"sentry_sdk.integrations.langchain._get_request_data",
266-
lambda obj, args, kwargs: ("test_agent_name", None),
267-
)
268-
269-
sentry_init(
270-
integrations=[LangchainIntegration(include_prompts=True)],
271-
traces_sample_rate=1.0,
272-
send_default_pii=True,
273-
)
274-
events = capture_events()
275-
276-
global llm_type, stream_result_mock
277-
llm_type = "openai-chat"
278-
stream_result_mock = Mock(
279-
side_effect=[
280-
[
281-
ChatGenerationChunk(
282-
type="ChatGenerationChunk",
283-
message=AIMessageChunk(
284-
content="",
285-
additional_kwargs={
286-
"tool_calls": [
287-
{
288-
"index": 0,
289-
"id": "call_BbeyNhCKa6kYLYzrD40NGm3b",
290-
"function": {"arguments": "", "name": "get_word_length"},
291-
"type": "function",
292-
}
293-
]
294-
},
295-
),
296-
),
297-
ChatGenerationChunk(
298-
type="ChatGenerationChunk",
299-
message=AIMessageChunk(
300-
content="5",
301-
usage_metadata={
302-
"input_tokens": 142,
303-
"output_tokens": 50,
304-
"total_tokens": 192,
305-
"input_token_details": {"audio": 0, "cache_read": 0},
306-
"output_token_details": {"audio": 0, "reasoning": 0},
307-
},
308-
),
309-
generation_info={"finish_reason": "function_call"},
310-
),
311-
],
312-
[
313-
ChatGenerationChunk(
314-
text="The word eudca has 5 letters.",
315-
type="ChatGenerationChunk",
316-
message=AIMessageChunk(
317-
content="The word eudca has 5 letters.",
318-
usage_metadata={
319-
"input_tokens": 89,
320-
"output_tokens": 28,
321-
"total_tokens": 117,
322-
"input_token_details": {"audio": 0, "cache_read": 0},
323-
"output_token_details": {"audio": 0, "reasoning": 0},
324-
},
325-
),
326-
),
327-
ChatGenerationChunk(
328-
type="ChatGenerationChunk",
329-
generation_info={"finish_reason": "stop"},
330-
message=AIMessageChunk(content=""),
331-
),
332-
],
333-
]
334-
)
335-
336-
llm = MockOpenAI(model_name="gpt-3.5-turbo", temperature=0, openai_api_key="badkey")
337-
agent = create_openai_tools_agent(llm, [get_word_length], prompt=None)
338-
agent_executor = AgentExecutor(agent=agent, tools=[get_word_length], verbose=True)
339-
340-
with start_transaction():
341-
list(agent_executor.stream({"input": "How many letters in the word eudca"}))
342-
343-
tx = events[0]
344-
assert tx["type"] == "transaction"
345-
for span in tx["spans"]:
346-
assert span["data"].get(SPANDATA.GEN_AI_AGENT_NAME) == "test_agent_name"
347-
348262
assert "5" in chat_spans[1]["data"][SPANDATA.GEN_AI_RESPONSE_TEXT]
349263

350264
# Verify tool calls are recorded when PII is enabled
@@ -399,6 +313,46 @@ def test_langchain_agent_name_propagation(sentry_init, capture_events, monkeypat
399313
)
400314

401315

316+
def test_langchain_agent_name_propagation(sentry_init, capture_events, monkeypatch):
317+
sentry_init(
318+
integrations=[LangchainIntegration(include_prompts=True)],
319+
traces_sample_rate=1.0,
320+
send_default_pii=True,
321+
)
322+
events = capture_events()
323+
324+
prompt = ChatPromptTemplate.from_messages(
325+
[
326+
(
327+
"system",
328+
"You are very powerful assistant, but don't know current events",
329+
),
330+
("user", "{input}"),
331+
MessagesPlaceholder(variable_name="agent_scratchpad"),
332+
]
333+
)
334+
335+
llm = MockOpenAI(model_name="gpt-3.5-turbo", temperature=0, openai_api_key="badkey")
336+
agent = create_openai_tools_agent(llm, [get_word_length], prompt)
337+
agent_executor = AgentExecutor(agent=agent, tools=[get_word_length], verbose=True)
338+
339+
# Mock _get_request_data to return a test agent name
340+
def mock_get_request_data(self, args, kwargs):
341+
return "test_agent_name", [get_word_length]
342+
343+
monkeypatch.setattr(
344+
"sentry_sdk.integrations.langchain._get_request_data", mock_get_request_data
345+
)
346+
347+
with start_transaction():
348+
list(agent_executor.stream({"input": "How many letters in the word eudca"}))
349+
350+
tx = events[0]
351+
assert tx["type"] == "transaction"
352+
for span in tx["spans"]:
353+
assert span["data"].get(SPANDATA.GEN_AI_AGENT_NAME) == "test_agent_name"
354+
355+
402356
def test_langchain_error(sentry_init, capture_events):
403357
sentry_init(
404358
integrations=[LangchainIntegration(include_prompts=True)],

0 commit comments

Comments
 (0)