Commit eb98edf
fix(ai): Handle Pydantic model classes in _normalize_data (#5143)
### 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
<!-- No specific issue linked - this was discovered during development
-->
---------
Co-authored-by: Ivana Kellyer <[email protected]>1 parent 5de66e2 commit eb98edf
2 files changed
+33
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
41 | 47 | | |
42 | 48 | | |
43 | 49 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
621 | 622 | | |
622 | 623 | | |
623 | 624 | | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
0 commit comments