[Bugfix][Processor] Replace bare asserts with ValueError in DeepseekVLV2/OCR processors (#53854)

Signed-off-by: adisivaprasad <adisivaprasad@users.noreply.github.com>
Co-authored-by: adisivaprasad <adisivaprasad@users.noreply.github.com>
This commit is contained in:
adisivaprasad
2026-08-26 09:10:54 +00:00
committed by GitHub
co-authored by adisivaprasad
parent 2267d3b112
commit 73bd7c83c4
3 changed files with 42 additions and 8 deletions
@@ -132,3 +132,27 @@ class TestDeepseekOCREmptyImagesCrop:
"image_size": 1024, # Wrong! Tensor has 640
},
)
class TestDeepseekOCRInputValidation:
"""Bare ``assert`` used to validate user input here returned HTTP 500
(AssertionError) instead of HTTP 400 (ValueError) and disappeared under
``python -O``, silently corrupting the tokenized sequence. See #53850.
"""
def test_mismatched_image_count_raises_value_error(self, processor):
"""Two ``<image>`` tokens with one image must raise ValueError, not
AssertionError (which would yield HTTP 500 and, under ``-O``, silently
drop the middle text segment)."""
image = Image.new("RGB", (100, 100), color="red")
with pytest.raises(ValueError, match="does not match number of images"):
processor(prompt="<image> and <image>", images=[image])
def test_missing_prompt_raises_value_error(self, processor):
image = Image.new("RGB", (100, 100), color="red")
with pytest.raises(ValueError, match="prompt and images"):
processor(prompt=None, images=[image])
def test_missing_images_raises_value_error(self, processor):
with pytest.raises(ValueError, match="prompt and images"):
processor(prompt="<image>\nDescribe this image.", images=None)
@@ -250,9 +250,8 @@ class DeepseekOCRProcessor(ProcessorMixin):
- num_image_tokens (List[int]): the number of image tokens
"""
assert prompt is not None and images is not None, (
"prompt and images must be used at the same time."
)
if prompt is None or images is None:
raise ValueError("prompt and images must be used at the same time.")
sft_format = prompt
@@ -311,7 +310,13 @@ class DeepseekOCRProcessor(ProcessorMixin):
):
"""Tokenize text with <image> tags."""
assert conversation.count(self.image_token) == len(images)
num_image_tags = conversation.count(self.image_token)
if num_image_tags != len(images):
raise ValueError(
f"Number of {self.image_token!r} tokens in prompt "
f"({num_image_tags}) does not match number of images "
f"({len(images)})."
)
text_splits = conversation.split(self.image_token)
images_list, images_crop_list, images_seq_mask, images_spatial_crop = (
[],
@@ -203,9 +203,8 @@ class DeepseekVLV2Processor(ProcessorMixin):
- num_image_tokens (list[int]): the number of image tokens
"""
assert prompt is not None and images is not None, (
"prompt and images must be used at the same time."
)
if prompt is None or images is None:
raise ValueError("prompt and images must be used at the same time.")
sft_format = prompt
(
@@ -310,7 +309,13 @@ class DeepseekVLV2Processor(ProcessorMixin):
cropping: bool = True,
):
"""Tokenize text with <image> tags."""
assert conversation.count(self.image_token) == len(images)
num_image_tags = conversation.count(self.image_token)
if num_image_tags != len(images):
raise ValueError(
f"Number of {self.image_token!r} tokens in prompt "
f"({num_image_tags}) does not match number of images "
f"({len(images)})."
)
text_splits = conversation.split(self.image_token)
images_list, images_seq_mask, images_spatial_crop = [], [], []
num_image_tokens = []