fix(core): Permit OpenAI style blocks to be passed into convert_to_openai_messages (#31140)

Should effectively be a noop, just shouldn't throw

CC @madams0013

---------

Co-authored-by: ccurme <chester.curme@gmail.com>
This commit is contained in:
Jacob Lee 2025-05-07 07:57:37 -07:00 committed by GitHub
parent a15034d8d1
commit 66d1ed6099
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 1 deletions

View File

@ -1139,6 +1139,24 @@ def convert_to_openai_messages(
f"content block:\n\n{block}"
)
raise ValueError(err)
# OpenAI file format
elif (
block.get("type") == "file"
and isinstance(block.get("file"), dict)
and isinstance(block.get("file", {}).get("file_data"), str)
):
if block.get("file", {}).get("filename") is None:
logger.info("Generating a fallback filename.")
block["file"]["filename"] = "LC_AUTOGENERATED"
content.append(block)
# OpenAI audio format
elif (
block.get("type") == "input_audio"
and isinstance(block.get("input_audio"), dict)
and isinstance(block.get("input_audio", {}).get("data"), str)
and isinstance(block.get("input_audio", {}).get("format"), str)
):
content.append(block)
elif block.get("type") == "tool_use":
if missing := [
k for k in ("id", "name", "input") if k not in block

View File

@ -1209,6 +1209,13 @@ def test_convert_to_openai_messages_multimodal() -> None:
"mime_type": "application/pdf",
"filename": "test.pdf",
},
{
"type": "file",
"file": {
"filename": "draconomicon.pdf",
"file_data": "data:application/pdf;base64,<base64 string>",
},
},
{
"type": "file",
"source_type": "id",
@ -1220,13 +1227,20 @@ def test_convert_to_openai_messages_multimodal() -> None:
"data": "<base64 string>",
"mime_type": "audio/wav",
},
{
"type": "input_audio",
"input_audio": {
"data": "<base64 string>",
"format": "wav",
},
},
]
)
]
result = convert_to_openai_messages(messages, text_format="block")
assert len(result) == 1
message = result[0]
assert len(message["content"]) == 6
assert len(message["content"]) == 8
# Test adding filename
messages = [