fix(qwen3_vl): require truthy value in untyped image_part fallback (#46)

This commit is contained in:
eligotts
2026-05-18 12:09:58 +02:00
committed by GitHub
parent 17d0584f87
commit 25ba413d46
2 changed files with 48 additions and 4 deletions
+11 -4
View File
@@ -70,9 +70,14 @@ def _is_image_part(item: Any) -> bool:
t = item.get("type")
if t in ("image", "image_url"):
return True
# Permissive fallback: chat templates check ``'image' in content`` to
# accept loosely-shaped image parts, so mirror that.
return "image" in item or "image_url" in item
if t is not None:
return False
# Untyped fallback for loosely-shaped image parts. Require a truthy
# value: HF Arrow schema unification (Dataset.from_list over a list of
# heterogeneous content dicts) fills missing keys with None, so any
# text part round-tripped through a Dataset will have ``image_url: None``
# as a key. Mere key presence isn't enough.
return bool(item.get("image")) or bool(item.get("image_url"))
def _is_video_part(item: Any) -> bool:
@@ -81,7 +86,9 @@ def _is_video_part(item: Any) -> bool:
t = item.get("type")
if t in ("video", "video_url"):
return True
return "video" in item or "video_url" in item
if t is not None:
return False
return bool(item.get("video")) or bool(item.get("video_url"))
def _load_pil_image(item: dict[str, Any]):
+37
View File
@@ -695,3 +695,40 @@ def test_qwen3_vl_renderer_exposes_image_modality():
renderer = create_renderer(tokenizer, renderer="auto")
assert isinstance(renderer, Qwen3VLRenderer)
assert "image" in MULTIMODAL_MODELS[model]
def test_is_image_part_treats_type_field_as_authoritative():
"""``Dataset.from_list`` unifies the Arrow schema across the elements
of a list-typed column. A content list mixing text and image parts
round-trips with ``image_url: None`` added to every text part (and
``text: None`` added to every image part). The classifier must treat
the ``type`` field as authoritative when present — falling back to
a key-presence check on ``image_url`` would misclassify the text
part and the renderer would later raise on ``_load_pil_image(None)``.
"""
from renderers.qwen3_vl import _is_image_part, _is_video_part
# Typed parts classify by their ``type``.
assert _is_image_part(
{"type": "image_url", "image_url": {"url": "data:image/png;base64,XXX"}}
)
assert _is_image_part({"type": "image", "image": object()})
assert _is_video_part(
{"type": "video_url", "video_url": {"url": "data:video/mp4;base64,XXX"}}
)
# Schema-unified text parts — typed as text, with a None zombie key
# for the sibling modality — must NOT classify as image / video.
schema_unified_text = {"type": "text", "text": "hello", "image_url": None}
assert not _is_image_part(schema_unified_text)
assert not _is_video_part(schema_unified_text)
schema_unified_text_with_video = {"type": "text", "text": "hi", "video_url": None}
assert not _is_video_part(schema_unified_text_with_video)
# Untyped fallback only fires when ``type`` is absent, and requires
# a truthy value (mere key presence isn't enough).
assert _is_image_part({"image_url": {"url": "data:..."}})
assert _is_image_part({"image": object()})
assert not _is_image_part({"image_url": None})
assert not _is_image_part({"image": None})
assert not _is_video_part({"video_url": None})