litellm/litellm/videos/utils.py
Mateo Wang 98acc01e0d
feat(videos): add first-class OpenRouter video request fields (#50319)
* feat(videos): add first-class OpenRouter video request fields

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(videos): remove recursive image parameter fallback

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(videos): preserve EdenAI native parameter passthrough

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(videos): deduplicate frame roles in linear time

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(videos): preserve image selection and dropped parameter semantics

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(videos): preserve provider scalar parameters and raw extra body

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(videos): retain accepted scalar inputs for Vertex and DashScope

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-11-06 16:50:48 -08:00

102 lines
4.1 KiB
Python

from collections.abc import Mapping
from typing import Final, cast
import litellm
from litellm.llms.base_llm.videos.async_task_utils import OPENROUTER_VIDEO_FIELDS
from litellm.llms.base_llm.videos.transformation import BaseVideoConfig
from litellm.types.videos.main import VideoCreateOptionalRequestParams
from litellm.utils import filter_out_litellm_params
class VideoGenerationRequestUtils:
"""Helper utils for constructing video generation requests"""
@staticmethod
def get_optional_params_video_generation(
model: str,
video_generation_provider_config: BaseVideoConfig,
video_generation_optional_params: VideoCreateOptionalRequestParams,
drop_params: bool = False,
) -> dict:
"""
Get optional parameters for the video generation API.
Args:
model: The model name
video_generation_provider_config: The provider configuration for video generation API
video_generation_optional_params: The optional parameters for video generation
Returns:
A dictionary of supported parameters for the video generation API
"""
# Map parameters to provider-specific format
mapped_params: Final = video_generation_provider_config.map_openai_params(
video_create_optional_params=video_generation_optional_params,
model=model,
drop_params=drop_params or litellm.drop_params is True,
)
# Merge extra_body params if present (for provider-specific parameters)
if "extra_body" in video_generation_optional_params:
extra_body: Final = video_generation_optional_params["extra_body"]
if extra_body and isinstance(extra_body, dict):
# extra_body params override mapped params
mapped_params.update(extra_body)
# Remove extra_body from mapped_params since it's not sent to the API
mapped_params.pop("extra_body", None)
return mapped_params
@staticmethod
def get_requested_video_generation_optional_param(
params: Mapping[str, object],
) -> VideoCreateOptionalRequestParams:
"""
Filter parameters to only include those defined in VideoCreateOptionalRequestParams.
Args:
params: Dictionary of parameters to filter
Returns:
VideoCreateOptionalRequestParams instance with only the valid parameters
"""
params = dict(params or {})
nested_kwargs: Final = params.get("kwargs", {})
raw_kwargs: Final[dict[str, object]] = nested_kwargs if isinstance(nested_kwargs, dict) else {}
kwargs_extra_body: Final = raw_kwargs.pop("extra_body", None)
top_level_extra_body: Final = params.get("extra_body")
base_params_raw: Final = {
key: value
for key, value in params.items()
if key not in {"kwargs", "extra_body", "prompt", "model"} and value is not None
}
base_params: Final = filter_out_litellm_params(kwargs=base_params_raw)
cleaned_kwargs: Final = filter_out_litellm_params(kwargs={k: v for k, v in raw_kwargs.items() if v is not None})
optional_params: Final[dict[str, object]] = {
**base_params,
**cleaned_kwargs,
}
merged_extra_body: dict[str, object] = {}
for extra_body_candidate in (top_level_extra_body, kwargs_extra_body):
if isinstance(extra_body_candidate, dict):
for key, value in extra_body_candidate.items():
if value is not None:
merged_extra_body[key] = value
if merged_extra_body:
merged_extra_body = filter_out_litellm_params(kwargs=merged_extra_body)
if merged_extra_body:
optional_params["extra_body"] = merged_extra_body
optional_params.update(
{key: value for key, value in merged_extra_body.items() if key not in OPENROUTER_VIDEO_FIELDS}
)
optional_params.pop("timeout", None)
return cast(VideoCreateOptionalRequestParams, optional_params)