Skip to content

Commit 680756b

Browse files
committed
Added validation for items with multiple keys
1 parent 1a5dd96 commit 680756b

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

outlines/models/transformers.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -472,20 +472,29 @@ def _prepare_message(self, role: str, content: str | list) -> tuple[dict, list]:
472472
else:
473473
raise ValueError(
474474
f"Invalid content type: {type(content)}. "
475-
"The content must be a string or a list containing text and assets "
476-
"or a list of dict items with explicit types."
475+
+ "The content must be a string or a list containing text and assets "
476+
+ "or a list of dict items with explicit types."
477477
)
478478

479479
def _extract_assets_from_content(self, content: list) -> list:
480480
"""Process a list of dict items."""
481481
assets = []
482+
482483
for item in content:
484+
if len(item) > 2:
485+
raise ValueError(
486+
f"Found item with multiple keys: {item}. "
487+
+ "Each item in the content list must be a dictionary with a 'type' key and a single asset key. "
488+
+ "To include multiple assets, use separate dictionary items. "
489+
+ "For example: [{{'type': 'image', 'image': image1}}, {{'type': 'image', 'image': image2}}]. "
490+
)
491+
483492
if "type" not in item:
484493
raise ValueError(
485494
"Each item in the content list must be a dictionary with a 'type' key. "
486-
"Valid types are 'text', 'image', 'video', or 'audio'. "
487-
f"For instance {{'type': 'text', 'text': 'your message'}}. "
488-
f"Found item without 'type' key: {item}"
495+
+ "Valid types are 'text', 'image', 'video', or 'audio'. "
496+
+ "For instance {{'type': 'text', 'text': 'your message'}}. "
497+
+ f"Found item without 'type' key: {item}"
489498
)
490499
if item["type"] == "text":
491500
continue
@@ -494,7 +503,7 @@ def _extract_assets_from_content(self, content: list) -> list:
494503
if asset_key not in item:
495504
raise ValueError(
496505
f"Item with type '{asset_key}' must contain a '{asset_key}' key. "
497-
f"Found item: {item}"
506+
+ f"Found item: {item}"
498507
)
499508
if isinstance(item[asset_key], (Image, Video, Audio)):
500509
assets.append(item[asset_key])

0 commit comments

Comments
 (0)