like insurance coverage insurance policies, medical information, and compliance reviews are notoriously lengthy and tedious to parse.
Essential particulars (e.g., protection limits and obligations in insurance coverage insurance policies) are buried in dense, unstructured textual content that’s difficult for the common individual to sift by way of and digest.
Giant language fashions (LLMs), already recognized for his or her versatility, function highly effective instruments to chop by way of this complexity, pulling out the important thing information and turning messy paperwork into clear, structured data.
On this article, we discover Google’s LangExtract framework and its open-source LLM, Gemma 3, which collectively make extracting structured data from unstructured textual content correct and environment friendly.
To carry this to life, we’ll stroll by way of a demo on parsing an insurance coverage coverage, displaying how particulars like exclusions will be surfaced successfully.
Contents
(1) Understanding LangExtract and Gemma
(2) Under the Hood of LangExtract
(3) Example Walkthrough
The accompanying GitHub repo will be discovered here.
(1) Understanding LangExtract and Gemma
(i) LangExtract
LangExtract is an open-source Python library (launched below Google’s GitHub) that makes use of LLMs to extract structured data from messy unstructured textual content primarily based on user-defined directions.
It allows LLMs to excel at named entity recognition (akin to protection limits, exclusions, and clauses) and relationship extraction (logically linking every clause to its situations) by effectively grouping associated entities.
Its reputation stems from its simplicity, as just some strains of code are sufficient to carry out structured data extraction. Past its simplicity, a number of key options make LangExtract stand out:
- Actual Supply Alignment: Every extracted merchandise is linked again to its exact location within the authentic textual content, making certain full traceability.
- Constructed for Lengthy Paperwork: Handles the “needle-in-a-haystack” downside with sensible chunking, parallel processing, and iterative passes to maximise recall in order to search out extra entities.
- Broad Mannequin Compatibility: Works seamlessly with totally different LLMs, from cloud-based fashions like Gemini to native open-source choices.
- Area Agnostic: Adapts to any area with solely a handful of examples, eradicating the necessity for pricey fine-tuning.
- Constant Structured Outputs: Makes use of few-shot examples and managed era (just for sure LLMs like Gemini) to implement a secure output schema and produce dependable, structured outcomes.
- Interactive Visualization: Generates an interactive HTML file to visualise and evaluate extracted entities of their authentic context.
(ii) Gemma 3
Gemma is a household of light-weight, state-of-the-art open LLMs from Google, constructed from the identical analysis used to create the Gemini fashions.
Gemma 3 is the newest launch within the Gemma household, and is out there in 5 parameter sizes: 270M, 1B, 4B, 12B, and 27B. It is usually presupposed to be the present, most succesful mannequin that runs on a single GPU.
It may well deal with immediate inputs of as much as 128K tokens, permitting us to course of many multi-page articles (or a whole lot of photographs) in a single immediate.
On this article, we’ll use the Gemma 3 4B mannequin (4-billion parameter variant), deployed regionally through Ollama.
(2) Below the Hood of LangExtract
LangExtract comes with many customary options anticipated in trendy LLM frameworks, akin to doc ingestion, preprocessing (e.g., tokenization), immediate administration, and output dealing with.
What caught my consideration are the three capabilities that help optimized long-context data extraction:
- Good chunking
- Parallel processing
- A number of extraction passes
To see how these have been carried out, I dug into the supply code and traced how they work below the hood.
(i) Chunking methods
LangExtract makes use of sensible chunking strategies to enhance extraction high quality over a single inference go on a big doc.
The objective is to separate paperwork into smaller, centered chunks of manageable context measurement, in order that the related textual content is saved in a means that’s well-formed and straightforward to know.
As a substitute of mindlessly reducing at character limits, it respects sentences, paragraphs, and newlines.
Here’s a abstract of the important thing behaviors within the chunking technique:
- Sentence- and paragraph-aware: Chunks are fashioned from complete sentences the place attainable (by respecting textual content delimiters like paragraph breaks), in order that the context stays intact.
- Handles lengthy sentences: If a sentence is just too lengthy, it’s damaged at pure factors like newlines. Provided that essential will it break up inside a sentence.
- Edge case dealing with: If a single phrase or token is longer than the restrict, it turns into a piece to keep away from errors.
- Token-based splitting: All cuts respect token boundaries, so phrases are by no means break up mid-way.
- Context preservation: Every chunk carries metadata (token and character positions) that map it again to the supply doc.
- Environment friendly processing: Chunks will be grouped into batches and processed in parallel, so high quality positive aspects don’t add additional latency.
Because of this, LangExtract creates well-formed chunks that pack in as a lot context as attainable whereas avoiding messy splits, which helps the LLM keep extraction high quality throughout massive paperwork.
(ii) Parallel processing
LangExtract’s help for parallel processing at LLM inference (as seen in model provider scripts) allows extraction high quality to be excessive over lengthy paperwork (i.e., good entity protection and attribute task) with out considerably growing general latency.
When given an inventory of textual content chunks, the max_workers
parameter controls what number of duties can run in parallel. These employees ship a number of chunks to the LLM concurrently, with as much as max_workers
chunks processed in parallel.
(iii) A number of extraction passes
The aim of iterative extraction passes is to enhance the recall by capturing entities that is perhaps missed in any single run.
In essence, it adopts a multi-sample and merge technique, the place extraction is run a number of instances independently, counting on the LLM’s stochastic nature to floor entities that is perhaps missed in a run.
Afterwards, outcomes from all passes are merged. If two extractions cowl the identical area of textual content, the model from the sooner go is saved.
This strategy boosts recall by capturing extra entities throughout runs, whereas resolving conflicts by a first-pass-wins rule. The draw back is that it reprocesses tokens a number of instances, which might enhance prices.
(3) Instance Walkthrough
Let’s put LangExtract and Gemma to the check on a pattern motor insurance coverage coverage doc, discovered publicly on the MSIG Singapore website.
Take a look at the accompanying GitHub repo to observe alongside.

(i) Preliminary Setup
LangExtract will be put in from PyPI with:
pip set up langextract
We then obtain and run Gemma 3 (4B mannequin) regionally with Ollama.
Ollama is an open-source instrument that simplifies operating LLMs on our laptop or a neighborhood server. It permits us to work together with these fashions with no need an Web connection or counting on cloud companies.
To put in Ollama, go to the Downloads page and select the installer to your working system. As soon as completed, confirm the set up by operating ollama --version
in your terminal.
Essential: Guarantee your native system has GPU entry for Ollama, as this dramatically accelerates efficiency.
After Ollama is put in, we get the service operating by opening the applying (macOS or Home windows) or coming into ollama serve
for Linux.
To obtain Gemma 3 (4B) regionally (3.3GB in measurement), we run this command: ollama pull gemma3:4b
, after which we run ollama listing
to confirm that Gemma is downloaded regionally in your system.

(ii) PDF Parsing and Processing
Step one is to learn the PDF coverage doc and parse the contents utilizing PyMuPDF (put in with pip set up PyMuPDF
).
We create a Doc
class storing a bit of textual content and related metadata, and a PDFProcessor
class for the general doc parsing.
Right here is a proof of the code above:
load_documents
: Goes by way of every web page, extracts textual content blocks, and saves them asDoc
objects. Every block consists of the textual content and metadata (e.g., web page quantity, coordinates with web page width/top).
The coordinates seize the place the textual content seems on the web page, preserving structure data akin to whether or not it’s a header, physique textual content, or footer.get_all_text
: Combines all extracted textual content into one string, with clear markers separating pages.get_page_text
: Will get solely the textual content from a selected web page.
(iii) Immediate Engineering
The following step is to supply directions to information the LLM within the extraction course of through LangExtract.
We start with a system immediate that specifies the structured data we wish to extract, specializing in the coverage exclusion clauses.
Within the immediate above, I explicitly specified a JSON output because the anticipated response format. With out this, we’ll probably hit an error of <a href="https://github.com/google/langextract/points/127" rel="noreferrer noopener" goal="_blank">langextract.resolver.ResolverParsingError</a>
.
The problem is that Gemma does not embody built-in structured-output enforcement, so by default, it outputs unstructured textual content in pure language. It might then inadvertently embody additional textual content or malformed JSON, probably breaking the strict JSON parsers in LangExtract.
Nevertheless, if we use LLMs like Gemini which have schema-constrained decoding (i.e., configurable for structured output), then immediate strains 11–21 will be omitted.
Subsequent, we introduce few-shot prompting by offering an instance of what exclusion clauses imply within the context of insurance coverage.
LangExtract’s ExampleData
class serves as a template that reveals the LLM labored examples of how textual content ought to map to structured outputs, informing it what to extract and methods to format it.
It comprises an inventory of Extraction
objects representing the specified output, the place each is a container class comprising attributes of a single piece of extracted data.
(iv) Extraction Run
With our PDF parser and prompts arrange, we’re able to run the extraction with LangExtract’s extract
methodology:
Right here is a proof of the parameters above:
- We go our enter textual content, prompts, and a number of other few-shot examples into the
text_or_documents
,prompt_description
, andexamples
parameters respectively - We go the mannequin model
gemma3:4b
intomodel_id
- The
model_url
is defaulted to Ollama’s native endpoint (http://localhost:11434
). Be sure that the Ollama service is already operating in your native machine - We set
fence_output
anduse_schema_constraint
toFalse
since Gemma will not be geared for structured output, and LangExtract doesn’t but help schema constraints for Ollama max_char_buffer
units the utmost variety of characters for inference. Smaller values enhance accuracy (by lowering context measurement) however enhance the variety of LLM callsextraction_passes
units the variety of extraction passes for improved recall within the extraction
On my 8GB VRAM GPU, the 10-page doc took <10 minutes to finish parsing and extraction.
(v) Save and Postprocess Output
We lastly save the output utilizing LangExtract’s io
module:
Custom post-processing is then utilized to beautify the outcome for simple viewing, and here’s a snippet of the output:
We are able to see that the LLM responses include structured extractions from the unique textual content, grouping them by class (particularly exclusions) and offering each the supply textual content line and a plain-English rationalization.
This format makes complicated insurance coverage clauses simpler to interpret, providing a transparent mapping between formal coverage language and easy summaries.
(4) Wrapping it up
On this article, we explored how LangExtract’s chunking, parallel processing, and iterative passes, mixed with Gemma 3’s capabilities, allow dependable extraction of structured knowledge from prolonged paperwork.
These methods display how the correct mixture of fashions and extraction methods can flip lengthy, complicated paperwork into structured insights which might be correct, traceable, and prepared for sensible use.
Earlier than You Go
I invite you to observe my GitHub and LinkedIn pages for extra participating and sensible content material. In the meantime, have enjoyable extracting structured data with LangExtract and Gemma 3!