Skip to content
18 changes: 18 additions & 0 deletions litellm/llms/base_llm/videos/transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,23 @@ def transform_video_get_character_response(
"video get character is not supported for this provider"
)

def get_video_edit_prefetch_params(
self,
video_id: str,
api_base: str,
litellm_params: GenericLiteLLMParams,
headers: dict,
) -> Optional[Tuple[str, Dict]]:
"""
Return (url, body) for a pre-fetch HTTP call that must be made before
transform_video_edit_request, or None if no pre-fetch is required.

Providers that need to retrieve the source video before constructing the
edit request (e.g. Vertex AI) should override this method. The handler
uses the existing shared httpx client so the call is properly async.
"""
return None

def transform_video_edit_request(
self,
prompt: str,
Expand All @@ -329,6 +346,7 @@ def transform_video_edit_request(
litellm_params: GenericLiteLLMParams,
headers: dict,
extra_body: Optional[Dict[str, Any]] = None,
prefetched_source_data: Optional[Dict[str, Any]] = None,
) -> Tuple[str, Dict]:
"""
Transform the video edit request into a URL and JSON data.
Expand Down
55 changes: 55 additions & 0 deletions litellm/llms/custom_httpx/llm_http_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6560,6 +6560,7 @@ def video_remix_handler(
api_key=api_key or litellm_params.get("api_key", None),
headers=extra_headers or {},
model="",
litellm_params=litellm_params,
)

if extra_headers:
Expand Down Expand Up @@ -6642,6 +6643,7 @@ async def async_video_remix_handler(
api_key=api_key or litellm_params.get("api_key", None),
headers=extra_headers or {},
model="",
litellm_params=litellm_params,
)

if extra_headers:
Expand Down Expand Up @@ -6734,6 +6736,7 @@ def video_create_character_handler(
api_key=api_key or litellm_params.get("api_key", None),
headers=extra_headers or {},
model="",
litellm_params=litellm_params,
)
if extra_headers:
headers.update(extra_headers)
Expand Down Expand Up @@ -6805,6 +6808,7 @@ async def async_video_create_character_handler(
api_key=api_key or litellm_params.get("api_key", None),
headers=extra_headers or {},
model="",
litellm_params=litellm_params,
)
if extra_headers:
headers.update(extra_headers)
Expand Down Expand Up @@ -6888,6 +6892,7 @@ def video_get_character_handler(
api_key=api_key or litellm_params.get("api_key", None),
headers=extra_headers or {},
model="",
litellm_params=litellm_params,
)
if extra_headers:
headers.update(extra_headers)
Expand Down Expand Up @@ -6945,6 +6950,7 @@ async def async_video_get_character_handler(
api_key=api_key or litellm_params.get("api_key", None),
headers=extra_headers or {},
model="",
litellm_params=litellm_params,
)
if extra_headers:
headers.update(extra_headers)
Expand Down Expand Up @@ -7021,6 +7027,7 @@ def video_edit_handler(
api_key=api_key or litellm_params.get("api_key", None),
headers=extra_headers or {},
model="",
litellm_params=litellm_params,
)
if extra_headers:
headers.update(extra_headers)
Expand All @@ -7031,13 +7038,35 @@ def video_edit_handler(
litellm_params=dict(litellm_params),
)

prefetched_source_data = None
prefetch_params = video_provider_config.get_video_edit_prefetch_params(
video_id=video_id,
api_base=api_base,
litellm_params=litellm_params,
headers=headers,
)
if prefetch_params is not None:
prefetch_url, prefetch_body = prefetch_params
try:
prefetch_resp = sync_httpx_client.post(
url=prefetch_url,
headers=headers,
json=prefetch_body,
timeout=timeout,
)
prefetch_resp.raise_for_status()
except Exception as e:
raise self._handle_error(e=e, provider_config=video_provider_config)
prefetched_source_data = prefetch_resp.json()

url, data = video_provider_config.transform_video_edit_request(
prompt=prompt,
video_id=video_id,
api_base=api_base,
litellm_params=litellm_params,
headers=headers,
extra_body=extra_body,
prefetched_source_data=prefetched_source_data,
)

logging_obj.pre_call(
Expand Down Expand Up @@ -7093,6 +7122,7 @@ async def async_video_edit_handler(
api_key=api_key or litellm_params.get("api_key", None),
headers=extra_headers or {},
model="",
litellm_params=litellm_params,
)
if extra_headers:
headers.update(extra_headers)
Expand All @@ -7103,13 +7133,35 @@ async def async_video_edit_handler(
litellm_params=dict(litellm_params),
)

prefetched_source_data = None
prefetch_params = video_provider_config.get_video_edit_prefetch_params(
video_id=video_id,
api_base=api_base,
litellm_params=litellm_params,
headers=headers,
)
if prefetch_params is not None:
prefetch_url, prefetch_body = prefetch_params
try:
prefetch_resp = await async_httpx_client.post(
url=prefetch_url,
headers=headers,
json=prefetch_body,
timeout=timeout,
)
prefetch_resp.raise_for_status()
except Exception as e:
raise self._handle_error(e=e, provider_config=video_provider_config)
prefetched_source_data = prefetch_resp.json()

url, data = video_provider_config.transform_video_edit_request(
prompt=prompt,
video_id=video_id,
api_base=api_base,
litellm_params=litellm_params,
headers=headers,
extra_body=extra_body,
prefetched_source_data=prefetched_source_data,
)

logging_obj.pre_call(
Expand Down Expand Up @@ -7182,6 +7234,7 @@ def video_extension_handler(
api_key=api_key or litellm_params.get("api_key", None),
headers=extra_headers or {},
model="",
litellm_params=litellm_params,
)
if extra_headers:
headers.update(extra_headers)
Expand Down Expand Up @@ -7256,6 +7309,7 @@ async def async_video_extension_handler(
api_key=api_key or litellm_params.get("api_key", None),
headers=extra_headers or {},
model="",
litellm_params=litellm_params,
)
if extra_headers:
headers.update(extra_headers)
Expand Down Expand Up @@ -7467,6 +7521,7 @@ async def async_video_delete_handler(
api_key=api_key,
headers=extra_headers or {},
model="",
litellm_params=litellm_params,
)

if extra_headers:
Expand Down
9 changes: 8 additions & 1 deletion litellm/llms/gemini/videos/transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,14 @@ def transform_video_get_character_response(self, raw_response, logging_obj):
raise NotImplementedError("video get character is not supported for Gemini")

def transform_video_edit_request(
self, prompt, video_id, api_base, litellm_params, headers, extra_body=None
self,
prompt,
video_id,
api_base,
litellm_params,
headers,
extra_body=None,
prefetched_source_data=None,
):
raise NotImplementedError("video edit is not supported for Gemini")

Expand Down
1 change: 1 addition & 0 deletions litellm/llms/openai/videos/transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ def transform_video_edit_request(
litellm_params: GenericLiteLLMParams,
headers: dict,
extra_body: Optional[Dict[str, Any]] = None,
prefetched_source_data: Optional[Dict[str, Any]] = None,
) -> Tuple[str, Dict]:
original_video_id = extract_original_video_id(video_id)
url = f"{api_base.rstrip('/')}/edits"
Expand Down
9 changes: 8 additions & 1 deletion litellm/llms/runwayml/videos/transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,14 @@ def transform_video_get_character_response(self, raw_response, logging_obj):
raise NotImplementedError("video get character is not supported for RunwayML")

def transform_video_edit_request(
self, prompt, video_id, api_base, litellm_params, headers, extra_body=None
self,
prompt,
video_id,
api_base,
litellm_params,
headers,
extra_body=None,
prefetched_source_data=None,
):
raise NotImplementedError("video edit is not supported for RunwayML")

Expand Down
119 changes: 113 additions & 6 deletions litellm/llms/vertex_ai/videos/transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,15 +647,122 @@ def transform_video_get_character_request(
def transform_video_get_character_response(self, raw_response, logging_obj):
raise NotImplementedError("video get character is not supported for Vertex AI")

def get_video_edit_prefetch_params(
self,
video_id: str,
api_base: str,
litellm_params: GenericLiteLLMParams,
headers: dict,
) -> Tuple[str, Dict]:
"""Return the fetchPredictOperation URL and body needed to retrieve the source video."""
operation_name = extract_original_video_id(video_id)
model = self.extract_model_from_operation_name(operation_name)

if not model:
raise ValueError(
f"Cannot extract model from operation name: {operation_name}. "
"Expected format: projects/PROJECT/locations/LOCATION/publishers/google/models/MODEL/operations/OPERATION_ID"
)

fetch_url = f"{api_base.rstrip('/')}/{model}:fetchPredictOperation"
return fetch_url, {"operationName": operation_name}
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

def transform_video_edit_request(
self, prompt, video_id, api_base, litellm_params, headers, extra_body=None
):
raise NotImplementedError("video edit is not supported for Vertex AI")
self,
prompt: str,
video_id: str,
api_base: str,
litellm_params: GenericLiteLLMParams,
headers: dict,
extra_body: Optional[Dict[str, Any]] = None,
prefetched_source_data: Optional[Dict[str, Any]] = None,
) -> Tuple[str, Dict]:
"""
Build a predictLongRunning edit request from the pre-fetched source video.

The actual fetchPredictOperation HTTP call is hoisted into the handler so
it can use the shared async/sync httpx client instead of blocking the loop.
"""
if prefetched_source_data is None:
raise ValueError(
"prefetched_source_data is required for Vertex AI video edit. "
"Ensure get_video_edit_prefetch_params is called by the handler."
)

if not prefetched_source_data.get("done", False):
raise ValueError(
"Source video generation is not complete yet. "
"Check the video status before editing."
)

videos = prefetched_source_data.get("response", {}).get("videos", [])
if not videos:
raise ValueError("No videos found in the completed operation. Cannot edit.")

source_video = videos[0]
video_input: Dict[str, Any] = {}
if "gcsUri" in source_video:
video_input["gcsUri"] = source_video["gcsUri"]
elif "bytesBase64Encoded" in source_video:
video_input["bytesBase64Encoded"] = source_video["bytesBase64Encoded"]
video_input["mimeType"] = source_video.get("mimeType", "video/mp4")
else:
raise ValueError(
"Source video has neither gcsUri nor bytesBase64Encoded. Cannot edit."
)

operation_name = extract_original_video_id(video_id)
model = self.extract_model_from_operation_name(operation_name) or ""

instance_dict: Dict[str, Any] = {"prompt": prompt, "video": video_input}
request_data: Dict[str, Any] = {"instances": [instance_dict]}

if extra_body:
extra_body_copy = dict(extra_body)
nested_params = extra_body_copy.pop("parameters", None)
vertex_params: Dict[str, Any] = {}
if isinstance(nested_params, dict):
vertex_params.update(nested_params)
vertex_params.update(extra_body_copy)
if vertex_params:
request_data["parameters"] = vertex_params
Comment thread
Sameerlite marked this conversation as resolved.
Resolved

edit_url = f"{api_base.rstrip('/')}/{model}:predictLongRunning"
return edit_url, request_data

def transform_video_edit_response(
self, raw_response, logging_obj, custom_llm_provider=None
):
raise NotImplementedError("video edit is not supported for Vertex AI")
self,
raw_response: httpx.Response,
logging_obj: LiteLLMLoggingObj,
custom_llm_provider: Optional[str] = None,
) -> VideoObject:
"""
Transform the Veo video edit response.

Veo returns the same operation response as video generation:
{"name": "projects/.../operations/OPERATION_ID"}
"""
response_data = raw_response.json()

operation_name = response_data.get("name")
if not operation_name:
raise ValueError(f"No operation name in Veo edit response: {response_data}")

model = self.extract_model_from_operation_name(operation_name) or ""

if custom_llm_provider:
video_id = encode_video_id_with_provider(
operation_name, custom_llm_provider, model
)
else:
video_id = operation_name

return VideoObject(
Comment thread
veria-ai[bot] marked this conversation as resolved.
Resolved
id=video_id,
object="video",
status="processing",
model=model,
)

def transform_video_extension_request(
self,
Expand Down
Loading
Loading