Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions sentry_sdk/ai/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inspect
import json
from collections import deque
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -38,6 +39,11 @@ def _normalize_data(data, unpack=True):
# type: (Any, bool) -> Any
# convert pydantic data (e.g. OpenAI v1+) to json compatible format
if hasattr(data, "model_dump"):
# Check if it's a class (type) rather than an instance
# Model classes can be passed as arguments (e.g., for schema definitions)
if inspect.isclass(data):
return f"<ClassType: {data.__name__}>"

try:
return _normalize_data(data.model_dump(), unpack=unpack)
except Exception as e:
Expand Down
26 changes: 26 additions & 0 deletions tests/utils/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os

import pytest
from sentry_sdk.ai.utils import _normalize_data


from sentry_sdk.utils import (
Expand Down Expand Up @@ -621,3 +622,28 @@ def test_failed_base64_conversion(input):
)
def test_strip_string(input, max_length, result):
assert strip_string(input, max_length) == result


def test_normalize_data_with_pydantic_class():
"""Test that _normalize_data handles Pydantic model classes"""
class TestClass:
name: str = None

def __init__(self, name: str):
self.name = name

def model_dump(self):
return {"name": self.name}

# Test with class (should NOT call model_dump())
result = _normalize_data(TestClass)
assert result == "<ClassType: TestClass>"

# Test with instance (should call model_dump())
instance = TestClass(name="test")
result = _normalize_data(instance)
assert result == {"name": "test"}

# Test with dict containing class
result = _normalize_data({"schema": TestClass, "count": 5})
assert result == {"schema": "<ClassType: TestClass>", "count": 5}
Loading