Skip to content

Commit 9ce1ae5

Browse files
ref(langchain): Revert input truncation
1 parent af25aff commit 9ce1ae5

2 files changed

Lines changed: 20 additions & 119 deletions

File tree

sentry_sdk/integrations/langchain.py

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
get_start_span_function,
1515
normalize_message_roles,
1616
set_data_normalized,
17-
truncate_and_annotate_messages,
1817
)
1918
from sentry_sdk.consts import OP, SPANDATA
2019
from sentry_sdk.integrations import DidNotEnable, Integration
@@ -377,17 +376,12 @@ def on_llm_start(
377376
}
378377
for prompt in prompts
379378
]
380-
scope = sentry_sdk.get_current_scope()
381-
messages_data = truncate_and_annotate_messages(
382-
normalized_messages, span, scope
379+
set_data_normalized(
380+
span,
381+
SPANDATA.GEN_AI_REQUEST_MESSAGES,
382+
normalized_messages,
383+
unpack=False,
383384
)
384-
if messages_data is not None:
385-
set_data_normalized(
386-
span,
387-
SPANDATA.GEN_AI_REQUEST_MESSAGES,
388-
messages_data,
389-
unpack=False,
390-
)
391385

392386
def on_chat_model_start(
393387
self: "SentryLangchainCallback",
@@ -457,17 +451,12 @@ def on_chat_model_start(
457451
self._normalize_langchain_message(message)
458452
)
459453
normalized_messages = normalize_message_roles(normalized_messages)
460-
scope = sentry_sdk.get_current_scope()
461-
messages_data = truncate_and_annotate_messages(
462-
normalized_messages, span, scope
454+
set_data_normalized(
455+
span,
456+
SPANDATA.GEN_AI_REQUEST_MESSAGES,
457+
normalized_messages,
458+
unpack=False,
463459
)
464-
if messages_data is not None:
465-
set_data_normalized(
466-
span,
467-
SPANDATA.GEN_AI_REQUEST_MESSAGES,
468-
messages_data,
469-
unpack=False,
470-
)
471460

472461
def on_chat_model_end(
473462
self: "SentryLangchainCallback",
@@ -979,17 +968,12 @@ def new_invoke(self: "Any", *args: "Any", **kwargs: "Any") -> "Any":
979968
and integration.include_prompts
980969
):
981970
normalized_messages = normalize_message_roles([input])
982-
scope = sentry_sdk.get_current_scope()
983-
messages_data = truncate_and_annotate_messages(
984-
normalized_messages, span, scope
971+
set_data_normalized(
972+
span,
973+
SPANDATA.GEN_AI_REQUEST_MESSAGES,
974+
normalized_messages,
975+
unpack=False,
985976
)
986-
if messages_data is not None:
987-
set_data_normalized(
988-
span,
989-
SPANDATA.GEN_AI_REQUEST_MESSAGES,
990-
messages_data,
991-
unpack=False,
992-
)
993977

994978
output = result.get("output")
995979
if (
@@ -1041,17 +1025,12 @@ def new_stream(self: "Any", *args: "Any", **kwargs: "Any") -> "Any":
10411025
and integration.include_prompts
10421026
):
10431027
normalized_messages = normalize_message_roles([input])
1044-
scope = sentry_sdk.get_current_scope()
1045-
messages_data = truncate_and_annotate_messages(
1046-
normalized_messages, span, scope
1028+
set_data_normalized(
1029+
span,
1030+
SPANDATA.GEN_AI_REQUEST_MESSAGES,
1031+
normalized_messages,
1032+
unpack=False,
10471033
)
1048-
if messages_data is not None:
1049-
set_data_normalized(
1050-
span,
1051-
SPANDATA.GEN_AI_REQUEST_MESSAGES,
1052-
messages_data,
1053-
unpack=False,
1054-
)
10551034

10561035
# Run the agent
10571036
result = f(self, *args, **kwargs)

tests/integrations/langchain/test_langchain.py

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,84 +1290,6 @@ def test_langchain_message_role_normalization_units():
12901290
assert normalized[5] == "string message" # String message unchanged
12911291

12921292

1293-
def test_langchain_message_truncation(sentry_init, capture_items):
1294-
"""Test that large messages are truncated properly in Langchain integration."""
1295-
from langchain_core.outputs import LLMResult, Generation
1296-
1297-
sentry_init(
1298-
integrations=[LangchainIntegration(include_prompts=True)],
1299-
traces_sample_rate=1.0,
1300-
send_default_pii=True,
1301-
)
1302-
items = capture_items("transaction", "span")
1303-
1304-
callback = SentryLangchainCallback(max_span_map_size=100, include_prompts=True)
1305-
1306-
run_id = "12345678-1234-1234-1234-123456789012"
1307-
serialized = {"_type": "openai-chat", "model_name": "gpt-3.5-turbo"}
1308-
1309-
large_content = (
1310-
"This is a very long message that will exceed our size limits. " * 1000
1311-
)
1312-
prompts = [
1313-
"small message 1",
1314-
large_content,
1315-
large_content,
1316-
"small message 4",
1317-
"small message 5",
1318-
]
1319-
1320-
with start_transaction():
1321-
callback.on_llm_start(
1322-
serialized=serialized,
1323-
prompts=prompts,
1324-
run_id=run_id,
1325-
name="my_pipeline",
1326-
invocation_params={
1327-
"temperature": 0.7,
1328-
"max_tokens": 100,
1329-
"model": "gpt-3.5-turbo",
1330-
},
1331-
)
1332-
1333-
response = LLMResult(
1334-
generations=[[Generation(text="The response")]],
1335-
llm_output={
1336-
"token_usage": {
1337-
"total_tokens": 25,
1338-
"prompt_tokens": 10,
1339-
"completion_tokens": 15,
1340-
}
1341-
},
1342-
)
1343-
callback.on_llm_end(response=response, run_id=run_id)
1344-
1345-
tx = next(item.payload for item in items if item.type == "transaction")
1346-
assert tx["type"] == "transaction"
1347-
1348-
spans = [item.payload for item in items if item.type == "span"]
1349-
llm_spans = [
1350-
span
1351-
for span in spans
1352-
if span["attributes"].get("sentry.op") == "gen_ai.text_completion"
1353-
]
1354-
assert len(llm_spans) > 0
1355-
1356-
llm_span = llm_spans[0]
1357-
assert llm_span["attributes"]["gen_ai.operation.name"] == "text_completion"
1358-
assert llm_span["attributes"][SPANDATA.GEN_AI_PIPELINE_NAME] == "my_pipeline"
1359-
1360-
assert SPANDATA.GEN_AI_REQUEST_MESSAGES in llm_span["attributes"]
1361-
messages_data = llm_span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
1362-
assert isinstance(messages_data, str)
1363-
1364-
parsed_messages = json.loads(messages_data)
1365-
assert isinstance(parsed_messages, list)
1366-
assert len(parsed_messages) == 1
1367-
assert "small message 5" in str(parsed_messages[0])
1368-
assert tx["_meta"]["spans"]["0"]["data"]["gen_ai.request.messages"][""]["len"] == 5
1369-
1370-
13711293
@pytest.mark.parametrize(
13721294
"send_default_pii, include_prompts",
13731295
[

0 commit comments

Comments
 (0)