like insurance coverage insurance policies, medical information, and compliance reviews are notoriously lengthy and tedious to parse.
Vital 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 particular person to sift by and digest.
Giant language fashions (LLMs), already identified for his or her versatility, function highly effective instruments to chop by this complexity, pulling out the important thing info and turning messy paperwork into clear, structured info.
On this article, we discover Google’s LangExtract framework and its open-source LLM, Gemma 3which collectively make extracting structured info from unstructured textual content correct and environment friendly.
To deliver this to life, we’ll stroll by a demo on parsing an insurance coverage coverage, exhibiting how particulars like exclusions will be surfaced successfully.
Contents
(1) Understanding LangExtract and Gemma
(2) Below the Hood of LangExtract
(3) Instance Walkthrough
The accompanying GitHub repo will be discovered right 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 info from messy unstructured textual content primarily based on user-defined directions.
It allows LLMs to excel at named entity recognition (similar to protection limits, exclusions, and clauses) and relationship extraction (logically linking every clause to its situations) by effectively grouping associated entities.
Its recognition stems from its simplicity, as only a few strains of code are sufficient to carry out structured info extraction. Past its simplicity, a number of key options make LangExtract stand out:
- Precise Supply Alignment: Every extracted merchandise is linked again to its exact location within the authentic textual content, guaranteeing full traceability.
- Constructed for Lengthy Paperwork: Handles the “needle-in-a-haystack” drawback with good 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 expensive fine-tuning.
- Constant Structured Outputs: Makes use of few-shot examples and managed technology (just for sure LLMs like Gemini) to implement a steady output schema and produce dependable, structured outcomes.
- Interactive Visualization: Generates an interactive HTML file to visualise and assessment extracted entities of their authentic context.
(ii) Gemma 3
Gemma is a household of light-weight, state-of-the-art open LLMs from Googleconstructed from the identical analysis used to create the Gemini fashions.
Gemma 3 is the most recent launch within the Gemma household, and is on the market 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 might 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 commonplace options anticipated in fashionable LLM frameworks, similar to doc ingestion, preprocessing (e.g., tokenization), immediate administration, and output dealing with.
What caught my consideration are the three capabilities that assist optimized long-context info extraction:
- Sensible chunking
- Parallel processing
- A number of extraction passes
To see how these had been carried out, I dug into the supply code and traced how they work below the hood.
(i) Chunking methods
LangExtract makes use of good chunking methods to enhance extraction high quality over a single inference cross on a big doc.
The purpose is to separate paperwork into smaller, targeted chunks of manageable context dimension, in order that the related textual content is stored in a approach that’s well-formed and straightforward to grasp.
As an alternative 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 entire sentences the place potential (by respecting textual content delimiters like paragraph breaks), in order that the context stays intact.
- Handles lengthy sentences: If a sentence is simply too lengthy, it’s damaged at pure factors like newlines. Provided that obligatory will it cut 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 cut 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 factors don’t add additional latency.
Because of this, LangExtract creates well-formed chunks that pack in as a lot context as potential whereas avoiding messy splits, which helps the LLM preserve extraction high quality throughout massive paperwork.
(ii) Parallel processing
LangExtract’s assist for parallel processing at LLM inference (as seen in mannequin supplier scripts) allows extraction high quality to be excessive over lengthy paperwork (i.e., good entity protection and attribute project) with out considerably rising total latency.
When given a listing of textual content chunks, the max_workers
parameter controls what number of duties can run in parallel. These staff 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 likely to be missed in any single run.
In essence, it adopts a multi-sample and merge technique, the place extraction is run a number of occasions independently, counting on the LLM’s stochastic nature to floor entities that is likely to be 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 cross is stored.
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 occasions, which may 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 web site.
Try 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 pc or a neighborhood server. It permits us to work together with these fashions with no need an Web connection or counting on cloud providers.
To put in Ollama, go to the Downloads web page and select the installer on your working system. As soon as executed, confirm the set up by operating ollama --version
in your terminal.
Vital: 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 appliance (macOS or Home windows) or getting into ollama serve
for Linux.
To obtain Gemma 3 (4B) regionally (3.3GB in dimension), we run this command: ollama pull gemma3:4b
after which we run ollama checklist
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 an evidence of the code above:
load_documents
: Goes by 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 format info similar 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 info we need 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 possible hit an error of langextract.resolver.ResolverParsingError
.
The difficulty is that Gemma does not embody built-in structured-output enforcement, so by default, it outputs unstructured textual content in pure language. It could then inadvertently embody additional textual content or malformed JSON, doubtlessly breaking the strict JSON parsers in LangExtract.
Nonetheless, 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 exhibits the LLM labored examples of how textual content ought to map to structured outputs, informing it what to extract and find out how to format it.
It incorporates a listing of Extraction
objects representing the specified output, the place every one is a container class comprising attributes of a single piece of extracted info.
(iv) Extraction Run
With our PDF parser and prompts arrange, we’re able to run the extraction with LangExtract’s extract
technique:
Right here is an evidence of the parameters above:
- We cross our enter textual content, prompts, and a number of other few-shot examples into the
text_or_documents
,prompt_description
andexamples
parameters respectively - We cross the mannequin model
gemma3:4b
intomodel_id
- The
model_url
is defaulted to Ollama’s native endpoint (http://localhost:11434
). Be certain that the Ollama service is already operating in your native machine - We set
fence_output
anduse_schema_constraint
toFalse
since Gemma is just not geared for structured output, and LangExtract doesn’t but assist schema constraints for Ollama max_char_buffer
units the utmost variety of characters for inference. Smaller values enhance accuracy (by lowering context dimension) 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:
Customized post-processing is then utilized to beautify the outcome for straightforward 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 clarification.
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 information from prolonged paperwork.
These strategies reveal how the fitting mixture of fashions and extraction methods can flip lengthy, complicated paperwork into structured insights which are correct, traceable, and prepared for sensible use.
Earlier than You Go
I invite you to observe my GitHub and LinkedIn pages for extra partaking and sensible content material. In the meantime, have enjoyable extracting structured info with LangExtract and Gemma 3!