@@ -35,6 +35,18 @@ def test_is_supported_metric_value_excludes_booleans():
3535 assert not _is_supported_metric_value ("1" )
3636
3737
38+ def test_try_to_dict_uses_pydantic_model_dump_for_basemodel_instances ():
39+ pydantic = pytest .importorskip ("pydantic" )
40+
41+ class Usage (pydantic .BaseModel ):
42+ tokens : int
43+ cached_tokens : int
44+
45+ result = _try_to_dict (Usage (tokens = 3 , cached_tokens = 1 ))
46+
47+ assert result == {"tokens" : 3 , "cached_tokens" : 1 }
48+
49+
3850def test_try_to_dict_uses_to_dict_when_available ():
3951 class ToDictOnly :
4052 __slots__ = ("_payload" ,)
@@ -50,6 +62,31 @@ def to_dict(self):
5062 assert result == {"tokens" : 3 }
5163
5264
65+ def test_try_to_dict_falls_back_from_model_dump_python_to_bare_model_dump ():
66+ class BareModelDumpOnly :
67+ def model_dump (self , mode = None ):
68+ if mode == "python" :
69+ raise TypeError ("mode not supported" )
70+ return {"tokens" : 3 }
71+
72+ result = _try_to_dict (BareModelDumpOnly ())
73+
74+ assert result == {"tokens" : 3 }
75+
76+
77+ def test_try_to_dict_continues_past_non_dict_converter_results ():
78+ class MixedConverters :
79+ def model_dump (self , mode = None ):
80+ return [mode ]
81+
82+ def to_dict (self ):
83+ return {"tokens" : 3 }
84+
85+ result = _try_to_dict (MixedConverters ())
86+
87+ assert result == {"tokens" : 3 }
88+
89+
5390def test_try_to_dict_falls_back_to_vars_for_plain_objects ():
5491 class PlainObject :
5592 def __init__ (self ):
@@ -130,6 +167,16 @@ def test_convert_data_url_to_attachment_preserves_invalid_base64():
130167 assert converted == data_url
131168
132169
170+ def test_convert_data_url_to_attachment_uses_file_prefix_for_non_image_mime_types ():
171+ data_url = "data:application/pdf;base64,aGVsbG8="
172+
173+ attachment = _convert_data_url_to_attachment (data_url )
174+
175+ assert isinstance (attachment , Attachment )
176+ assert attachment .reference ["content_type" ] == "application/pdf"
177+ assert attachment .reference ["filename" ] == "file.pdf"
178+
179+
133180def test_convert_data_url_to_attachment_preserves_non_data_urls ():
134181 value = "https://example.com/image.png"
135182
0 commit comments