Skip to content

Commit 306741e

Browse files
.
1 parent 1c87221 commit 306741e

3 files changed

Lines changed: 43 additions & 20 deletions

File tree

sentry_sdk/_types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,7 @@ class SDKInfo(TypedDict):
359359
)
360360

361361
HttpStatusCodeRange = Union[int, Container[int]]
362+
363+
class TextPart(TypedDict):
364+
type: Literal["text"]
365+
content: str

sentry_sdk/integrations/langchain.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from uuid import UUID
3737

3838
from sentry_sdk.tracing import Span
39+
from sentry_sdk._types import TextPart
3940

4041

4142
try:
@@ -189,20 +190,27 @@ def _get_current_agent() -> "Optional[str]":
189190
return None
190191

191192

192-
def _set_system_prompt(
193-
span: "sentry_sdk.tracing.Span", messages: "List[List[BaseMessage]]"
194-
) -> None:
193+
def _get_system_instructions(messages: "List[List[BaseMessage]]") -> "List[TextPart]":
194+
system_instructions = []
195+
195196
for list_ in messages:
196197
for message in list_:
197198
if message.type == "system":
198-
system_prompt = message.content
199-
set_data_normalized(
200-
span,
201-
SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS,
202-
system_prompt,
203-
unpack=False,
204-
)
205-
return
199+
system_instructions.append(message)
200+
201+
return system_instructions
202+
203+
204+
def _transform_system_instructions(
205+
system_instructions: "List[BaseMessage]",
206+
) -> "List[TextPart]":
207+
return [
208+
{
209+
"type": "text",
210+
"content": instruction.content,
211+
}
212+
for instruction in system_instructions
213+
]
206214

207215

208216
class LangchainIntegration(Integration):
@@ -446,7 +454,14 @@ def on_chat_model_start(
446454
_set_tools_on_span(span, all_params.get("tools"))
447455

448456
if should_send_default_pii() and self.include_prompts:
449-
_set_system_prompt(span, messages)
457+
system_instructions = _get_system_instructions(messages)
458+
if len(system_instructions) > 0:
459+
set_data_normalized(
460+
span,
461+
SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS,
462+
_transform_system_instructions(system_instructions),
463+
unpack=False,
464+
)
450465

451466
normalized_messages = []
452467
for list_ in messages:

tests/integrations/langchain/test_langchain.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,21 @@ def test_langchain_agent(
217217
assert chat_spans[1]["data"]["gen_ai.usage.total_tokens"] == 117
218218

219219
if send_default_pii and include_prompts:
220-
assert (
221-
"You are very powerful"
222-
in chat_spans[0]["data"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS]
223-
)
220+
assert [
221+
{
222+
"type": "text",
223+
"content": "You are very powerful assistant, but don't know current events",
224+
}
225+
] == json.loads(chat_spans[0]["data"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS])
224226
assert "5" in chat_spans[0]["data"][SPANDATA.GEN_AI_RESPONSE_TEXT]
225227
assert "word" in tool_exec_span["data"][SPANDATA.GEN_AI_TOOL_INPUT]
226228
assert 5 == int(tool_exec_span["data"][SPANDATA.GEN_AI_TOOL_OUTPUT])
227-
assert (
228-
"You are very powerful"
229-
in chat_spans[1]["data"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS]
230-
)
229+
assert [
230+
{
231+
"type": "text",
232+
"content": "You are very powerful assistant, but don't know current events",
233+
}
234+
] == json.loads(chat_spans[1]["data"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS])
231235
assert "5" in chat_spans[1]["data"][SPANDATA.GEN_AI_RESPONSE_TEXT]
232236

233237
# Verify tool calls are recorded when PII is enabled

0 commit comments

Comments
 (0)