Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(rag): preserve embedded PowerPoint image media types (#3339)
Jingbo Chai8e661c987a4b
diff --git a/src/agentscope/rag/_parser/_ppt.py b/src/agentscope/rag/_parser/_ppt.pyindex 3e176f929..6d8f26bcd 100644--- a/src/agentscope/rag/_parser/_ppt.py+++ b/src/agentscope/rag/_parser/_ppt.py@@ -94,17 +94,18 @@ def _extract_table_rows(table: Any) -> list[list[str]]:return rows-def _extract_image_bytes(shape: Any) -> bytes | None:- """Return the embedded image bytes for a picture shape, or ``None``.+def _extract_image_data(shape: Any) -> tuple[bytes, str] | None:+ """Return a picture's bytes and MIME type, or ``None``.Args:shape (`Any`):A python-pptx shape. Non-picture shapes return ``None``.Returns:- `bytes | None`:- The raw image bytes, or ``None`` when ``shape`` is not a- picture / the bytes are unreadable.+ `tuple[bytes, str] | None`:+ The raw image bytes and their MIME type, falling back to+ byte inspection when image metadata is unavailable. Returns+ ``None`` when ``shape`` is not a picture or is unreadable."""from pptx.shapes.picture import Picture@@ -113,10 +114,17 @@ def _extract_image_bytes(shape: Any) -> bytes | None:if not isinstance(shape, Picture):return Nonetry:- return shape.image.blob+ image = shape.image+ image_bytes = image.blobexcept Exception as e: # pylint: disable=broad-exceptlogger.warning("Failed to extract image from PPT shape: %s", e)return None+ try:+ media_type = image.content_type+ except Exception: # pylint: disable=broad-except+ # MIME metadata may be unavailable even when the bytes are readable.+ media_type = None+ return image_bytes, media_type or _guess_image_media_type(image_bytes)class PPTParser(ParserBase):@@ -302,10 +310,10 @@ class PPTParser(ParserBase):for shape in _iter_shapes(slide.shapes):# 1. Pictures — flush running text, emit a DataBlock section.if self.include_image:- image_bytes = _extract_image_bytes(shape)- if image_bytes is not None:+ image_data = _extract_image_data(shape)+ if image_data is not None:+ image_bytes, media_type = image_dataflush_text()- media_type = _guess_image_media_type(image_bytes)data = base64.b64encode(image_bytes).decode("utf-8")slide_sections.append(Section(diff --git a/tests/rag_parser_test.py b/tests/rag_parser_test.pyindex 88eb46502..41f66d221 100644--- a/tests/rag_parser_test.py+++ b/tests/rag_parser_test.py@@ -113,6 +113,18 @@ def _make_pptx_rich() -> bytes:return buffer.getvalue()+def _make_pptx_with_image(image_bytes: bytes) -> bytes:+ """Build a PPTX containing a single picture on a blank slide."""+ from pptx import Presentation++ presentation = Presentation()+ slide = presentation.slides.add_slide(presentation.slide_layouts[6])+ slide.shapes.add_picture(io.BytesIO(image_bytes), 0, 0)+ buffer = io.BytesIO()+ presentation.save(buffer)+ return buffer.getvalue()++def _make_pptx_with_group() -> bytes:"""Build a PPTX whose slide holds a text box and a group of two boxes.@@ -1101,6 +1113,164 @@ class PPTParserTest(IsolatedAsyncioTestCase):],)+ async def test_tiff_image_media_type_for_bytes_and_path(self) -> None:+ """TIFF bytes retain their MIME type for both PPTX input forms."""+ import tempfile++ from PIL import Image+ from pptx import Presentation++ image_buffer = io.BytesIO()+ Image.new("RGB", (2, 2), color=(17, 34, 51)).save(+ image_buffer,+ format="TIFF",+ )+ image_bytes = image_buffer.getvalue()+ pptx_bytes = _make_pptx_with_image(image_bytes)+ embedded = (+ Presentation(io.BytesIO(pptx_bytes)).slides[0].shapes[0].image+ )+ self.assertEqual(+ {"content_type": embedded.content_type, "blob": embedded.blob},+ {"content_type": "image/tiff", "blob": image_bytes},+ )++ with tempfile.TemporaryDirectory() as directory:+ path = os.path.join(directory, "tiff.pptx")+ with open(path, "wb") as fp:+ fp.write(pptx_bytes)+ for input_type, pptx_input in (+ ("bytes", pptx_bytes),+ ("path", path),+ ):+ for include_image in (True, False):+ with self.subTest(+ input_type=input_type,+ include_image=include_image,+ ):+ sections = await PPTParser(+ include_image=include_image,+ slide_prefix=None,+ slide_suffix=None,+ ).parse(pptx_input, "tiff.pptx")+ self.assertEqual(+ [section.model_dump() for section in sections],+ [+ {+ "content": {+ "type": "data",+ "id": AnyString(),+ "created_at": AnyString(),+ "finished_at": None,+ "source": {+ "type": "base64",+ "data": base64.b64encode(+ image_bytes,+ ).decode("utf-8"),+ "media_type": "image/tiff",+ },+ "name": "tiff.pptx",+ },+ "source": "tiff.pptx",+ "metadata": {+ "slide": 1,+ "media_type": "image/tiff",+ },+ },+ ]+ if include_image+ else [],+ )++ async def test_image_media_type_falls_back_when_unavailable(self) -> None:+ """Missing or unreadable MIME information preserves readable images."""+ from unittest.mock import PropertyMock, patch++ from pptx.parts.image import Image++ pptx_bytes = _make_pptx_with_image(_PNG_PIXEL)+ for content_type in (+ None,+ "",+ AttributeError("missing content type"),+ ValueError("unreadable content type"),+ ):+ with self.subTest(content_type=content_type):+ with patch.object(+ Image,+ "content_type",+ new_callable=PropertyMock,+ ) as mocked_type:+ if isinstance(content_type, Exception):+ mocked_type.side_effect = content_type+ else:+ mocked_type.return_value = content_type+ sections = await PPTParser(+ slide_prefix=None,+ slide_suffix=None,+ ).parse(pptx_bytes, "fallback.pptx")+ self.assertEqual(+ [section.model_dump() for section in sections],+ [+ {+ "content": {+ "type": "data",+ "id": AnyString(),+ "created_at": AnyString(),+ "finished_at": None,+ "source": {+ "type": "base64",+ "data": _PNG_PIXEL_B64,+ "media_type": "image/png",+ },+ "name": "fallback.pptx",+ },+ "source": "fallback.pptx",+ "metadata": {+ "slide": 1,+ "media_type": "image/png",+ },+ },+ ],+ )++ async def test_unreadable_image_is_skipped(self) -> None:+ """Unreadable picture objects or image bytes keep warning and skip."""+ from unittest.mock import PropertyMock, patch++ from pptx.parts.image import Image+ from pptx.shapes.picture import Picture++ pptx_bytes = _make_pptx_with_image(_PNG_PIXEL)+ for target, attribute in ((Picture, "image"), (Image, "blob")):+ with self.subTest(attribute=attribute):+ with patch.object(+ target,+ attribute,+ new_callable=PropertyMock,+ side_effect=ValueError("unreadable image"),+ ):+ with self.assertLogs("as", level="WARNING") as captured:+ sections = await PPTParser(+ slide_prefix=None,+ slide_suffix=None,+ ).parse(pptx_bytes, "unreadable.pptx")+ self.assertEqual(+ {+ "sections": [+ section.model_dump() for section in sections+ ],+ "warnings": captured.output,+ },+ {+ "sections": [],+ "warnings": [+ "WARNING:as:Failed to extract image from PPT "+ "shape: unreadable image",+ ],+ },+ )+async def test_picture_placeholder_emits_data_block(self) -> None:"""Pictures inserted into layout placeholders keep their content."""from pptx import Presentation