Skip to content
Merged
Show file tree
Hide file tree
Changes from 69 commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
238f87c
fix(ai): Keep single content input message
alexander-alderman-webb Jan 20, 2026
de7996d
remove system prompt anthropic test
alexander-alderman-webb Jan 20, 2026
7aedbde
update openai agents test
alexander-alderman-webb Jan 20, 2026
67b768a
openai tests
alexander-alderman-webb Jan 20, 2026
3e1cb35
ai monitoring tests
alexander-alderman-webb Jan 20, 2026
49bbcd8
google_genai tests
alexander-alderman-webb Jan 20, 2026
9d192b4
pydantic-ai tests
alexander-alderman-webb Jan 20, 2026
708b55a
remove comment
alexander-alderman-webb Jan 20, 2026
95d15f6
remove if statement in test
alexander-alderman-webb Jan 20, 2026
01d0619
deduplicate
alexander-alderman-webb Jan 20, 2026
9cf5643
Revert "deduplicate"
alexander-alderman-webb Jan 21, 2026
fe5d1e1
remove if condition in test
alexander-alderman-webb Jan 21, 2026
a7990ad
ref(openai): Separate input handling to improve typing
alexander-alderman-webb Jan 21, 2026
c6ebc0f
remove old func
alexander-alderman-webb Jan 21, 2026
8d9fa37
.
alexander-alderman-webb Jan 21, 2026
ab41f1e
expand type
alexander-alderman-webb Jan 21, 2026
b3117ac
add ignores
alexander-alderman-webb Jan 21, 2026
847d4b2
revert unrelated change
alexander-alderman-webb Jan 21, 2026
4f25a56
feat(openai): Set system instruction attribute
alexander-alderman-webb Jan 21, 2026
903bbbd
use union
alexander-alderman-webb Jan 21, 2026
f304bec
mypy
alexander-alderman-webb Jan 21, 2026
921a82f
Merge branch 'webb/openai-separate-input-handling' into webb/openai-a…
alexander-alderman-webb Jan 21, 2026
9f5d801
.
alexander-alderman-webb Jan 21, 2026
d66ffe1
use specific openai types
alexander-alderman-webb Jan 21, 2026
ac3ce00
wip
alexander-alderman-webb Jan 22, 2026
ef9fe6f
.
alexander-alderman-webb Jan 22, 2026
ce84a29
.
alexander-alderman-webb Jan 22, 2026
dee9930
remove test
alexander-alderman-webb Jan 22, 2026
cb00ab3
.
alexander-alderman-webb Jan 22, 2026
26b932b
.
alexander-alderman-webb Jan 22, 2026
04dc92c
.
alexander-alderman-webb Jan 22, 2026
c7263ea
edge case
alexander-alderman-webb Jan 22, 2026
d947899
full responses api tests
alexander-alderman-webb Jan 22, 2026
8cbeac1
remove sentry_sdk/ai/_openai_completions_api.py
alexander-alderman-webb Jan 22, 2026
bcebcc8
fix test
alexander-alderman-webb Jan 22, 2026
fd194e2
merge master
alexander-alderman-webb Jan 23, 2026
53b96f9
merge opanai branch
alexander-alderman-webb Jan 23, 2026
f749ae4
Merge branch 'master' into webb/openai-separate-input-handling
alexander-alderman-webb Jan 23, 2026
87dd8fe
Merge branch 'webb/openai-separate-input-handling' into webb/openai-a…
alexander-alderman-webb Jan 23, 2026
6f5d62c
Merge branch 'webb/openai-ai-prompt-message' into webb/keep-single-me…
alexander-alderman-webb Jan 23, 2026
4aed172
handle embeddings separately
alexander-alderman-webb Jan 23, 2026
085b496
feat(openai-agents): Set system instruction attribute on gen_ai.chat …
alexander-alderman-webb Jan 23, 2026
48c7fbe
add type ignores
alexander-alderman-webb Jan 23, 2026
3f5ad11
more defensive checks in case input is not iterable
alexander-alderman-webb Jan 23, 2026
ebf2d9c
merge
alexander-alderman-webb Jan 23, 2026
fc9f1fa
pick up changes to extraction functions
alexander-alderman-webb Jan 23, 2026
bcdd87c
fix func name
alexander-alderman-webb Jan 23, 2026
68b853f
fix Iterable import
alexander-alderman-webb Jan 23, 2026
5ee5274
remove runtime import
alexander-alderman-webb Jan 23, 2026
1f0f98d
feat(ai): Add original input length meta attribute
alexander-alderman-webb Jan 26, 2026
a8840d5
more early returns
alexander-alderman-webb Jan 26, 2026
382e933
revert unrelated tests
alexander-alderman-webb Jan 26, 2026
179d59e
revert unrelated change
alexander-alderman-webb Jan 26, 2026
12cb219
address comment
alexander-alderman-webb Jan 26, 2026
a6152fe
remove unused type ignore
alexander-alderman-webb Jan 26, 2026
74b5a4c
merge
alexander-alderman-webb Jan 26, 2026
eda980c
fix typo in filename
alexander-alderman-webb Jan 26, 2026
5825835
remove unused import
alexander-alderman-webb Jan 26, 2026
92ecc88
merge
alexander-alderman-webb Jan 26, 2026
753b5c0
add anthropic test case
alexander-alderman-webb Jan 26, 2026
7400f9d
remove parameter in test case
alexander-alderman-webb Jan 26, 2026
23868e5
merge
alexander-alderman-webb Jan 26, 2026
c625e12
revert test_ai_monitoring tests
alexander-alderman-webb Jan 26, 2026
69c78a8
merge
alexander-alderman-webb Jan 26, 2026
2a80a8d
restore test change
alexander-alderman-webb Jan 26, 2026
0628d92
update ai_monitoring tests
alexander-alderman-webb Jan 26, 2026
6dc02f9
add const file
alexander-alderman-webb Jan 26, 2026
7e5b395
Merge branch 'webb/keep-single-message' into webb/set-original-messag…
alexander-alderman-webb Jan 26, 2026
d151119
openai tests
alexander-alderman-webb Jan 26, 2026
1418237
merge master
alexander-alderman-webb Jan 28, 2026
9ce59bb
resolving openai test conflicts
alexander-alderman-webb Jan 28, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions sentry_sdk/ai/_openai_completions_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from collections.abc import Iterable

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from sentry_sdk._types import TextPart

from openai.types.chat import (
ChatCompletionMessageParam,
ChatCompletionSystemMessageParam,
)


def _is_system_instruction(message: "ChatCompletionMessageParam") -> bool:
return isinstance(message, dict) and message.get("role") == "system"


def _get_system_instructions(
messages: "Iterable[ChatCompletionMessageParam]",
) -> "list[ChatCompletionMessageParam]":
if not isinstance(messages, Iterable):
return []

return [message for message in messages if _is_system_instruction(message)]


def _transform_system_instructions(
system_instructions: "list[ChatCompletionSystemMessageParam]",
) -> "list[TextPart]":
instruction_text_parts: "list[TextPart]" = []

for instruction in system_instructions:
if not isinstance(instruction, dict):
continue

content = instruction.get("content")

if isinstance(content, str):
instruction_text_parts.append({"type": "text", "content": content})

elif isinstance(content, list):
for part in content:
if isinstance(part, dict) and part.get("type") == "text":
text = part.get("text", "")
if text:
instruction_text_parts.append({"type": "text", "content": text})

return instruction_text_parts
22 changes: 22 additions & 0 deletions sentry_sdk/ai/_openai_responses_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Union

from openai.types.responses import ResponseInputParam, ResponseInputItemParam


def _is_system_instruction(message: "ResponseInputItemParam") -> bool:
if not isinstance(message, dict) or not message.get("role") == "system":
return False

return "type" not in message or message["type"] == "message"


def _get_system_instructions(
messages: "Union[str, ResponseInputParam]",
) -> "list[ResponseInputItemParam]":
if not isinstance(messages, list):
return []

return [message for message in messages if _is_system_instruction(message)]
23 changes: 23 additions & 0 deletions sentry_sdk/ai/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import sentry_sdk
from sentry_sdk.utils import logger
from sentry_sdk.consts import SPANDATA

MAX_GEN_AI_MESSAGE_BYTES = 20_000 # 20KB
# Maximum characters when only a single message is left after bytes truncation
Expand Down Expand Up @@ -682,6 +683,28 @@ def truncate_messages_by_size(


def truncate_and_annotate_messages(
messages: "Optional[List[Dict[str, Any]]]",
span: "Any",
scope: "Any",
max_single_message_chars: int = MAX_SINGLE_MESSAGE_CONTENT_CHARS,
) -> "Optional[List[Dict[str, Any]]]":
if not messages:
return None

messages = redact_blob_message_parts(messages)

truncated_message = _truncate_single_message_content_if_present(
deepcopy(messages[-1]), max_chars=max_single_message_chars
)
if len(messages) > 1:
scope._gen_ai_original_message_count[span.span_id] = len(messages)

span.set_data(SPANDATA.META_GEN_AI_ORIGINAL_INPUT_MESSAGES_LENGTH, len(messages))

return [truncated_message]


def truncate_and_annotate_embedding_inputs(
messages: "Optional[List[Dict[str, Any]]]",
span: "Any",
scope: "Any",
Expand Down
8 changes: 8 additions & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,14 @@ class SPANDATA:
Example: "a1b2c3d4e5f6"
"""

META_GEN_AI_ORIGINAL_INPUT_MESSAGES_LENGTH = (
"sentry.sdk_meta.gen_ai.input.messages.original_length"
)
"""
The original number of input non-system instruction messages, before SDK trimming.
Example: 4
"""


class SPANSTATUS:
"""
Expand Down
5 changes: 4 additions & 1 deletion sentry_sdk/integrations/litellm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
set_data_normalized,
truncate_and_annotate_messages,
transform_openai_content_part,
truncate_and_annotate_embedding_inputs,
)
from sentry_sdk.consts import SPANDATA
from sentry_sdk.integrations import DidNotEnable, Integration
Expand Down Expand Up @@ -118,7 +119,9 @@ def _input_callback(kwargs: "Dict[str, Any]") -> None:
if isinstance(embedding_input, list)
else [embedding_input]
)
messages_data = truncate_and_annotate_messages(input_list, span, scope)
messages_data = truncate_and_annotate_embedding_inputs(
input_list, span, scope
)
if messages_data is not None:
set_data_normalized(
span,
Expand Down
Loading
Loading