Skip to content
Closed
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
46 changes: 43 additions & 3 deletions libs/core/langchain_core/messages/block_translators/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,44 @@ def convert_to_openai_data_block(
else:
error_msg = "Key base64 is required for audio blocks."
raise ValueError(error_msg)
elif block["type"] == "video":
# Handle video blocks similar to audio/image blocks
if "base64" in block or block.get("source_type") == "base64":
# Handle v0 format: {"source_type": "base64", "data": "...", ...}
# Handle v1 format: {"base64": "...", ...}
base64_data = block["data"] if "source_type" in block else block["base64"]
video_format = block["mime_type"].split("/")[-1] # e.g., mp4, webm, avi

if api == "chat/completions":
# For Chat Completions API
formatted_block = {
"type": "video",
"video": {
"data": f"data:{block['mime_type']};base64,{base64_data}",
},
}
else:
# For Responses API
formatted_block = {
"type": "input_video",
"input_video": {"data": base64_data, "format": video_format},
}
elif "url" in block:
if api == "chat/completions":
# Chat Completions API doesn't support video URLs
error_msg = "OpenAI Chat Completions does not support video URLs."
raise ValueError(error_msg)
# Only supported by Responses API
formatted_block = {"type": "input_video", "video_url": block["url"]}
elif "file_id" in block or block.get("source_type") == "id":
# Handle video file IDs
file_id = block["id"] if "source_type" in block else block["file_id"]
formatted_block = {"type": "video", "video": {"file_id": file_id}}
if api == "responses":
formatted_block = {"type": "input_video", "file_id": file_id}
else:
error_msg = "Keys base64, url, or file_id required for video blocks."
raise ValueError(error_msg)
else:
error_msg = f"Block of type {block['type']} is not supported."
raise ValueError(error_msg)
Expand Down Expand Up @@ -172,9 +210,11 @@ def _convert_to_v1_from_chat_completions_input(

converted_blocks = []
unpacked_blocks: list[dict[str, Any]] = [
cast("dict[str, Any]", block)
if block.get("type") != "non_standard"
else block["value"] # type: ignore[typeddict-item] # this is only non-standard blocks
(
cast("dict[str, Any]", block)
if block.get("type") != "non_standard"
else cast("dict[str, Any]", block.get("value", block))
)
for block in content
]
for block in unpacked_blocks:
Expand Down