API Reference¶
Factory Function¶
create_extractor¶
The main entry point for creating extractors.
unifex.create_extractor ¶
create_extractor(
path: Path | str,
extractor_type: ExtractorType,
*,
languages: list[str] | None = None,
dpi: int = 200,
use_gpu: bool = False,
credentials: dict[str, str] | None = None,
output_unit: CoordinateUnit = CoordinateUnit.POINTS,
character_merger: str | None = None,
) -> BaseExtractor
Create an extractor by type with unified parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Path to document/image file (Path object or string). |
required |
extractor_type
|
ExtractorType
|
ExtractorType enum value specifying which extractor to use: - ExtractorType.PDF - Native PDF extraction - ExtractorType.EASYOCR - EasyOCR for images and PDFs (auto-detects) - ExtractorType.TESSERACT - Tesseract for images and PDFs (auto-detects) - ExtractorType.PADDLE - PaddleOCR for images and PDFs (auto-detects) - ExtractorType.AZURE_DI - Azure Document Intelligence - ExtractorType.GOOGLE_DOCAI - Google Document AI |
required |
languages
|
list[str] | None
|
Language codes for OCR (default: ["en"]). EasyOCR/Tesseract use full list, PaddleOCR uses first language. |
None
|
dpi
|
int
|
DPI for PDF-to-image conversion (default: 200). |
200
|
use_gpu
|
bool
|
Enable GPU acceleration where supported (default: False). |
False
|
credentials
|
dict[str, str] | None
|
Override credentials dict. If None, reads from env vars: - UNIFEX_AZURE_DI_ENDPOINT, UNIFEX_AZURE_DI_KEY for Azure - UNIFEX_GOOGLE_DOCAI_PROCESSOR_NAME, UNIFEX_GOOGLE_DOCAI_CREDENTIALS_PATH for Google |
None
|
output_unit
|
CoordinateUnit
|
Coordinate unit for output (default: POINTS). - CoordinateUnit.POINTS - 1/72 inch (PDF native, resolution-independent) - CoordinateUnit.PIXELS - Pixels at the specified DPI - CoordinateUnit.INCHES - Imperial inches - CoordinateUnit.NORMALIZED - 0-1 range relative to page dimensions |
POINTS
|
character_merger
|
str | None
|
Character merger strategy for PDF extractor (default: basic-line). - "basic-line" - Merge characters into lines - "keep-char" - Keep each character as separate TextBlock |
None
|
Returns:
| Type | Description |
|---|---|
BaseExtractor
|
Configured extractor instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If extractor_type is invalid or required credentials are missing. |
Example
from unifex import create_extractor, ExtractorType, CoordinateUnit with create_extractor("doc.pdf", ExtractorType.PDF) as ext: ... doc = ext.extract() # Coordinates in points (default) with create_extractor("doc.pdf", ExtractorType.EASYOCR, ... output_unit=CoordinateUnit.PIXELS) as ext: ... doc = ext.extract() # Coordinates in pixels
Source code in unifex/text_factory.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
Extractor Types¶
ExtractorType¶
Enum for available extractor types.
unifex.ExtractorType ¶
Coordinate Units¶
CoordinateUnit¶
Enum for coordinate output units.
unifex.CoordinateUnit ¶
Bases: StrEnum
Units for coordinate output.
Source code in unifex/base/models.py
Executor Types¶
ExecutorType¶
Enum for parallel execution modes.
unifex.base.ExecutorType ¶
LLM Extraction¶
extract_structured¶
Extract structured data from a document using an LLM (single request).
unifex.llm_factory.extract_structured ¶
extract_structured(
path: Path | str,
model: str,
*,
schema: type[T],
prompt: str | None = None,
pages: list[int] | None = None,
dpi: int = 200,
max_retries: int = 3,
temperature: float = 0.0,
credentials: dict[str, str] | None = None,
base_url: str | None = None,
headers: dict[str, str] | None = None,
_extractor: Any = None,
) -> LLMExtractionResult[T]
extract_structured(
path: Path | str,
model: str,
*,
schema: None = None,
prompt: str | None = None,
pages: list[int] | None = None,
dpi: int = 200,
max_retries: int = 3,
temperature: float = 0.0,
credentials: dict[str, str] | None = None,
base_url: str | None = None,
headers: dict[str, str] | None = None,
_extractor: Any = None,
) -> LLMExtractionResult[dict[str, Any]]
extract_structured(
path: Path | str,
model: str,
*,
schema: type[T] | None = None,
prompt: str | None = None,
pages: list[int] | None = None,
dpi: int = 200,
max_retries: int = 3,
temperature: float = 0.0,
credentials: dict[str, str] | None = None,
base_url: str | None = None,
headers: dict[str, str] | None = None,
_extractor: SingleExtractor[T] | None = None,
) -> LLMExtractionResult[T | dict[str, Any]]
Extract structured data from a document using an LLM.
All specified pages are sent in a single request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Path to document/image file. |
required |
model
|
str
|
Model identifier (e.g., "openai/gpt-4o", "anthropic/claude-3-5-sonnet"). |
required |
schema
|
type[T] | None
|
Pydantic model for structured output. None for free-form dict. |
None
|
prompt
|
str | None
|
Custom extraction prompt. Auto-generated from schema if None. |
None
|
pages
|
list[int] | None
|
Page numbers to extract from (0-indexed). None for all pages. |
None
|
dpi
|
int
|
DPI for PDF-to-image conversion. |
200
|
max_retries
|
int
|
Max retry attempts with validation feedback. |
3
|
temperature
|
float
|
Sampling temperature (0.0 = deterministic). |
0.0
|
credentials
|
dict[str, str] | None
|
Override credentials dict (otherwise uses env vars). |
None
|
base_url
|
str | None
|
Custom API base URL for OpenAI-compatible APIs (vLLM, Ollama, etc.). |
None
|
headers
|
dict[str, str] | None
|
Custom HTTP headers for OpenAI-compatible APIs. |
None
|
_extractor
|
SingleExtractor[T] | None
|
Internal parameter for dependency injection (testing only). |
None
|
Returns:
| Type | Description |
|---|---|
LLMExtractionResult[T | dict[str, Any]]
|
LLMExtractionResult containing extracted data, |
LLMExtractionResult[T | dict[str, Any]]
|
model info, and provider. |
Source code in unifex/llm_factory.py
extract_structured_async¶
Async version of extract_structured.
unifex.llm_factory.extract_structured_async
async
¶
extract_structured_async(
path: Path | str,
model: str,
*,
schema: type[T],
prompt: str | None = None,
pages: list[int] | None = None,
dpi: int = 200,
max_retries: int = 3,
temperature: float = 0.0,
credentials: dict[str, str] | None = None,
base_url: str | None = None,
headers: dict[str, str] | None = None,
_extractor: Any = None,
) -> LLMExtractionResult[T]
extract_structured_async(
path: Path | str,
model: str,
*,
schema: None = None,
prompt: str | None = None,
pages: list[int] | None = None,
dpi: int = 200,
max_retries: int = 3,
temperature: float = 0.0,
credentials: dict[str, str] | None = None,
base_url: str | None = None,
headers: dict[str, str] | None = None,
_extractor: Any = None,
) -> LLMExtractionResult[dict[str, Any]]
extract_structured_async(
path: Path | str,
model: str,
*,
schema: type[T] | None = None,
prompt: str | None = None,
pages: list[int] | None = None,
dpi: int = 200,
max_retries: int = 3,
temperature: float = 0.0,
credentials: dict[str, str] | None = None,
base_url: str | None = None,
headers: dict[str, str] | None = None,
_extractor: AsyncSingleExtractor[T] | None = None,
) -> LLMExtractionResult[T | dict[str, Any]]
Async version of extract_structured.
All specified pages are sent in a single request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Path to document/image file. |
required |
model
|
str
|
Model identifier (e.g., "openai/gpt-4o", "anthropic/claude-3-5-sonnet"). |
required |
schema
|
type[T] | None
|
Pydantic model for structured output. None for free-form dict. |
None
|
prompt
|
str | None
|
Custom extraction prompt. Auto-generated from schema if None. |
None
|
pages
|
list[int] | None
|
Page numbers to extract from (0-indexed). None for all pages. |
None
|
dpi
|
int
|
DPI for PDF-to-image conversion. |
200
|
max_retries
|
int
|
Max retry attempts with validation feedback. |
3
|
temperature
|
float
|
Sampling temperature (0.0 = deterministic). |
0.0
|
credentials
|
dict[str, str] | None
|
Override credentials dict (otherwise uses env vars). |
None
|
base_url
|
str | None
|
Custom API base URL for OpenAI-compatible APIs (vLLM, Ollama, etc.). |
None
|
headers
|
dict[str, str] | None
|
Custom HTTP headers for OpenAI-compatible APIs. |
None
|
_extractor
|
AsyncSingleExtractor[T] | None
|
Internal parameter for dependency injection (testing only). |
None
|
Returns:
| Type | Description |
|---|---|
LLMExtractionResult[T | dict[str, Any]]
|
LLMExtractionResult containing extracted data, |
LLMExtractionResult[T | dict[str, Any]]
|
model info, and provider. |
Source code in unifex/llm_factory.py
extract_structured_parallel¶
Extract structured data in parallel (one page per request).
unifex.llm_factory.extract_structured_parallel ¶
extract_structured_parallel(
path: Path | str,
model: str,
*,
schema: type[T],
prompt: str | None = None,
pages: list[int] | None = None,
max_workers: int = 4,
executor: ExecutorType = ExecutorType.THREAD,
dpi: int = 200,
max_retries: int = 3,
temperature: float = 0.0,
credentials: dict[str, str] | None = None,
base_url: str | None = None,
headers: dict[str, str] | None = None,
_extractor: Any = None,
) -> LLMBatchExtractionResult[T]
extract_structured_parallel(
path: Path | str,
model: str,
*,
schema: None = None,
prompt: str | None = None,
pages: list[int] | None = None,
max_workers: int = 4,
executor: ExecutorType = ExecutorType.THREAD,
dpi: int = 200,
max_retries: int = 3,
temperature: float = 0.0,
credentials: dict[str, str] | None = None,
base_url: str | None = None,
headers: dict[str, str] | None = None,
_extractor: Any = None,
) -> LLMBatchExtractionResult[dict[str, Any]]
extract_structured_parallel(
path: Path | str,
model: str,
*,
schema: type[T] | None = None,
prompt: str | None = None,
pages: list[int] | None = None,
max_workers: int = 4,
executor: ExecutorType = ExecutorType.THREAD,
dpi: int = 200,
max_retries: int = 3,
temperature: float = 0.0,
credentials: dict[str, str] | None = None,
base_url: str | None = None,
headers: dict[str, str] | None = None,
_extractor: SingleExtractor[T] | None = None,
) -> LLMBatchExtractionResult[T | dict[str, Any]]
Extract structured data from a document in parallel (one page per request).
Each page is extracted in a separate request, allowing parallel processing. Errors on individual pages are captured in the result, not raised.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Path to document/image file. |
required |
model
|
str
|
Model identifier (e.g., "openai/gpt-4o", "anthropic/claude-3-5-sonnet"). |
required |
schema
|
type[T] | None
|
Pydantic model for structured output. None for free-form dict. |
None
|
prompt
|
str | None
|
Custom extraction prompt. Auto-generated from schema if None. |
None
|
pages
|
list[int] | None
|
Page numbers to extract from (0-indexed). None for all pages. |
None
|
max_workers
|
int
|
Number of parallel workers. |
4
|
executor
|
ExecutorType
|
Type of executor (THREAD or PROCESS) for parallel extraction. |
THREAD
|
dpi
|
int
|
DPI for PDF-to-image conversion. |
200
|
max_retries
|
int
|
Max retry attempts with validation feedback. |
3
|
temperature
|
float
|
Sampling temperature (0.0 = deterministic). |
0.0
|
credentials
|
dict[str, str] | None
|
Override credentials dict (otherwise uses env vars). |
None
|
base_url
|
str | None
|
Custom API base URL for OpenAI-compatible APIs (vLLM, Ollama, etc.). |
None
|
headers
|
dict[str, str] | None
|
Custom HTTP headers for OpenAI-compatible APIs. |
None
|
_extractor
|
SingleExtractor[T] | None
|
Internal parameter for dependency injection (testing only). |
None
|
Returns:
| Type | Description |
|---|---|
LLMBatchExtractionResult[T | dict[str, Any]]
|
LLMBatchExtractionResult containing |
LLMBatchExtractionResult[T | dict[str, Any]]
|
per-page PageExtractionResult with data or errors. |
LLMBatchExtractionResult[T | dict[str, Any]]
|
Results are guaranteed to be in the same order as the input pages, |
LLMBatchExtractionResult[T | dict[str, Any]]
|
i.e., |
Source code in unifex/llm_factory.py
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 | |
extract_structured_parallel_async¶
Async parallel extraction.
unifex.llm_factory.extract_structured_parallel_async
async
¶
extract_structured_parallel_async(
path: Path | str,
model: str,
*,
schema: type[T],
prompt: str | None = None,
pages: list[int] | None = None,
max_workers: int = 4,
dpi: int = 200,
max_retries: int = 3,
temperature: float = 0.0,
credentials: dict[str, str] | None = None,
base_url: str | None = None,
headers: dict[str, str] | None = None,
_extractor: Any = None,
) -> LLMBatchExtractionResult[T]
extract_structured_parallel_async(
path: Path | str,
model: str,
*,
schema: None = None,
prompt: str | None = None,
pages: list[int] | None = None,
max_workers: int = 4,
dpi: int = 200,
max_retries: int = 3,
temperature: float = 0.0,
credentials: dict[str, str] | None = None,
base_url: str | None = None,
headers: dict[str, str] | None = None,
_extractor: Any = None,
) -> LLMBatchExtractionResult[dict[str, Any]]
extract_structured_parallel_async(
path: Path | str,
model: str,
*,
schema: type[T] | None = None,
prompt: str | None = None,
pages: list[int] | None = None,
max_workers: int = 4,
dpi: int = 200,
max_retries: int = 3,
temperature: float = 0.0,
credentials: dict[str, str] | None = None,
base_url: str | None = None,
headers: dict[str, str] | None = None,
_extractor: AsyncSingleExtractor[T] | None = None,
) -> LLMBatchExtractionResult[T | dict[str, Any]]
Async parallel extraction (one page per request).
Each page is extracted in a separate async request, with concurrency limited by max_workers via semaphore. Errors on individual pages are captured in the result, not raised.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Path to document/image file. |
required |
model
|
str
|
Model identifier (e.g., "openai/gpt-4o", "anthropic/claude-3-5-sonnet"). |
required |
schema
|
type[T] | None
|
Pydantic model for structured output. None for free-form dict. |
None
|
prompt
|
str | None
|
Custom extraction prompt. Auto-generated from schema if None. |
None
|
pages
|
list[int] | None
|
Page numbers to extract from (0-indexed). None for all pages. |
None
|
max_workers
|
int
|
Number of concurrent requests (semaphore limit). |
4
|
dpi
|
int
|
DPI for PDF-to-image conversion. |
200
|
max_retries
|
int
|
Max retry attempts with validation feedback. |
3
|
temperature
|
float
|
Sampling temperature (0.0 = deterministic). |
0.0
|
credentials
|
dict[str, str] | None
|
Override credentials dict (otherwise uses env vars). |
None
|
base_url
|
str | None
|
Custom API base URL for OpenAI-compatible APIs (vLLM, Ollama, etc.). |
None
|
headers
|
dict[str, str] | None
|
Custom HTTP headers for OpenAI-compatible APIs. |
None
|
_extractor
|
AsyncSingleExtractor[T] | None
|
Internal parameter for dependency injection (testing only). |
None
|
Returns:
| Type | Description |
|---|---|
LLMBatchExtractionResult[T | dict[str, Any]]
|
LLMBatchExtractionResult containing |
LLMBatchExtractionResult[T | dict[str, Any]]
|
per-page PageExtractionResult with data or errors. |
LLMBatchExtractionResult[T | dict[str, Any]]
|
Results are guaranteed to be in the same order as the input pages, |
LLMBatchExtractionResult[T | dict[str, Any]]
|
i.e., |
Source code in unifex/llm_factory.py
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 | |