[Fix] Add max_depth guard to BFL _read_image_bytes recursive function

Use the standard depth/max_depth pattern with DEFAULT_MAX_RECURSE_DEPTH
to guard the recursive list-unwrapping in _read_image_bytes, matching
the existing pattern used by _read_all_bytes in vertex_imagen.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
yuneng-jiang 2026-03-12 13:22:56 -07:00
parent 553cfc6d72
commit a93c069dd5
2 changed files with 15 additions and 8 deletions

View File

@ -14,6 +14,7 @@ from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union
import httpx
from httpx._types import RequestFiles
from litellm.constants import DEFAULT_MAX_RECURSE_DEPTH
from litellm.llms.base_llm.image_edit.transformation import BaseImageEditConfig
from litellm.secret_managers.main import get_secret_str
from litellm.types.images.main import ImageEditOptionalRequestParams
@ -189,17 +190,22 @@ class BlackForestLabsImageEditConfig(BaseImageEditConfig):
endpoint = self._get_model_endpoint(model)
return f"{base_url}{endpoint}"
def _read_image_bytes(self, image: Any) -> bytes:
def _read_image_bytes(
self,
image: Any,
depth: int = 0,
max_depth: int = DEFAULT_MAX_RECURSE_DEPTH,
) -> bytes:
"""Read image bytes from various input types."""
# Unwrap nested lists iteratively to avoid recursion
for _ in range(10):
if isinstance(image, list):
image = image[0]
else:
break
if depth > max_depth:
raise ValueError(
f"Max recursion depth {max_depth} reached while reading image bytes for Black Forest Labs image edit."
)
if isinstance(image, bytes):
return image
elif isinstance(image, list):
# If it's a list, take the first image
return self._read_image_bytes(image[0], depth=depth + 1, max_depth=max_depth)
elif isinstance(image, str):
if image.startswith(("http://", "https://")):
# Download image from URL

View File

@ -44,6 +44,7 @@ IGNORE_FUNCTIONS = [
"extract_text_from_a2a_message", # max depth set (default 10) to prevent infinite recursion in A2A message parsing.
"_convert_to_json_serializable_dict", # max depth set (default 20) and circular reference protection to prevent infinite recursion.
"dict", # max depth set. _LiteLLMParamsDictView.dict() calls builtin dict(), not itself.
"_read_image_bytes", # max depth set.
]