Skip to content

fix(ai): normalize messages in truncate_messages_by_size to support Pydantic (#5350)#5730

Open
harryautomazione wants to merge 6 commits intogetsentry:masterfrom
harryautomazione:fix/openai-pydantic-5350
Open

fix(ai): normalize messages in truncate_messages_by_size to support Pydantic (#5350)#5730
harryautomazione wants to merge 6 commits intogetsentry:masterfrom
harryautomazione:fix/openai-pydantic-5350

Conversation

@harryautomazione
Copy link

What

  • Normalize messages in truncate_messages_by_size using _normalize_data(messages, unpack=False) before serializing them with json.dumps.
  • Pass normalized_messages onwards to _find_truncation_index to protect size lookups.

Why

How

  • Inside sentry_sdk/ai/utils.py:truncate_messages_by_size(), apply _normalize_data() before json.dumps().
  • Use the resulting dictionary structure for lengths and slicing returns instead of original structures.

Testing

  • Verified locally with manual mock class triggering a direct json.dumps exception.
  • Passed local integration test suite running pytest tests/integrations/openai/.

Checklist

  • Linked issue: Fixes #5350
  • No secrets introduced
  • Descriptions provided

…mand, parameter name, and attribute

- Change install command to include [pydantic_ai] extra
- Change result_type to output_type in example
- Change result.data to result.output in example

This ensures the docstring matches the current Pydantic AI API and correct installation instructions.

Fixes getsentry#5293
…gation

- Add CURRENT_LANGCHAIN_AGENT_NAME contextvar to track agent name across spans
- Set agent name in agent executor wrappers (invoke/stream)
- Propagate agent name to all child spans via _create_span
- Add test to verify agent name is set on all spans
@harryautomazione harryautomazione requested a review from a team as a code owner March 22, 2026 16:11
@github-actions
Copy link
Contributor

github-actions bot commented Mar 22, 2026

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

  • (asgi) Add option to disable suppressing chained exceptions by alexander-alderman-webb in #5714
  • (logging) Separate ignore lists for events/breadcrumbs and sentry logs by sl0thentr0py in #5698

Bug Fixes 🐛

Anthropic

  • Set exception info on streaming span when applicable by alexander-alderman-webb in #5683
  • Patch AsyncStream.close() and AsyncMessageStream.close() to finish spans by alexander-alderman-webb in #5675
  • Patch Stream.close() and MessageStream.close() to finish spans by alexander-alderman-webb in #5674

Other

Documentation 📚

  • Add note on AI PRs to CONTRIBUTING.md by sentrivana in #5696

Internal Changes 🔧

  • Add -latest alias for each integration test suite by sentrivana in #5706
  • Use date-based branch names for toxgen PRs by sentrivana in #5704
  • 🤖 Update test matrix with new releases (03/19) by github-actions in #5703
  • Add client report tests for span streaming by sentrivana in #5677

Other

  • Update CHANGELOG.md by sentrivana in #5685

🤖 This preview updates automatically when you update the PR.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normalization silently converts None values to string "None"

Medium Severity

_normalize_data converts None values to the string "None" (line 488's fallback: str(data)). OpenAI assistant messages with tool calls commonly have content: None. After this change, those become content: "None", altering the JSON representation from null to "None" — a semantic difference that also slightly skews the byte-size calculation used for truncation decisions.

Additional Locations (1)
Fix in Cursor Fix in Web

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAI Integration: truncate_messages_by_size fails on Pydantic SDK objects (e.g., ResponseFunctionToolCall)

1 participant