|
36 | 36 | from uuid import UUID |
37 | 37 |
|
38 | 38 | from sentry_sdk.tracing import Span |
| 39 | + from sentry_sdk._types import TextPart |
39 | 40 |
|
40 | 41 |
|
41 | 42 | try: |
@@ -189,20 +190,27 @@ def _get_current_agent() -> "Optional[str]": |
189 | 190 | return None |
190 | 191 |
|
191 | 192 |
|
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 | + |
195 | 196 | for list_ in messages: |
196 | 197 | for message in list_: |
197 | 198 | 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 | + ] |
206 | 214 |
|
207 | 215 |
|
208 | 216 | class LangchainIntegration(Integration): |
@@ -446,7 +454,14 @@ def on_chat_model_start( |
446 | 454 | _set_tools_on_span(span, all_params.get("tools")) |
447 | 455 |
|
448 | 456 | 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 | + ) |
450 | 465 |
|
451 | 466 | normalized_messages = [] |
452 | 467 | for list_ in messages: |
|
0 commit comments