From 07ca69bbc34763657772f81a49d0c168de54cb46 Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Wed, 15 Jul 2026 23:34:46 +0000 Subject: [PATCH 1/3] fix(ai): Normalize Pydantic messages before truncation Co-Authored-By: benry --- sentry_sdk/ai/utils.py | 1 + tests/test_ai_monitoring.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/sentry_sdk/ai/utils.py b/sentry_sdk/ai/utils.py index 7b1ca9324b..8d88d4da5e 100644 --- a/sentry_sdk/ai/utils.py +++ b/sentry_sdk/ai/utils.py @@ -715,6 +715,7 @@ def truncate_messages_by_size( In the single message case, the serialized message size may exceed `max_bytes`, because truncation is based only on character count in that case. """ + messages = _normalize_data(messages, unpack=False) serialized_json = json.dumps(messages, separators=(",", ":")) current_size = len(serialized_json.encode("utf-8")) diff --git a/tests/test_ai_monitoring.py b/tests/test_ai_monitoring.py index 70b4a6bcdd..aa5e113801 100644 --- a/tests/test_ai_monitoring.py +++ b/tests/test_ai_monitoring.py @@ -245,6 +245,24 @@ def test_empty_messages_list(self): assert result == [] assert truncation_index == 0 + def test_normalizes_pydantic_messages(self): + class PydanticMessage: + def model_dump(self): + return {"type": "function_call", "name": "get_weather"} + + messages = [ + {"role": "user", "content": "What's the weather?"}, + PydanticMessage(), + ] + + result, truncation_index = truncate_messages_by_size(messages) + + assert result == [ + {"role": "user", "content": "What's the weather?"}, + {"type": "function_call", "name": "get_weather"}, + ] + assert truncation_index == 0 + def test_find_truncation_index( self, ): From 8b2c7b05f0fb8ad779fad4c17fa4235be481b1c3 Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Wed, 15 Jul 2026 23:42:43 +0000 Subject: [PATCH 2/3] chore: Link PY-2043 Refs: https://linear.app/getsentry/issue/PY-2043 Co-Authored-By: benry From 6ff0c90ebf58689c2c7c22b72e055ea8fe3b2e72 Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Wed, 15 Jul 2026 23:57:20 +0000 Subject: [PATCH 3/3] fix(ai): Preserve null values during normalization Refs: https://linear.app/getsentry/issue/PY-2043 Co-Authored-By: benry --- sentry_sdk/ai/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentry_sdk/ai/utils.py b/sentry_sdk/ai/utils.py index 8d88d4da5e..2103dc4778 100644 --- a/sentry_sdk/ai/utils.py +++ b/sentry_sdk/ai/utils.py @@ -485,7 +485,7 @@ def _normalize_data(data: "Any", unpack: bool = True) -> "Any": if isinstance(data, dict): return {k: _normalize_data(v, unpack=unpack) for (k, v) in data.items()} - return data if isinstance(data, (int, float, bool, str)) else str(data) + return data if data is None or isinstance(data, (int, float, bool, str)) else str(data) def set_data_normalized(