Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
47 changes: 47 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,52 @@
# Changelog

## 2.58.0a1

### New Features ✨

- (ci) Cancel in-progress PR workflows on new commit push by @joshuarli in [#5994](https://github.com/getsentry/sentry-python/pull/5994)
- Send GenAI spans as V2 envelope items by @alexander-alderman-webb in [#6079](https://github.com/getsentry/sentry-python/pull/6079)

### Bug Fixes 🐛

- (google_genai) Redact binary data in inline_data and fix multi-part message extraction by @ericapisani in [#5977](https://github.com/getsentry/sentry-python/pull/5977)
- (profiler) Stop nulling buffer on teardown by @ericapisani in [#6075](https://github.com/getsentry/sentry-python/pull/6075)

### Internal Changes 🔧

#### Anthropic

- Revert input truncation by @alexander-alderman-webb in [#6113](https://github.com/getsentry/sentry-python/pull/6113)
- Revert input transformation by @alexander-alderman-webb in [#6108](https://github.com/getsentry/sentry-python/pull/6108)

#### Google Genai

- Revert input truncation by @alexander-alderman-webb in [#6111](https://github.com/getsentry/sentry-python/pull/6111)
- Revert input transformation by @alexander-alderman-webb in [#6105](https://github.com/getsentry/sentry-python/pull/6105)

#### Langchain

- Revert input truncation by @alexander-alderman-webb in [#6115](https://github.com/getsentry/sentry-python/pull/6115)
- Revert input transformation by @alexander-alderman-webb in [#6109](https://github.com/getsentry/sentry-python/pull/6109)

#### Litellm

- Revert input truncation by @alexander-alderman-webb in [#6112](https://github.com/getsentry/sentry-python/pull/6112)
- Revert input transformation by @alexander-alderman-webb in [#6107](https://github.com/getsentry/sentry-python/pull/6107)

#### Pydantic Ai

- Revert input truncation by @alexander-alderman-webb in [#6106](https://github.com/getsentry/sentry-python/pull/6106)
- Remove dead `Model.request` patch by @alexander-alderman-webb in [#5956](https://github.com/getsentry/sentry-python/pull/5956)

#### Other

- (ai) Revert binary blob truncation by @alexander-alderman-webb in [#6110](https://github.com/getsentry/sentry-python/pull/6110)
- (langgraph) Revert input truncation by @alexander-alderman-webb in [#6114](https://github.com/getsentry/sentry-python/pull/6114)
- (openai) Revert input truncation by @alexander-alderman-webb in [#6117](https://github.com/getsentry/sentry-python/pull/6117)
- (openai-agents) Revert input truncation by @alexander-alderman-webb in [#6116](https://github.com/getsentry/sentry-python/pull/6116)
- Set explicit base-branch for codecov action by @ericapisani in [#5992](https://github.com/getsentry/sentry-python/pull/5992)

## 2.58.0

### New Features ✨
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
copyright = "2019-{}, Sentry Team and Contributors".format(datetime.now().year)
author = "Sentry Team and Contributors"

release = "2.58.0"
release = "2.58.0a1"
version = ".".join(release.split(".")[:2]) # The short X.Y version.


Expand Down
93 changes: 1 addition & 92 deletions sentry_sdk/ai/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import inspect
import json
from copy import deepcopy
from typing import TYPE_CHECKING


if TYPE_CHECKING:
from typing import Any, Callable, Dict, List, Optional, Tuple
from typing import Any, Callable, Dict, Tuple

from sentry_sdk.tracing import Span

Expand All @@ -14,10 +13,6 @@
from sentry_sdk.traces import StreamedSpan
from sentry_sdk.tracing_utils import has_span_streaming_enabled

MAX_GEN_AI_MESSAGE_BYTES = 20_000 # 20KB
# Maximum characters when only a single message is left after bytes truncation
MAX_SINGLE_MESSAGE_CONTENT_CHARS = 10_000


class GEN_AI_ALLOWED_MESSAGE_ROLES:
SYSTEM = "system"
Expand Down Expand Up @@ -180,92 +175,6 @@ def _truncate_single_message_content_if_present(
return message


def _find_truncation_index(messages: "List[Dict[str, Any]]", max_bytes: int) -> int:
"""
Find the index of the first message that would exceed the max bytes limit.
Compute the individual message sizes, and return the index of the first message from the back
of the list that would exceed the max bytes limit.
"""
running_sum = 0
for idx in range(len(messages) - 1, -1, -1):
size = len(json.dumps(messages[idx], separators=(",", ":")).encode("utf-8"))
running_sum += size
if running_sum > max_bytes:
return idx + 1

return 0


def truncate_messages_by_size(
messages: "List[Dict[str, Any]]",
max_bytes: int = MAX_GEN_AI_MESSAGE_BYTES,
max_single_message_chars: int = MAX_SINGLE_MESSAGE_CONTENT_CHARS,
) -> "Tuple[List[Dict[str, Any]], int]":
"""
Returns a truncated messages list, consisting of
- the last message, with its content truncated to `max_single_message_chars` characters,
if the last message's size exceeds `max_bytes` bytes; otherwise,
- the maximum number of messages, starting from the end of the `messages` list, whose total
serialized size does not exceed `max_bytes` bytes.

In the single message case, the serialized message size may exceed `max_bytes`, because
truncation is based only on character count in that case.
"""
serialized_json = json.dumps(messages, separators=(",", ":"))
current_size = len(serialized_json.encode("utf-8"))

if current_size <= max_bytes:
return messages, 0

truncation_index = _find_truncation_index(messages, max_bytes)
if truncation_index < len(messages):
truncated_messages = messages[truncation_index:]
else:
truncation_index = len(messages) - 1
truncated_messages = messages[-1:]

if len(truncated_messages) == 1:
truncated_messages[0] = _truncate_single_message_content_if_present(
deepcopy(truncated_messages[0]), max_chars=max_single_message_chars
)

return truncated_messages, truncation_index


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

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)

return [truncated_message]


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

truncated_messages, removed_count = truncate_messages_by_size(messages, max_bytes)
if removed_count > 0:
scope._gen_ai_original_message_count[span.span_id] = len(messages)

return truncated_messages


def set_conversation_id(conversation_id: str) -> None:
"""
Set the conversation_id in the scope.
Expand Down
2 changes: 1 addition & 1 deletion sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -1486,4 +1486,4 @@ def _get_default_options() -> "dict[str, Any]":
del _get_default_options


VERSION = "2.58.0"
VERSION = "2.58.0a1"
56 changes: 16 additions & 40 deletions sentry_sdk/integrations/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from sentry_sdk.ai.utils import (
set_data_normalized,
normalize_message_roles,
truncate_and_annotate_messages,
truncate_and_annotate_embedding_inputs,
)
from sentry_sdk.ai._openai_completions_api import (
_is_system_instruction as _is_system_instruction_completions,
Expand Down Expand Up @@ -397,12 +395,9 @@ def _set_responses_api_input_data(

if isinstance(messages, str):
normalized_messages = normalize_message_roles([messages]) # type: ignore
scope = sentry_sdk.get_current_scope()
messages_data = truncate_and_annotate_messages(normalized_messages, span, scope)
if messages_data is not None:
set_data_normalized(
span, SPANDATA.GEN_AI_REQUEST_MESSAGES, messages_data, unpack=False
)
set_data_normalized(
span, SPANDATA.GEN_AI_REQUEST_MESSAGES, normalized_messages, unpack=False
)

set_data_normalized(span, SPANDATA.GEN_AI_OPERATION_NAME, "responses")
return
Expand All @@ -412,12 +407,9 @@ def _set_responses_api_input_data(
]
if len(non_system_messages) > 0:
normalized_messages = normalize_message_roles(non_system_messages)
scope = sentry_sdk.get_current_scope()
messages_data = truncate_and_annotate_messages(normalized_messages, span, scope)
if messages_data is not None:
set_data_normalized(
span, SPANDATA.GEN_AI_REQUEST_MESSAGES, messages_data, unpack=False
)
set_data_normalized(
span, SPANDATA.GEN_AI_REQUEST_MESSAGES, normalized_messages, unpack=False
)

set_data_normalized(span, SPANDATA.GEN_AI_OPERATION_NAME, "responses")

Expand Down Expand Up @@ -471,12 +463,9 @@ def _set_completions_api_input_data(

if isinstance(messages, str):
normalized_messages = normalize_message_roles([messages]) # type: ignore
scope = sentry_sdk.get_current_scope()
messages_data = truncate_and_annotate_messages(normalized_messages, span, scope)
if messages_data is not None:
set_data_normalized(
span, SPANDATA.GEN_AI_REQUEST_MESSAGES, messages_data, unpack=False
)
set_data_normalized(
span, SPANDATA.GEN_AI_REQUEST_MESSAGES, normalized_messages, unpack=False
)
set_data_normalized(span, SPANDATA.GEN_AI_OPERATION_NAME, "chat")
return

Expand All @@ -502,12 +491,9 @@ def _set_completions_api_input_data(
]
if len(non_system_messages) > 0:
normalized_messages = normalize_message_roles(non_system_messages)
scope = sentry_sdk.get_current_scope()
messages_data = truncate_and_annotate_messages(normalized_messages, span, scope)
if messages_data is not None:
set_data_normalized(
span, SPANDATA.GEN_AI_REQUEST_MESSAGES, messages_data, unpack=False
)
set_data_normalized(
span, SPANDATA.GEN_AI_REQUEST_MESSAGES, normalized_messages, unpack=False
)

set_data_normalized(span, SPANDATA.GEN_AI_OPERATION_NAME, "chat")

Expand Down Expand Up @@ -538,14 +524,9 @@ def _set_embeddings_input_data(
set_data_normalized(span, SPANDATA.GEN_AI_OPERATION_NAME, "embeddings")

normalized_messages = normalize_message_roles([messages]) # type: ignore
scope = sentry_sdk.get_current_scope()
messages_data = truncate_and_annotate_embedding_inputs(
normalized_messages, span, scope
set_data_normalized(
span, SPANDATA.GEN_AI_EMBEDDINGS_INPUT, normalized_messages, unpack=False
)
if messages_data is not None:
set_data_normalized(
span, SPANDATA.GEN_AI_EMBEDDINGS_INPUT, messages_data, unpack=False
)

return

Expand All @@ -559,14 +540,9 @@ def _set_embeddings_input_data(

if len(messages) > 0:
normalized_messages = normalize_message_roles(messages)
scope = sentry_sdk.get_current_scope()
messages_data = truncate_and_annotate_embedding_inputs(
normalized_messages, span, scope
set_data_normalized(
span, SPANDATA.GEN_AI_EMBEDDINGS_INPUT, normalized_messages, unpack=False
)
if messages_data is not None:
set_data_normalized(
span, SPANDATA.GEN_AI_EMBEDDINGS_INPUT, messages_data, unpack=False
)

set_data_normalized(span, SPANDATA.GEN_AI_OPERATION_NAME, "embeddings")

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def get_file_text(file_name):

setup(
name="sentry-sdk",
version="2.58.0",
version="2.58.0a1",
author="Sentry Team and Contributors",
author_email="hello@sentry.io",
url="https://github.com/getsentry/sentry-python",
Expand Down
45 changes: 0 additions & 45 deletions tests/integrations/openai/test_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -3724,51 +3724,6 @@ def test_openai_message_role_mapping(
assert stored_messages[0]["role"] == expected_role


def test_openai_message_truncation(sentry_init, capture_items):
"""Test that large messages are truncated properly in OpenAI integration."""
sentry_init(
integrations=[OpenAIIntegration(include_prompts=True)],
traces_sample_rate=1.0,
send_default_pii=True,
)
items = capture_items("transaction", "span")

client = OpenAI(api_key="z")
client.chat.completions._post = mock.Mock(return_value=EXAMPLE_CHAT_COMPLETION)

large_content = (
"This is a very long message that will exceed our size limits. " * 1000
)
large_messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": large_content},
{"role": "assistant", "content": large_content},
{"role": "user", "content": large_content},
]

with start_transaction(name="openai tx"):
client.chat.completions.create(
model="some-model",
messages=large_messages,
)

span = next(item.payload for item in items if item.type == "span")
assert SPANDATA.GEN_AI_REQUEST_MESSAGES in span["attributes"]

messages_data = span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
assert isinstance(messages_data, str)

parsed_messages = json.loads(messages_data)
assert isinstance(parsed_messages, list)
assert len(parsed_messages) <= len(large_messages)

(event,) = (item.payload for item in items if item.type == "transaction")
meta_path = event["_meta"]
span_meta = meta_path["spans"]["0"]["data"]
messages_meta = span_meta[SPANDATA.GEN_AI_REQUEST_MESSAGES]
assert "len" in messages_meta.get("", {})


# noinspection PyTypeChecker
def test_streaming_chat_completion_ttft(
sentry_init, capture_items, get_model_response, server_side_event_chunks
Expand Down
Loading
Loading