Skip to content

About

LangChain document loader for scanned PDFs and images, in 100+ languages, with FastOCR

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

FastOCR

A LangChain document loader that reads scanned PDFs and images with FastOCR.

CI PyPI Python License: MIT

Website · Docs · Get an API key

FastOCR reads a scanned page in English, Arabic and Hindi and returns its text

Quickstart

pip install langchain-fastocr
export FASTOCR_API_KEY="your_api_key"
from langchain_fastocr import FastOCRLoader

documents = FastOCRLoader("scanned_contract.pdf").load()
print(documents[0].page_content)

Python 3.10 or newer. The distribution is langchain-fastocr and the import is langchain_fastocr.

What you get back

FastOCRLoader returns one Document per file. For bank statements, see the next section.

You get Where
The text of the whole file page_content
The file's path and name metadata["source"], metadata["file_name"]
The FastOCR document id metadata["fastocr_document_id"]
Pages billed metadata["pages_billed"]
completed or partial metadata["status"]
A searchable PDF, with output="pdf" saved as <name>.searchable.pdf beside the input; its path is in metadata["searchable_pdf_path"]

If the account runs out of pages partway through a file, you still get the text of the pages that were processed, with status set to partial and a warning in the log. A document that fails raises the error from fastocr-sdk with its message and code.

Bank statements (beta)

FastOCRBankStatementLoader reads bank statements (PDF, PNG, JPG, JPEG or WebP) and returns one Document per transaction. A transaction is what people ask about ("what did I pay this shop?"), so each one is its own chunk to embed and retrieve; the statement-level facts sit in every document's metadata.

from langchain_fastocr import FastOCRBankStatementLoader

documents = FastOCRBankStatementLoader("october-statement.pdf").load()
print(documents[0].page_content)  # 2026-10-02 | Coffee | out 5.00 USD | balance 95.00
You get Where
Date, description, signed amount and balance of one transaction page_content
Bank, account number, currency metadata["bank"], metadata["account_number"], metadata["currency"]
Which account of the statement (1 is the first) metadata["account"]
Date, amount, in or out, balance, page metadata["date"], ["amount"], ["direction"], ["balance"], ["page"]
verified, check or unchecked metadata["status"]
True when a person should check the statement metadata["needs_review"]
The statement id metadata["fastocr_statement_id"]

Bank statement pages are a separate balance, shared with the website's bank statement converter. A statement with no transactions gives no documents and a warning in the log; a failed one raises the SDK's StatementFailedError. Options: file_path, api_key, base_url and timeout (600 seconds by default).

Languages and files

  • 100+ languages, detected automatically. No language setting is needed.
  • PDF files up to 1 GiB, and PNG, JPG, JPEG, GIF, WebP and BMP images up to 20 MiB. An image counts as one page.

Get an API key and pages

Any signed-in FastOCR account can create an API key and start right away.

  1. Sign in and create an API key in the developer console. You accept the API terms with your first key.
  2. Set the key as FASTOCR_API_KEY, or pass api_key= to the loader.
  3. When you need more than the free pages, buy a Page Pack on the pricing page. Packs work on the website and in the API.

Every account gets 20 free pages a month, shared with PDFs on the website. An image sent to the API is one page.

Examples

RAG over scanned PDFs

from langchain_core.vectorstores import InMemoryVectorStore
from langchain_fastocr import FastOCRLoader
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter

documents = FastOCRLoader("scanned_contract.pdf").load()
chunks = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150).split_documents(documents)

store = InMemoryVectorStore.from_documents(chunks, OpenAIEmbeddings())
for hit in store.as_retriever(search_kwargs={"k": 3}).invoke("When can the contract be terminated?"):
    print(hit.metadata["file_name"], hit.page_content[:200])

Each chunk keeps its file's metadata, so answers can cite the source file. This example also needs pip install langchain-openai langchain-text-splitters numpy and an OpenAI key; any other embeddings class works the same way.

A folder of scans

from pathlib import Path

from langchain_fastocr import FastOCRLoader

scans = sorted(p for p in Path("scans").iterdir() if p.suffix.lower() in {".pdf", ".png", ".jpg", ".jpeg"})
for document in FastOCRLoader(scans).lazy_load():
    print(document.metadata["file_name"], document.metadata["pages_billed"])

lazy_load() yields each document as soon as its file is finished. The loader runs one file at a time.

Searchable PDFs for an archive

from langchain_fastocr import FastOCRLoader

documents = FastOCRLoader(["invoice-001.pdf", "invoice-002.pdf"], output="pdf").load()
for document in documents:
    print(document.metadata["searchable_pdf_path"])

Each searchable PDF is saved beside its input, as invoice-001.searchable.pdf. An existing file with that name is replaced. Index the text and keep the PDF.

Options

Argument Default Meaning
file_path required One path or a list of paths
api_key FASTOCR_API_KEY Your FastOCR API key
output "text" "pdf" also saves a searchable PDF beside each input
base_url FASTOCR_BASE_URL, then https://api.fastocr.org API address
timeout 300.0 Seconds to wait for each document

Support and license

Questions: [email protected]. Bugs and requests: GitHub issues. MIT license; see LICENSE.

About

LangChain document loader for scanned PDFs and images, in 100+ languages, with FastOCR

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages