Repository navigation
Conversation
Contributor
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="astrbot/core/knowledge_base/parsers/pdf_parser.py" line_range="73" />
<code_context>
)
except Exception:
- # 单个图片提取失败不影响整体
+ # A broken image must not hide other images on the page.
continue
except Exception:
</code_context>
<issue_to_address>
**Image failures go unnoticed**
When pypdf or Pillow raises while retrieving or encoding an image, the per-image `except Exception` discards the failed image and returns the remaining media without recording the exception, so the upload succeeds with missing media and operators have no indication extraction failed.
Log the image/page context and exception before continuing so extraction remains isolated but failures are observable.
</issue_to_address>Sourcery assessment
Approval pending. 1 finding to address first.
Blocking findings: astrbot/core/knowledge_base/parsers/pdf_parser.py:73
| ) | ||
| except Exception: | ||
| # 单个图片提取失败不影响整体 | ||
| # A broken image must not hide other images on the page. |
Contributor
There was a problem hiding this comment.
🟡 Medium · Image failures go unnoticed
When pypdf or Pillow raises while retrieving or encoding an image, the per-image except Exception discards the failed image and returns the remaining media without recording the exception, so the upload succeeds with missing media and operators have no indication extraction failed.
Log the image/page context and exception before continuing so extraction remains isolated but failures are observable.
Prompt for AI agents
In `astrbot/core/knowledge_base/parsers/pdf_parser.py` at line 73:
**Image failures go unnoticed**
When pypdf or Pillow raises while retrieving or encoding an image, the per-image `except Exception` discards the failed image and returns the remaining media without recording the exception, so the upload succeeds with missing media and operators have no indication extraction failed.
Log the image/page context and exception before continuing so extraction remains isolated but failures are observable.This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PDFParser currently saves
XObject.get_data()as image bytes and guesses the file type from a single/Filtervalue. For Flate-compressed images those bytes are raw pixels, so the resulting ".png" cannot be opened. A JPEG with an ASCII85 + DCT filter chain is saved as ".png" withimage/pngmetadata.Modifications
page.imagesAPI to obtain encoded image bytes, including soft masks and images inside Form XObjects.No new dependencies or API/schema changes. Previously stored media are not rewritten; affected documents need to be parsed again.
Screenshots or Test Results
Verified locally on Windows with Python 3.12:
tests/test_pdf_parser.py— 5 failed, 2 passed. PNG decoding fails withUnidentifiedImageError, JPEG metadata is incorrect, and nested images are omitted.uv run pytest tests/test_pdf_parser.py -q— 7 passed with pypdf 6.21.2.uv run --with pypdf==6.1.1 python -m pytest tests/test_pdf_parser.py -q— 7 passed.tests/test_pdf_parser.py tests/test_epub_parser.py tests/unit/test_kb_upload_atomicity.py tests/unit/test_kb_document_cleanup.py tests/unit/test_knowledge_base_service_contract.py.Run with the same
pytest.main(...)/ flushedos._exit(...)wrapper used byscripts/run_pytests_ci.shbecause existing database test worker threads can keep the process alive after completion.uv run ruff format .— 524 files unchanged.uv run ruff check .— passed.uvx --from pyupgrade==3.21.0 pyupgrade --py310-plus astrbot/core/knowledge_base/parsers/pdf_parser.py tests/test_pdf_parser.pyandgit diff --check— passed.The full repository suite was not run locally.
Checklist
Summary by Sourcery
Fix PDF image extraction so stored media remains decodable and correctly identified across supported image layouts.
Bug Fixes:
Tests: