Skip to content

Commit dafd62f

Browse files
ref: Replace set_data_normalized() with Span.set_data() for system instructions (#5374)
1 parent b3aa033 commit dafd62f

File tree

6 files changed

+34
-38
lines changed

6 files changed

+34
-38
lines changed

sentry_sdk/integrations/anthropic.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
import json
23
from collections.abc import Iterable
34
from functools import wraps
45
from typing import TYPE_CHECKING
@@ -217,11 +218,9 @@ def _set_input_data(
217218
if isinstance(system_instructions, str) or isinstance(
218219
system_instructions, Iterable
219220
):
220-
set_data_normalized(
221-
span,
221+
span.set_data(
222222
SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS,
223-
_transform_system_instructions(system_instructions),
224-
unpack=False,
223+
json.dumps(_transform_system_instructions(system_instructions)),
225224
)
226225

227226
normalized_messages = []

sentry_sdk/integrations/google_genai/utils.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import copy
2+
import json
23
import inspect
34
from functools import wraps
45
from .consts import ORIGIN, TOOL_ATTRIBUTES_MAP, GEN_AI_SYSTEM
@@ -808,11 +809,9 @@ def set_span_data_for_request(
808809
system_instructions = config.get("system_instruction")
809810

810811
if system_instructions is not None:
811-
set_data_normalized(
812-
span,
812+
span.set_data(
813813
SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS,
814-
_transform_system_instructions(system_instructions),
815-
unpack=False,
814+
json.dumps(_transform_system_instructions(system_instructions)),
816815
)
817816

818817
# Extract messages from contents

sentry_sdk/integrations/langchain.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import contextvars
22
import itertools
33
import sys
4+
import json
45
import warnings
56
from collections import OrderedDict
67
from functools import wraps
@@ -467,11 +468,9 @@ def on_chat_model_start(
467468
if should_send_default_pii() and self.include_prompts:
468469
system_instructions = _get_system_instructions(messages)
469470
if len(system_instructions) > 0:
470-
set_data_normalized(
471-
span,
471+
span.set_data(
472472
SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS,
473-
_transform_system_instructions(system_instructions),
474-
unpack=False,
473+
json.dumps(_transform_system_instructions(system_instructions)),
475474
)
476475

477476
normalized_messages = []

sentry_sdk/integrations/openai.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
import json
23
import time
34
from functools import wraps
45
from collections.abc import Iterable
@@ -273,16 +274,16 @@ def _set_responses_api_input_data(
273274
and explicit_instructions is not None
274275
and _is_given(explicit_instructions)
275276
):
276-
set_data_normalized(
277-
span,
277+
span.set_data(
278278
SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS,
279-
[
280-
{
281-
"type": "text",
282-
"content": explicit_instructions,
283-
}
284-
],
285-
unpack=False,
279+
json.dumps(
280+
[
281+
{
282+
"type": "text",
283+
"content": explicit_instructions,
284+
}
285+
]
286+
),
286287
)
287288

288289
set_data_normalized(span, SPANDATA.GEN_AI_OPERATION_NAME, "responses")
@@ -309,11 +310,9 @@ def _set_responses_api_input_data(
309310
instructions_text_parts += _transform_system_instructions(system_instructions)
310311

311312
if len(instructions_text_parts) > 0:
312-
set_data_normalized(
313-
span,
313+
span.set_data(
314314
SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS,
315-
instructions_text_parts,
316-
unpack=False,
315+
json.dumps(instructions_text_parts),
317316
)
318317

319318
if isinstance(messages, str):
@@ -365,11 +364,9 @@ def _set_completions_api_input_data(
365364

366365
system_instructions = _get_system_instructions_completions(messages)
367366
if len(system_instructions) > 0:
368-
set_data_normalized(
369-
span,
367+
span.set_data(
370368
SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS,
371-
_transform_system_instructions(system_instructions),
372-
unpack=False,
369+
json.dumps(_transform_system_instructions(system_instructions)),
373370
)
374371

375372
if isinstance(messages, str):

sentry_sdk/integrations/openai_agents/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
13
import sentry_sdk
24
from sentry_sdk.ai.utils import (
35
GEN_AI_ALLOWED_MESSAGE_ROLES,
@@ -146,11 +148,9 @@ def _set_input_data(
146148
instructions_text_parts += _transform_system_instructions(system_instructions)
147149

148150
if len(instructions_text_parts) > 0:
149-
set_data_normalized(
150-
span,
151+
span.set_data(
151152
SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS,
152-
instructions_text_parts,
153-
unpack=False,
153+
json.dumps(instructions_text_parts),
154154
)
155155

156156
non_system_messages = [

sentry_sdk/integrations/pydantic_ai/spans/ai_client.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
13
import sentry_sdk
24
from sentry_sdk._types import BLOB_DATA_SUBSTITUTE
35
from sentry_sdk.ai.utils import (
@@ -101,13 +103,13 @@ def _set_input_messages(span: "sentry_sdk.tracing.Span", messages: "Any") -> Non
101103

102104
permanent_instructions, current_instructions = _get_system_instructions(messages)
103105
if len(permanent_instructions) > 0 or len(current_instructions) > 0:
104-
set_data_normalized(
105-
span,
106+
span.set_data(
106107
SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS,
107-
_transform_system_instructions(
108-
permanent_instructions, current_instructions
108+
json.dumps(
109+
_transform_system_instructions(
110+
permanent_instructions, current_instructions
111+
)
109112
),
110-
unpack=False,
111113
)
112114

113115
try:

0 commit comments

Comments
 (0)