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
28 changes: 28 additions & 0 deletions src/agents/models/chatcmpl_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from openai.types.chat import (
ChatCompletionAssistantMessageParam,
ChatCompletionContentPartImageParam,
ChatCompletionContentPartInputAudioParam,
ChatCompletionContentPartParam,
ChatCompletionContentPartTextParam,
ChatCompletionDeveloperMessageParam,
Expand All @@ -27,6 +28,7 @@
ResponseFileSearchToolCallParam,
ResponseFunctionToolCall,
ResponseFunctionToolCallParam,
ResponseInputAudioParam,
ResponseInputContentParam,
ResponseInputFileParam,
ResponseInputImageParam,
Expand Down Expand Up @@ -287,6 +289,32 @@ def extract_all_content(
},
)
)
elif isinstance(c, dict) and c.get("type") == "input_audio":
casted_audio_param = cast(ResponseInputAudioParam, c)
audio_payload = casted_audio_param.get("input_audio")
if not audio_payload:
raise UserError(
f"Only audio data is supported for input_audio {casted_audio_param}"
)
if not isinstance(audio_payload, dict):
raise UserError(
f"input_audio must provide audio data and format {casted_audio_param}"
)
audio_data = audio_payload.get("data")
audio_format = audio_payload.get("format")
if not audio_data or not audio_format:
raise UserError(
f"input_audio requires both data and format {casted_audio_param}"
)
out.append(
ChatCompletionContentPartInputAudioParam(
type="input_audio",
input_audio={
"data": audio_data,
"format": audio_format,
},
)
)
elif isinstance(c, dict) and c.get("type") == "input_file":
casted_file_param = cast(ResponseInputFileParam, c)
if "file_data" not in casted_file_param or not casted_file_param["file_data"]:
Expand Down
31 changes: 31 additions & 0 deletions tests/test_openai_chatcompletions_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from openai.types.responses import (
ResponseFunctionToolCall,
ResponseFunctionToolCallParam,
ResponseInputAudioParam,
ResponseInputTextParam,
ResponseOutputMessage,
ResponseOutputRefusal,
Expand Down Expand Up @@ -280,6 +281,36 @@ def test_extract_all_and_text_content_for_strings_and_lists():
assert [p["text"] for p in text_parts] == ["one", "two"]


def test_extract_all_content_handles_input_audio():
"""
input_audio entries should translate into ChatCompletion input_audio parts.
"""
audio: ResponseInputAudioParam = {
"type": "input_audio",
"input_audio": {"data": "AAA=", "format": "wav"},
}
parts = Converter.extract_all_content([audio])
assert isinstance(parts, list)
assert parts == [
{
"type": "input_audio",
"input_audio": {"data": "AAA=", "format": "wav"},
}
]


def test_extract_all_content_rejects_invalid_input_audio():
"""
input_audio requires both data and format fields to be present.
"""
audio_missing_data = cast(ResponseInputAudioParam, {
"type": "input_audio",
"input_audio": {"format": "wav"},
})
with pytest.raises(UserError):
Converter.extract_all_content([audio_missing_data])


def test_items_to_messages_handles_system_and_developer_roles():
"""
Roles other than `user` (e.g. `system` and `developer`) need to be
Expand Down