Skip to content

Commit ca94851

Browse files
.
1 parent 5aa6136 commit ca94851

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

sentry_sdk/integrations/pydantic_ai/spans/ai_client.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,40 @@
5151

5252

5353
def _transform_system_instructions(
54-
system_instructions: "list[SystemPromptPart]",
54+
permanent_instructions: "list[SystemPromptPart]",
55+
current_instructions: "list[str]",
5556
) -> "list[SentryTextPart]":
5657
return [
5758
{
5859
"type": "text",
5960
"content": instruction.content,
6061
}
61-
for instruction in system_instructions
62+
for instruction in permanent_instructions
63+
] + [
64+
{
65+
"type": "text",
66+
"content": instruction,
67+
}
68+
for instruction in current_instructions
6269
]
6370

6471

6572
def _get_system_instructions(
6673
messages: "list[ModelMessage]",
67-
) -> "list[SystemPromptPart]":
68-
system_instructions = []
74+
) -> "tuple[list[SystemPromptPart], list[str]]":
75+
permanent_instructions = []
76+
current_instructions = []
6977

7078
for msg in messages:
7179
if hasattr(msg, "parts"):
7280
for part in msg.parts:
7381
if SystemPromptPart and isinstance(part, SystemPromptPart):
74-
system_instructions.append(part)
82+
permanent_instructions.append(part)
83+
84+
if hasattr(msg, "instructions") and msg.instructions is not None:
85+
current_instructions.append(msg.instructions)
7586

76-
return system_instructions
87+
return permanent_instructions, current_instructions
7788

7889

7990
def _set_input_messages(span: "sentry_sdk.tracing.Span", messages: "Any") -> None:
@@ -84,12 +95,14 @@ def _set_input_messages(span: "sentry_sdk.tracing.Span", messages: "Any") -> Non
8495
if not messages:
8596
return
8697

87-
system_instructions = _get_system_instructions(messages)
88-
if len(system_instructions) > 0:
98+
permanent_instructions, current_instructions = _get_system_instructions(messages)
99+
if len(permanent_instructions) > 0 or len(current_instructions) > 0:
89100
set_data_normalized(
90101
span,
91102
SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS,
92-
_transform_system_instructions(system_instructions),
103+
_transform_system_instructions(
104+
permanent_instructions, current_instructions
105+
),
93106
unpack=False,
94107
)
95108

tests/integrations/pydantic_ai/test_pydantic_ai.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1249,7 +1249,8 @@ async def test_invoke_agent_with_instructions(
12491249
if send_default_pii and include_prompts:
12501250
system_instructions = chat_span["data"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS]
12511251
assert json.loads(system_instructions) == [
1252-
{"type": "text", "content": "System prompt"}
1252+
{"type": "text", "content": "System prompt"},
1253+
{"type": "text", "content": "Instruction 1\nInstruction 2"},
12531254
]
12541255
else:
12551256
assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS not in chat_span["data"]

0 commit comments

Comments
 (0)