Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(rag): preserve images in PowerPoint picture placeholders (#2851)
SIcheng Han87893fbe711a
diff --git a/src/agentscope/rag/_parser/_ppt.py b/src/agentscope/rag/_parser/_ppt.pyindex e3bae199e..4ad37b5f6 100644--- a/src/agentscope/rag/_parser/_ppt.py+++ b/src/agentscope/rag/_parser/_ppt.py@@ -90,17 +90,11 @@ def _extract_image_bytes(shape: Any) -> bytes | None:The raw image bytes, or ``None`` when ``shape`` is not apicture / the bytes are unreadable."""- try:- from pptx.enum.shapes import MSO_SHAPE_TYPE-- picture_type = MSO_SHAPE_TYPE.PICTURE- except ImportError:- # MSO_SHAPE_TYPE.PICTURE numeric value used as the fallback- # so the parser still works against pptx builds where the- # enum import path has moved.- picture_type = 13+ from pptx.shapes.picture import Picture- if shape.shape_type != picture_type:+ # PlaceholderPicture inherits Picture but reports PLACEHOLDER, not+ # PICTURE. Empty placeholders do not inherit Picture.+ if not isinstance(shape, Picture):return Nonetry:return shape.image.blobdiff --git a/tests/rag_parser_test.py b/tests/rag_parser_test.pyindex b304b9cc6..5676c22d9 100644--- a/tests/rag_parser_test.py+++ b/tests/rag_parser_test.py@@ -878,6 +878,67 @@ class PPTParserTest(IsolatedAsyncioTestCase):],)+ async def test_picture_placeholder_emits_data_block(self) -> None:+ """Pictures inserted into layout placeholders keep their content."""+ from pptx import Presentation++ presentation = Presentation()+ slide = presentation.slides.add_slide(presentation.slide_layouts[8])+ slide.shapes.title.text = "Before picture"+ slide.placeholders[1].insert_picture(io.BytesIO(_PNG_PIXEL))+ slide.placeholders[2].text = "After picture"+ # An unfilled picture placeholder must not be treated as an image.+ presentation.slides.add_slide(presentation.slide_layouts[8])+ buffer = io.BytesIO()+ presentation.save(buffer)++ parser = PPTParser(slide_prefix=None, slide_suffix=None)+ sections = await parser.parse(buffer.getvalue(), "placeholder.pptx")++ self.assertEqual(+ [s.model_dump() for s in sections],+ [+ {+ "content": {+ "type": "text",+ "text": "Before picture",+ "id": AnyString(),+ "created_at": AnyString(),+ "finished_at": None,+ },+ "source": "placeholder.pptx",+ "metadata": {"slide": 1},+ },+ {+ "content": {+ "type": "data",+ "id": AnyString(),+ "created_at": AnyString(),+ "finished_at": None,+ "source": {+ "type": "base64",+ "data": _PNG_PIXEL_B64,+ "media_type": "image/png",+ },+ "name": "placeholder.pptx",+ },+ "source": "placeholder.pptx",+ "metadata": {"slide": 1, "media_type": "image/png"},+ },+ {+ "content": {+ "type": "text",+ "text": "After picture",+ "id": AnyString(),+ "created_at": AnyString(),+ "finished_at": None,+ },+ "source": "placeholder.pptx",+ "metadata": {"slide": 1},+ },+ ],+ )+async def test_table_json_format(self) -> None:"""``table_format="json"`` emits the JSON marker payload."""pptx_bytes = _make_pptx_rich()