Using langraph to parse a pdf into semenatic chunks
Project description
PDF Segmentation
PDF Segmentation is a utility for parsing PDFs into structured page-range chunks using LLMs, designed for downstream processing such as image conversion, question extraction, derivations, and document analysis.
It is intended to sit at the front of a PDF processing pipeline, handling segmentation and orchestration.
Features
- LLM-driven PDF segmentation into page ranges
- Schema-validated, structured outputs
- PDF → image conversion utilities
- PDF page and range splitting
- Multimodal (PDF + image) LLM support
Core Components
Segmentation Graph
from graph.graph import (
graph as PDFSegmentation,
Section,
ListOutput,
State as SegmentationInput,
)
PDFSegmentation– main graph entry pointSection– base class for output unitsListOutput[T]– typed LLM output containerState– input configuration for segmentation
The graph:
- Accepts a PDF (path or bytes)
- Invokes an LLM
- Returns schema-validated segmentation results
PDF Utilities
PDF → Images
from pdf_image_converter import PDFImageConverter
PDF Page Splitting
from pdf_seperator import PDFSeperator
Multimodal LLM Invocation
from pdf_llm import PDFMultiModalLLM
Example
Define an Output Schema
from typing import Literal, List
from pydantic import BaseModel
from graph.graph import Section, ListOutput
class MySection(Section, BaseModel):
title: str
description: str
section_type: Literal["derivation", "question"]
class MySections(ListOutput[MySection]):
items: List[MySection]
Run Segmentation
from pathlib import Path
from graph.graph import graph, State
result = graph.invoke(
State(
pdf=Path("data/Lecture_02_03.pdf"),
prompt="Extract the content into these chunks",
output_schema=MySections,
)
)
Input State
class State(BaseModel, Generic[T]):
pdf: str | Path
prompt: str
pdf_bytes: bytes | None = None
output_schema: Type[ListOutput[T]] = Field(exclude=True)
raw_output: List[T] = []
parsed: list[ParsedUnit[T]] = Field(default_factory=list)
@field_serializer("pdf_bytes")
def serialize_pdf_bytes(self, value: bytes):
return base64.b64encode(value).decode("ascii")
Notes
- Provide either
pdforpdf_bytes output_schemacontrols the LLM output structureparsedcontains validated segmentation results
Typical Pipeline
PDF → LLM Segmentation → Page Ranges
→ PDF Splitter
→ PDF → Image Conversion
→ Downstream Processing
Common use cases include lecture parsing, educational content generation, and document analysis.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pdf_segmentation-2.1.0.tar.gz.
File metadata
- Download URL: pdf_segmentation-2.1.0.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f9213c4146366eddd2d6c1eb81a9457b20c4a5e5fcda3584b10bc7b081e45baf
|
|
| MD5 |
16fb9827c7c86d4f23111791f9a683d2
|
|
| BLAKE2b-256 |
f561e9505aa9fe0319404661b2ca2cda3edb741cb69fb474d5e775ffb20e8eb6
|
File details
Details for the file pdf_segmentation-2.1.0-py3-none-any.whl.
File metadata
- Download URL: pdf_segmentation-2.1.0-py3-none-any.whl
- Upload date:
- Size: 12.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
673cfb4296b8d7a1420e36046edf024c37fd41b6b50caa642360a18c425d8ca8
|
|
| MD5 |
5f92dfaf738fb7f7cecfd2f9922ee8d7
|
|
| BLAKE2b-256 |
89a98b8f7f4ae6c8fa1fbfd582611c7e3c5eec75bdf4225b2dafbdb41ebf7597
|