Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
69 changes: 26 additions & 43 deletions tests/entrypoints/openai/test_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,35 @@ def base64_encoded_audio() -> dict[str, str]:
}


@pytest.mark.asyncio
@pytest.mark.parametrize("model_name", [MODEL_NAME])
@pytest.mark.parametrize("audio_url", [TEST_AUDIO_URLS[0]])
async def test_single_chat_session_audio(
client: openai.AsyncOpenAI, model_name: str, audio_url: str
def dummy_messages_from_audio_url(
audio_urls: str | list[str],
content_text: str = "What's happening in this audio?",
):
messages = [
if isinstance(audio_urls, str):
audio_urls = [audio_urls]

return [
{
"role": "user",
"content": [
{"type": "audio_url", "audio_url": {"url": audio_url}},
{"type": "text", "text": "What's happening in this audio?"},
*(
{"type": "audio_url", "audio_url": {"url": audio_url}}
for audio_url in audio_urls
),
{"type": "text", "text": content_text},
],
}
]
Comment on lines +56 to 74
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

While this helper function is a good step towards reducing code duplication within this file, similar helper functions (dummy_messages_from_video_url, dummy_messages_from_image_url) have been added to test_video.py and test_vision.py, which are nearly identical. This introduces a new form of code duplication across test files.

To further improve maintainability, consider creating a single, more generic helper function in a shared location like tests/utils.py. This function could take the media type as an argument.

For example, you could have a function in tests/utils.py:

def dummy_messages_from_media_url(
    media_type: str,
    media_urls: str | list[str],
    content_text: str,
):
    if isinstance(media_urls, str):
        media_urls = [media_urls]

    media_key = f"{media_type}_url"
    return [
        {
            "role": "user",
            "content": [
                *(
                    {"type": media_key, media_key: {"url": media_url}}
                    for media_url in media_urls
                ),
                {"type": "text", "text": content_text},
            ],
        }
    ]

Then, in this file, you could use it like this:

from tests.utils import dummy_messages_from_media_url

# ...

messages = dummy_messages_from_media_url(
    "audio",
    audio_url,
    "What's happening in this audio?"
)

This would eliminate the duplicated helper functions and make the test suite easier to maintain. The default text can be passed from each test file.



@pytest.mark.asyncio
@pytest.mark.parametrize("model_name", [MODEL_NAME])
@pytest.mark.parametrize("audio_url", [TEST_AUDIO_URLS[0]])
async def test_single_chat_session_audio(
client: openai.AsyncOpenAI, model_name: str, audio_url: str
):
messages = dummy_messages_from_audio_url(audio_url)

# test single completion
chat_completion = await client.chat.completions.create(
model=model_name,
Expand Down Expand Up @@ -138,20 +151,9 @@ async def test_single_chat_session_audio_base64encoded(
audio_url: str,
base64_encoded_audio: dict[str, str],
):
messages = [
{
"role": "user",
"content": [
{
"type": "audio_url",
"audio_url": {
"url": f"data:audio/wav;base64,{base64_encoded_audio[audio_url]}" # noqa: E501
},
},
{"type": "text", "text": "What's happening in this audio?"},
],
}
]
messages = dummy_messages_from_audio_url(
f"data:audio/wav;base64,{base64_encoded_audio[audio_url]}"
)

# test single completion
chat_completion = await client.chat.completions.create(
Expand Down Expand Up @@ -252,15 +254,7 @@ async def test_single_chat_session_input_audio(
async def test_chat_streaming_audio(
client: openai.AsyncOpenAI, model_name: str, audio_url: str
):
messages = [
{
"role": "user",
"content": [
{"type": "audio_url", "audio_url": {"url": audio_url}},
{"type": "text", "text": "What's happening in this audio?"},
],
}
]
messages = dummy_messages_from_audio_url(audio_url)

# test single completion
chat_completion = await client.chat.completions.create(
Expand Down Expand Up @@ -365,18 +359,7 @@ async def test_chat_streaming_input_audio(
async def test_multi_audio_input(
client: openai.AsyncOpenAI, model_name: str, audio_urls: list[str]
):
messages = [
{
"role": "user",
"content": [
*(
{"type": "audio_url", "audio_url": {"url": audio_url}}
for audio_url in audio_urls
),
{"type": "text", "text": "What's happening in this audio?"},
],
}
]
messages = dummy_messages_from_audio_url(audio_urls)

if len(audio_urls) > MAXIMUM_AUDIOS:
with pytest.raises(openai.BadRequestError): # test multi-audio input
Expand Down
97 changes: 31 additions & 66 deletions tests/entrypoints/openai/test_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,35 @@ def base64_encoded_video() -> dict[str, str]:
}


@pytest.mark.asyncio
@pytest.mark.parametrize("model_name", [MODEL_NAME])
@pytest.mark.parametrize("video_url", TEST_VIDEO_URLS)
async def test_single_chat_session_video(
client: openai.AsyncOpenAI, model_name: str, video_url: str
def dummy_messages_from_video_url(
video_urls: str | list[str],
content_text: str = "What's in this video?",
):
messages = [
if isinstance(video_urls, str):
video_urls = [video_urls]

return [
{
"role": "user",
"content": [
{"type": "video_url", "video_url": {"url": video_url}},
{"type": "text", "text": "What's in this video?"},
*(
{"type": "video_url", "video_url": {"url": video_url}}
for video_url in video_urls
),
{"type": "text", "text": content_text},
],
}
]


@pytest.mark.asyncio
@pytest.mark.parametrize("model_name", [MODEL_NAME])
@pytest.mark.parametrize("video_url", TEST_VIDEO_URLS)
async def test_single_chat_session_video(
client: openai.AsyncOpenAI, model_name: str, video_url: str
):
messages = dummy_messages_from_video_url(video_url)

# test single completion
chat_completion = await client.chat.completions.create(
model=model_name,
Expand Down Expand Up @@ -137,15 +150,7 @@ async def test_error_on_invalid_video_url_type(
async def test_single_chat_session_video_beamsearch(
client: openai.AsyncOpenAI, model_name: str, video_url: str
):
messages = [
{
"role": "user",
"content": [
{"type": "video_url", "video_url": {"url": video_url}},
{"type": "text", "text": "What's in this video?"},
],
}
]
messages = dummy_messages_from_video_url(video_url)

chat_completion = await client.chat.completions.create(
model=model_name,
Expand All @@ -172,20 +177,9 @@ async def test_single_chat_session_video_base64encoded(
video_url: str,
base64_encoded_video: dict[str, str],
):
messages = [
{
"role": "user",
"content": [
{
"type": "video_url",
"video_url": {
"url": f"data:video/jpeg;base64,{base64_encoded_video[video_url]}" # noqa: E501
},
},
{"type": "text", "text": "What's in this video?"},
],
}
]
messages = dummy_messages_from_video_url(
f"data:video/jpeg;base64,{base64_encoded_video[video_url]}"
)

# test single completion
chat_completion = await client.chat.completions.create(
Expand Down Expand Up @@ -231,20 +225,10 @@ async def test_single_chat_session_video_base64encoded_beamsearch(
video_url: str,
base64_encoded_video: dict[str, str],
):
messages = [
{
"role": "user",
"content": [
{
"type": "video_url",
"video_url": {
"url": f"data:video/jpeg;base64,{base64_encoded_video[video_url]}" # noqa: E501
},
},
{"type": "text", "text": "What's in this video?"},
],
}
]
messages = dummy_messages_from_video_url(
f"data:video/jpeg;base64,{base64_encoded_video[video_url]}"
)

chat_completion = await client.chat.completions.create(
model=model_name,
messages=messages,
Expand All @@ -265,15 +249,7 @@ async def test_single_chat_session_video_base64encoded_beamsearch(
async def test_chat_streaming_video(
client: openai.AsyncOpenAI, model_name: str, video_url: str
):
messages = [
{
"role": "user",
"content": [
{"type": "video_url", "video_url": {"url": video_url}},
{"type": "text", "text": "What's in this video?"},
],
}
]
messages = dummy_messages_from_video_url(video_url)

# test single completion
chat_completion = await client.chat.completions.create(
Expand Down Expand Up @@ -318,18 +294,7 @@ async def test_chat_streaming_video(
async def test_multi_video_input(
client: openai.AsyncOpenAI, model_name: str, video_urls: list[str]
):
messages = [
{
"role": "user",
"content": [
*(
{"type": "video_url", "video_url": {"url": video_url}}
for video_url in video_urls
),
{"type": "text", "text": "What's in this video?"},
],
}
]
messages = dummy_messages_from_video_url(video_urls)

if len(video_urls) > MAXIMUM_VIDEOS:
with pytest.raises(openai.BadRequestError): # test multi-video input
Expand Down
Loading