Skip to content

Commit 3286292

Browse files
committed
add to langchain
1 parent 16342b1 commit 3286292

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

sentry_sdk/ai/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class GEN_AI_ALLOWED_MESSAGE_ROLES:
1919

2020
GEN_AI_MESSAGE_ROLE_REVERSE_MAPPING = {
2121
GEN_AI_ALLOWED_MESSAGE_ROLES.SYSTEM: ["system"],
22-
GEN_AI_ALLOWED_MESSAGE_ROLES.USER: ["user"],
22+
GEN_AI_ALLOWED_MESSAGE_ROLES.USER: ["user", "human"],
2323
GEN_AI_ALLOWED_MESSAGE_ROLES.ASSISTANT: ["assistant", "ai"],
2424
GEN_AI_ALLOWED_MESSAGE_ROLES.TOOL_CALL: ["tool", "tool_call"],
2525
}

sentry_sdk/integrations/langchain.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
import sentry_sdk
66
from sentry_sdk.ai.monitoring import set_ai_pipeline_name
7-
from sentry_sdk.ai.utils import set_data_normalized, get_start_span_function
7+
from sentry_sdk.ai.utils import (
8+
normalize_message_roles,
9+
set_data_normalized,
10+
get_start_span_function,
11+
)
812
from sentry_sdk.consts import OP, SPANDATA
913
from sentry_sdk.integrations import DidNotEnable, Integration
1014
from sentry_sdk.scope import should_send_default_pii
@@ -209,8 +213,12 @@ def on_llm_start(
209213
_set_tools_on_span(span, all_params.get("tools"))
210214

211215
if should_send_default_pii() and self.include_prompts:
216+
normalized_messages = normalize_message_roles(prompts)
212217
set_data_normalized(
213-
span, SPANDATA.GEN_AI_REQUEST_MESSAGES, prompts, unpack=False
218+
span,
219+
SPANDATA.GEN_AI_REQUEST_MESSAGES,
220+
normalized_messages,
221+
unpack=False,
214222
)
215223

216224
def on_chat_model_start(self, serialized, messages, *, run_id, **kwargs):
@@ -262,6 +270,8 @@ def on_chat_model_start(self, serialized, messages, *, run_id, **kwargs):
262270
normalized_messages.append(
263271
self._normalize_langchain_message(message)
264272
)
273+
normalized_messages = normalize_message_roles(normalized_messages)
274+
265275
set_data_normalized(
266276
span,
267277
SPANDATA.GEN_AI_REQUEST_MESSAGES,
@@ -740,8 +750,12 @@ def new_invoke(self, *args, **kwargs):
740750
and should_send_default_pii()
741751
and integration.include_prompts
742752
):
753+
normalized_messages = normalize_message_roles([input])
743754
set_data_normalized(
744-
span, SPANDATA.GEN_AI_REQUEST_MESSAGES, [input], unpack=False
755+
span,
756+
SPANDATA.GEN_AI_REQUEST_MESSAGES,
757+
normalized_messages,
758+
unpack=False,
745759
)
746760

747761
output = result.get("output")
@@ -791,8 +805,12 @@ def new_stream(self, *args, **kwargs):
791805
and should_send_default_pii()
792806
and integration.include_prompts
793807
):
808+
normalized_messages = normalize_message_roles([input])
794809
set_data_normalized(
795-
span, SPANDATA.GEN_AI_REQUEST_MESSAGES, [input], unpack=False
810+
span,
811+
SPANDATA.GEN_AI_REQUEST_MESSAGES,
812+
normalized_messages,
813+
unpack=False,
796814
)
797815

798816
# Run the agent

0 commit comments

Comments
 (0)