fix(ai): normalize SDK objects before truncating messages#5779
fix(ai): normalize SDK objects before truncating messages#5779saschabuehrle wants to merge 1 commit intogetsentry:masterfrom
Conversation
Semver Impact of This PR🟢 Patch (bug fixes) 📋 Changelog PreviewThis is how your changes will appear in the changelog. New Features ✨
Bug Fixes 🐛Anthropic
Other
Documentation 📚
Internal Changes 🔧
Other
🤖 This preview updates automatically when you update the PR. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
| truncation is based only on character count in that case. | ||
| """ | ||
| serialized_json = json.dumps(messages, separators=(",", ":")) | ||
| normalized_messages = _normalize_data(messages, unpack=False) |
There was a problem hiding this comment.
Normalization converts None values to string "None"
High Severity
Calling _normalize_data(messages, unpack=False) before truncation corrupts None values in message dicts. The _normalize_data fallback at line 488 converts any non-primitive (including None) to str(data), turning None into the string "None". This is very common in practice — OpenAI assistant messages with tool calls have "content": null. The existing parameterized test with content=None will also fail since it asserts result[0]["content"] is content.


Fixes #5350
truncate_messages_by_size()currently callsjson.dumps()on the rawmessagesinput. That breaks when SDK/Pydantic objects (e.g. OpenAI response tool call objects) are present.This change normalizes messages first via
_normalize_data(..., unpack=False)before doing any JSON size calculations/truncation logic.It also adds a regression test covering a pydantic-like object with
model_dump().Greetings, saschabuehrle