Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(pdf): offload local conversion to thread (#2220)

xiejianqiao07dfde379402
diff --git a/openviking/parse/parsers/pdf.py b/openviking/parse/parsers/pdf.py
index 00b31bea7..e1f3b1c63 100644
--- a/openviking/parse/parsers/pdf.py
+++ b/openviking/parse/parsers/pdf.py
@@ -12,6 +12,7 @@ This design simplifies PDF handling by delegating structure analysis
to the MarkdownParser after conversion.
"""
+import asyncio
import re
import time
from collections import Counter, defaultdict
@@ -209,25 +210,16 @@ class PDFParser(BaseParser):
async def _convert_local(
self, pdf_path: Path, storage=None, resource_name: Optional[str] = None
) -> tuple[str, Dict[str, Any]]:
- """
- Convert PDF to Markdown using pdfplumber.
-
- When the PDF contains bookmarks/outlines, these are extracted and
- injected as markdown headings at the appropriate page positions.
- This allows MarkdownParser to build a hierarchical directory tree
- instead of producing flat numbered files.
-
- Args:
- pdf_path: Path to PDF file
- storage: Optional StoragePath for saving images
- resource_name: Resource name for organizing saved images
+ # pdfplumber / pdfminer 的解析与图片/表格提取通常是 CPU/IO 密集且为同步实现,
+ # 放到线程池中执行,避免阻塞事件循环。
+ return await asyncio.to_thread(self._convert_local_sync, pdf_path, storage, resource_name)
- Returns:
- Tuple of (markdown_content, metadata)
+ def _convert_local_sync(
+ self, pdf_path: Path, storage=None, resource_name: Optional[str] = None
+ ) -> tuple[str, Dict[str, Any]]:
+ """同步版:用 pdfplumber 将 PDF 转 Markdown。
- Raises:
- ImportError: If pdfplumber not installed
- Exception: If conversion fails
+ 该方法会在 :meth:`_convert_local` 中通过 asyncio.to_thread 调用。
"""
pdfplumber = lazy_import("pdfplumber")