@@ -85,7 +85,7 @@ def otel_event(self, settings: InstrumentationSettings) -> Event:
85
85
__repr__ = _utils .dataclasses_no_defaults_repr
86
86
87
87
88
- @dataclass (repr = False )
88
+ @dataclass (init = False , repr = False )
89
89
class FileUrl (ABC ):
90
90
"""Abstract base class for any URL-based file."""
91
91
@@ -106,11 +106,29 @@ class FileUrl(ABC):
106
106
- `GoogleModel`: `VideoUrl.vendor_metadata` is used as `video_metadata`: https://ai.google.dev/gemini-api/docs/video-understanding#customize-video-processing
107
107
"""
108
108
109
- @property
109
+ _media_type : str | None = field (init = False , repr = False )
110
+
111
+ def __init__ (
112
+ self ,
113
+ url : str ,
114
+ force_download : bool = False ,
115
+ vendor_metadata : dict [str , Any ] | None = None ,
116
+ media_type : str | None = None ,
117
+ ) -> None :
118
+ self .url = url
119
+ self .vendor_metadata = vendor_metadata
120
+ self .force_download = force_download
121
+ self ._media_type = media_type
122
+
110
123
@abstractmethod
111
- def media_type (self ) -> str :
124
+ def _infer_media_type (self ) -> str :
112
125
"""Return the media type of the file, based on the url."""
113
126
127
+ @property
128
+ def media_type (self ) -> str :
129
+ """Return the media type of the file, based on the url or the provided `_media_type`."""
130
+ return self ._media_type or self ._infer_media_type ()
131
+
114
132
@property
115
133
@abstractmethod
116
134
def format (self ) -> str :
@@ -119,7 +137,7 @@ def format(self) -> str:
119
137
__repr__ = _utils .dataclasses_no_defaults_repr
120
138
121
139
122
- @dataclass (repr = False )
140
+ @dataclass (init = False , repr = False )
123
141
class VideoUrl (FileUrl ):
124
142
"""A URL to a video."""
125
143
@@ -129,8 +147,18 @@ class VideoUrl(FileUrl):
129
147
kind : Literal ['video-url' ] = 'video-url'
130
148
"""Type identifier, this is available on all parts as a discriminator."""
131
149
132
- @property
133
- def media_type (self ) -> VideoMediaType :
150
+ def __init__ (
151
+ self ,
152
+ url : str ,
153
+ force_download : bool = False ,
154
+ vendor_metadata : dict [str , Any ] | None = None ,
155
+ media_type : str | None = None ,
156
+ kind : Literal ['video-url' ] = 'video-url' ,
157
+ ) -> None :
158
+ super ().__init__ (url = url , force_download = force_download , vendor_metadata = vendor_metadata , media_type = media_type )
159
+ self .kind = kind
160
+
161
+ def _infer_media_type (self ) -> VideoMediaType :
134
162
"""Return the media type of the video, based on the url."""
135
163
if self .url .endswith ('.mkv' ):
136
164
return 'video/x-matroska'
@@ -170,7 +198,7 @@ def format(self) -> VideoFormat:
170
198
return _video_format_lookup [self .media_type ]
171
199
172
200
173
- @dataclass (repr = False )
201
+ @dataclass (init = False , repr = False )
174
202
class AudioUrl (FileUrl ):
175
203
"""A URL to an audio file."""
176
204
@@ -180,8 +208,18 @@ class AudioUrl(FileUrl):
180
208
kind : Literal ['audio-url' ] = 'audio-url'
181
209
"""Type identifier, this is available on all parts as a discriminator."""
182
210
183
- @property
184
- def media_type (self ) -> AudioMediaType :
211
+ def __init__ (
212
+ self ,
213
+ url : str ,
214
+ force_download : bool = False ,
215
+ vendor_metadata : dict [str , Any ] | None = None ,
216
+ media_type : str | None = None ,
217
+ kind : Literal ['audio-url' ] = 'audio-url' ,
218
+ ) -> None :
219
+ super ().__init__ (url = url , force_download = force_download , vendor_metadata = vendor_metadata , media_type = media_type )
220
+ self .kind = kind
221
+
222
+ def _infer_media_type (self ) -> AudioMediaType :
185
223
"""Return the media type of the audio file, based on the url.
186
224
187
225
References:
@@ -208,7 +246,7 @@ def format(self) -> AudioFormat:
208
246
return _audio_format_lookup [self .media_type ]
209
247
210
248
211
- @dataclass (repr = False )
249
+ @dataclass (init = False , repr = False )
212
250
class ImageUrl (FileUrl ):
213
251
"""A URL to an image."""
214
252
@@ -218,8 +256,18 @@ class ImageUrl(FileUrl):
218
256
kind : Literal ['image-url' ] = 'image-url'
219
257
"""Type identifier, this is available on all parts as a discriminator."""
220
258
221
- @property
222
- def media_type (self ) -> ImageMediaType :
259
+ def __init__ (
260
+ self ,
261
+ url : str ,
262
+ force_download : bool = False ,
263
+ vendor_metadata : dict [str , Any ] | None = None ,
264
+ media_type : str | None = None ,
265
+ kind : Literal ['image-url' ] = 'image-url' ,
266
+ ) -> None :
267
+ super ().__init__ (url = url , force_download = force_download , vendor_metadata = vendor_metadata , media_type = media_type )
268
+ self .kind = kind
269
+
270
+ def _infer_media_type (self ) -> ImageMediaType :
223
271
"""Return the media type of the image, based on the url."""
224
272
if self .url .endswith (('.jpg' , '.jpeg' )):
225
273
return 'image/jpeg'
@@ -241,7 +289,7 @@ def format(self) -> ImageFormat:
241
289
return _image_format_lookup [self .media_type ]
242
290
243
291
244
- @dataclass (repr = False )
292
+ @dataclass (init = False , repr = False )
245
293
class DocumentUrl (FileUrl ):
246
294
"""The URL of the document."""
247
295
@@ -251,8 +299,18 @@ class DocumentUrl(FileUrl):
251
299
kind : Literal ['document-url' ] = 'document-url'
252
300
"""Type identifier, this is available on all parts as a discriminator."""
253
301
254
- @property
255
- def media_type (self ) -> str :
302
+ def __init__ (
303
+ self ,
304
+ url : str ,
305
+ force_download : bool = False ,
306
+ vendor_metadata : dict [str , Any ] | None = None ,
307
+ media_type : str | None = None ,
308
+ kind : Literal ['document-url' ] = 'document-url' ,
309
+ ) -> None :
310
+ super ().__init__ (url = url , force_download = force_download , vendor_metadata = vendor_metadata , media_type = media_type )
311
+ self .kind = kind
312
+
313
+ def _infer_media_type (self ) -> str :
256
314
"""Return the media type of the document, based on the url."""
257
315
type_ , _ = guess_type (self .url )
258
316
if type_ is None :
0 commit comments