Skip to content

Commit 72970c2

Browse files
authored
Add all audio types supported by Gemini to AudioUrl (#2151)
1 parent 7da8cc6 commit 72970c2

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

pydantic_ai_slim/pydantic_ai/messages.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from .models.instrumented import InstrumentationSettings
2626

2727

28-
AudioMediaType: TypeAlias = Literal['audio/wav', 'audio/mpeg']
28+
AudioMediaType: TypeAlias = Literal['audio/wav', 'audio/mpeg', 'audio/ogg', 'audio/flac', 'audio/aiff', 'audio/aac']
2929
ImageMediaType: TypeAlias = Literal['image/jpeg', 'image/png', 'image/gif', 'image/webp']
3030
DocumentMediaType: TypeAlias = Literal[
3131
'application/pdf',
@@ -48,7 +48,7 @@
4848
'video/3gpp',
4949
]
5050

51-
AudioFormat: TypeAlias = Literal['wav', 'mp3']
51+
AudioFormat: TypeAlias = Literal['wav', 'mp3', 'oga', 'flac', 'aiff', 'aac']
5252
ImageFormat: TypeAlias = Literal['jpeg', 'png', 'gif', 'webp']
5353
DocumentFormat: TypeAlias = Literal['csv', 'doc', 'docx', 'html', 'md', 'pdf', 'txt', 'xls', 'xlsx']
5454
VideoFormat: TypeAlias = Literal['mkv', 'mov', 'mp4', 'webm', 'flv', 'mpeg', 'mpg', 'wmv', 'three_gp']
@@ -182,13 +182,25 @@ class AudioUrl(FileUrl):
182182

183183
@property
184184
def media_type(self) -> AudioMediaType:
185-
"""Return the media type of the audio file, based on the url."""
185+
"""Return the media type of the audio file, based on the url.
186+
187+
References:
188+
- Gemini: https://ai.google.dev/gemini-api/docs/audio#supported-formats
189+
"""
186190
if self.url.endswith('.mp3'):
187191
return 'audio/mpeg'
188-
elif self.url.endswith('.wav'):
192+
if self.url.endswith('.wav'):
189193
return 'audio/wav'
190-
else:
191-
raise ValueError(f'Unknown audio file extension: {self.url}')
194+
if self.url.endswith('.flac'):
195+
return 'audio/flac'
196+
if self.url.endswith('.oga'):
197+
return 'audio/ogg'
198+
if self.url.endswith('.aiff'):
199+
return 'audio/aiff'
200+
if self.url.endswith('.aac'):
201+
return 'audio/aac'
202+
203+
raise ValueError(f'Unknown audio file extension: {self.url}')
192204

193205
@property
194206
def format(self) -> AudioFormat:
@@ -358,6 +370,10 @@ class ToolReturn:
358370
_audio_format_lookup: dict[str, AudioFormat] = {
359371
'audio/mpeg': 'mp3',
360372
'audio/wav': 'wav',
373+
'audio/flac': 'flac',
374+
'audio/ogg': 'oga',
375+
'audio/aiff': 'aiff',
376+
'audio/aac': 'aac',
361377
}
362378
_image_format_lookup: dict[str, ImageFormat] = {
363379
'image/jpeg': 'jpeg',

tests/evals/test_otel.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ async def task2():
6565
task1_root = tree1.roots[0]
6666
assert len(task1_root.children) == 2, 'task1 should have exactly two children'
6767
task1_child_names = {child.name for child in task1_root.children}
68-
assert task1_child_names == {'task1_child1', 'task1_child2'}, (
69-
"task1's children should be task1_child1 and task1_child2"
70-
)
68+
assert task1_child_names == {
69+
'task1_child1',
70+
'task1_child2',
71+
}, "task1's children should be task1_child1 and task1_child2"
7172

7273
# Verify that tree2 only contains spans from task2
7374
assert len(tree2.roots) == 1, 'tree2 should have exactly one root span'

tests/test_messages.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ def test_binary_content_document(media_type: str, format: str):
125125
[
126126
pytest.param(AudioUrl('foobar.mp3'), 'audio/mpeg', 'mp3', id='mp3'),
127127
pytest.param(AudioUrl('foobar.wav'), 'audio/wav', 'wav', id='wav'),
128+
pytest.param(AudioUrl('foobar.oga'), 'audio/ogg', 'oga', id='oga'),
129+
pytest.param(AudioUrl('foobar.flac'), 'audio/flac', 'flac', id='flac'),
130+
pytest.param(AudioUrl('foobar.aiff'), 'audio/aiff', 'aiff', id='aiff'),
131+
pytest.param(AudioUrl('foobar.aac'), 'audio/aac', 'aac', id='aac'),
128132
],
129133
)
130134
def test_audio_url(audio_url: AudioUrl, media_type: str, format: str):

0 commit comments

Comments
 (0)