Skip to content

Conversation

@skalinchuk
Copy link
Contributor

Description

Previously, _normalize_data() would attempt to call model_dump() on Pydantic model classes (types) when they were passed as arguments, which would fail. This commonly occurs when model classes are used in schema definitions, such as when passing response format schemas to AI SDKs.

This change adds an inspect.isclass() check to detect when a class type is passed instead of an instance. For classes, we now return a string representation "<ClassType: ClassName>" instead of attempting to call model_dump().

Changes:

  • Added inspect import to sentry_sdk/ai/utils.py
  • Added class type detection in _normalize_data() before calling model_dump()
  • Added comprehensive test coverage for both class types and instances in tests/utils/test_general.py

Behavior:

  • Pydantic model instances: Still normalized via model_dump() (existing behavior)
  • Pydantic model classes: Now serialized as "<ClassType: ClassName>" (new behavior)
  • Mixed data structures: Classes within dicts/lists are properly handled

Issues

Previously, _normalize_data would attempt to call model_dump() on Pydantic
model classes (types) when they were passed as arguments, which would fail.
This commonly occurs when model classes are used in schema definitions.

This change adds an inspect.isclass() check to detect when a class type is
passed instead of an instance. For classes, we now return a string
representation "<ClassType: ClassName>" instead of attempting to call
model_dump().
@skalinchuk skalinchuk requested a review from a team as a code owner November 25, 2025 08:13
Copy link
Contributor

@sentrivana sentrivana left a comment

Choose a reason for hiding this comment

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

Thank you @skalinchuk!

@sentrivana sentrivana enabled auto-merge (squash) November 25, 2025 08:57
@sentrivana sentrivana merged commit eb98edf into getsentry:master Nov 25, 2025
142 of 144 checks passed
@codecov
Copy link

codecov bot commented Nov 25, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.02%. Comparing base (5de66e2) to head (88c5c55).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff             @@
##           master    #5143       +/-   ##
===========================================
+ Coverage   38.20%   84.02%   +45.82%     
===========================================
  Files         180      180               
  Lines       18222    18225        +3     
  Branches     2863     3236      +373     
===========================================
+ Hits         6961    15313     +8352     
+ Misses      10538     1922     -8616     
- Partials      723      990      +267     
Files with missing lines Coverage Δ
sentry_sdk/ai/utils.py 96.15% <100.00%> (+9.48%) ⬆️

... and 134 files with indirect coverage changes

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.

2 participants