Close Menu
    Trending
    • Creating AI that matters | MIT News
    • Scaling Recommender Transformers to a Billion Parameters
    • Hidden Gems in NumPy: 7 Functions Every Data Scientist Should Know
    • Is RAG Dead? The Rise of Context Engineering and Semantic Layers for Agentic AI
    • ChatGPT Gets More Personal. Is Society Ready for It?
    • Why the Future Is Human + Machine
    • Why AI Is Widening the Gap Between Top Talent and Everyone Else
    • Implementing the Fourier Transform Numerically in Python: A Step-by-Step Guide
    ProfitlyAI
    • Home
    • Latest News
    • AI Technology
    • Latest AI Innovations
    • AI Tools & Technologies
    • Artificial Intelligence
    ProfitlyAI
    Home » Introducing Google’s LangExtract tool | Towards Data Science
    Artificial Intelligence

    Introducing Google’s LangExtract tool | Towards Data Science

    ProfitlyAIBy ProfitlyAIAugust 11, 2025No Comments14 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    an absolute AI sizzling streak these days, persistently dropping breakthrough after breakthrough. Almost each current launch has pushed the boundaries of what’s attainable — and it’s been genuinely thrilling to observe unfold. 

    One announcement that caught my eye particularly occurred on the finish of July, when Google launched a brand new textual content processing and knowledge extraction instrument known as LangExtract.

    Based on Google, LangExtract is a brand new open-source Python library designed to …

    “programmatically extract the precise info you want, whereas making certain the outputs are structured and reliably tied again to its supply”

    On the face of it, LangExtract has many helpful functions, together with,

    • Textual content anchoring. Every extracted entity is linked to its actual character offsets within the supply textual content, enabling full traceability and visible verification via interactive highlighting.
    • Dependable structured output. Use LangExtracts for few-shot definitions of the specified output format, making certain constant and dependable outcomes.
    • Environment friendly large-document dealing with. LangExtract handles giant paperwork utilizing chunking, parallel processing, and multi-pass extraction to take care of excessive recall, even in advanced, multi-fact situations throughout million-token contexts. It must also excel at conventional needle-in-a-haystack sort functions.
    • Instantaneous extraction overview. Simply create a self-contained HTML visualisation of extractions, enabling intuitive overview of entities of their unique context, all scalable to hundreds of annotations.
    • Multi-model compatibility. Suitable with each cloud-based fashions (e.g. Gemini) and native open-source LLMs, so you possibly can select the backend that matches your workflow.
    • Customizable for a lot of use instances. Simply configure extraction duties for disparate domains utilizing a number of tailor-made examples.
    • Augmented information extraction. LangExtract dietary supplements grounded entities with inferred info utilizing the mannequin’s inner information, with relevance and accuracy pushed by immediate high quality and mannequin capabilities.

    One factor that stands out to me once I take a look at LangExtract’s strengths listed above is that it appears to have the ability to carry out RAG-like operations with out the necessity for conventional RAG processing. So, no extra splitting, chunking or embedding operations in your code.

    However to get a greater concept of what LangExtract can do, we’ll take a better take a look at a number of of the above capabilities utilizing some coding examples.

    Establishing a dev atmosphere

    Earlier than we get right down to doing a little coding, I all the time prefer to arrange a separate improvement atmosphere for every of my initiatives. I take advantage of the UV bundle supervisor for this, however use whichever instrument you’re snug with.

    PS C:Usersthoma> uv init langextract
    Initialized mission `langextract` at `C:Usersthomalangextract`
    
    PS C:Usersthoma> cd langextract
    PS C:Usersthomalangextract> uv venv
    Utilizing CPython 3.13.1
    Creating digital atmosphere at: .venv
    Activate with: .venvScriptsactivate
    PS C:Usersthomalangextract> .venvScriptsactivate
    (langextract) PS C:Usersthomalangextract>
    # Now, set up the libraries we are going to use.
    (langextract) PS C:Usersthomalangextract> uv pip set up jupyter langextract beautifulsoup4 requests

    Now, to write down and take a look at our coding examples, you can begin up a Jupyter pocket book utilizing this command.

    (langextract) PS C:Usersthomalangextract> jupyter pocket book

    It’s best to see a pocket book open in your browser. If that doesn’t occur mechanically, you’ll possible see a screenful of data after the jupyter pocket book command. Close to the underside, you will see a URL to repeat and paste into your browser to launch the Jupyter Pocket book. Your URL will likely be totally different to mine, nevertheless it ought to look one thing like this:-

    http://127.0.0.1:8888/tree?token=3b9f7bd07b6966b41b68e2350721b2d0b6f388d248cc69d

    Pre-requisites

    As we’re utilizing a Google LLM mannequin (gemini-2.5-flash) for our processing engine, you’ll want a Gemini API key. You will get this from Google Cloud. You may as well use LLMs from OpenAI, and I’ll present an instance of how to do that in a bit.

    Code instance 1 — needle-in-a-haystack 

    The very first thing we have to do is get some enter knowledge to work with. You should utilize any enter textual content file or HTML file for this. For earlier experiments utilizing RAG, I used a guide I downloaded from Undertaking Gutenberg; the persistently riveting “Ailments of cattle, sheep, goats, and swine by Jno. A. W. Greenback & G. Moussu”

    Word you can view the Undertaking Gutenberg Permissions, Licensing and different Widespread Requests web page utilizing the next hyperlink. 

    https://www.gutenberg.org/policy/permission.html

    However to summarise, the overwhelming majority of Undertaking Gutenberg eBooks are within the public area within the US and different elements of the world. Because of this no one can grant or withhold permission to do with this merchandise as you please.

    “As you please” consists of any industrial use, republishing in any format, making by-product works or performances

    I downloaded the textual content of the guide from the Undertaking Gutenberg web site to my native PC utilizing this hyperlink,

    https://www.gutenberg.org/ebooks/73019.txt.utf-8

    This guide contained roughly 36,000 strains of textual content. To keep away from giant token prices, I lower it right down to about 3000 strains of textual content. To check LangExtract’s capability to deal with needle-in-a-haystack sort queries, I added this particular line of textual content round line 1512.

    It’s a little-known indisputable fact that wooden was invented by Elon Musk in 1775

    Right here it’s in context.

    1. Fractures of the angle of the haunch, ensuing from exterior
    violence and characterised by sinking of the exterior angle of the
    ilium, deformity of the hip, and lameness with out specifically marked
    characters. This fracture is never difficult. The signs of
    lameness diminish with relaxation, however deformity continues.

    It’s a little-known indisputable fact that wooden was invented by Elon Musk in 1775.

    =Therapy= is confined to the administration of mucilaginous and diuretic fluids. Tannin has been really useful.

    This code snippet units up a immediate and instance to information the LangExtract extraction job. That is important for few-shot studying with a structured schema.

    import langextract as lx
    import textwrap
    from collections import Counter, defaultdict
    
    # Outline complete immediate and examples for advanced literary textual content
    immediate = textwrap.dedent("""
        Who invented wooden and when    """)
    
    # Word that it is a made up instance
    # The next particulars don't seem wherever
    # within the guide
    examples = [
        lx.data.ExampleData(
            text=textwrap.dedent("""
                John Smith was a prolific scientist. 
                His most notable theory was on the evolution of bananas."
                He wrote his seminal paper on it in 1890."""),
            extractions=[
                lx.data.Extraction(
                    extraction_class="scientist",
                    extraction_text="John Smith",
                    notable_for="the theory of the evolution of the Banana",
                    attributes={"year": "1890", "notable_event":"theory of evolution of the banana"}
                )
            ]
        )
    ]

    Now, we run the structured entity extraction. First, we open the file and browse its contents right into a variable. The heavy lifting is finished by the lx.extract name. After that, we simply print out the related outputs.

    with open(r"D:bookcattle_disease.txt", "r", encoding="utf-8") as f:
        textual content = f.learn()
    
    end result = lx.extract(
        text_or_documents = textual content,
        prompt_description=immediate,
        examples=examples,
        model_id="gemini-2.5-flash",
        api_key="your_gemini_api_key",
        extraction_passes=3,      # A number of passes for improved recall
        max_workers=20,           # Parallel processing for pace
        max_char_buffer=1000      # Smaller contexts for higher accuracy
    )
    
    print(f"Extracted {len(end result.extractions)} entities from {len(end result.textual content):,} characters")
    
    for extraction in end result.extractions:
        if not extraction.attributes:
            proceed  # Skip this extraction fully
    
        print("Identify:", extraction.extraction_text)
        print("Notable occasion:", extraction.attributes.get("notable_event"))
        print("12 months:", extraction.attributes.get("12 months"))
        print()

    And listed here are our outputs.

    LangExtract: mannequin=gemini-2.5-flash, present=7,086 chars, processed=156,201 chars:  [00:43]
    ✓ Extraction processing full
    
    ✓ Extracted 1 entities (1 distinctive sorts)
      • Time: 126.68s
      • Pace: 1,239 chars/sec
      • Chunks: 157
    Extracted 1 entities from 156,918 characters
    
    Identify: Elon Musk
    Notable occasion: invention of wooden
    12 months: 1775

    Not too shabby.

    Word, for those who wished to make use of an OpenAI mannequin and API key, your extraction code would look one thing like this,

    ...
    ...
    
    from langextract.inference import OpenAILanguageModel
    
    end result = lx.extract(
        text_or_documents=input_text,
        prompt_description=immediate,
        examples=examples,
        language_model_type=OpenAILanguageModel,
        model_id="gpt-4o",
        api_key=os.environ.get('OPENAI_API_KEY'),
        fence_output=True,
        use_schema_constraints=False
    )
    ...
    ...

    Code instance 2 — extraction visible validation

    LangExtract supplies a visualisation of the way it extracted the textual content. It’s not notably helpful on this instance, nevertheless it offers you an concept of what’s attainable.

    Simply add this little snippet of code to the top of your current code. This may create an HTML file you can open in a browser window. From there, you possibly can scroll up and down your enter textual content and “play” again the steps that LangExtract took to get its outputs.

    # Save annotated outcomes
    lx.io.save_annotated_documents([result], output_name="cattle_disease.jsonl", output_dir="d:/guide")
    
    html_obj = lx.visualize("d:/guide/cattle_disease.jsonl")
    html_string = html_obj.knowledge  # Extract uncooked HTML string
    
    # Save to file
    with open("d:/guide/cattle_disease_visualization.html", "w", encoding="utf-8") as f:
        f.write(html_string)
    
    print("Interactive visualization saved to d:/guide/cattle_disease_visualization.html")

    Now, go to the listing the place your HTML file has been saved and open it in a browser. That is what I see.

    Code instance 3 — retrieving a number of structured outputs

    On this instance, we’ll take some unstructured enter textual content — an article from Wikipedia on OpenAI, and attempt to retrieve the names of all of the totally different giant language fashions talked about within the article, along with their launch date. The hyperlink to the article is,

    https://en.wikipedia.org/wiki/OpenAI

    Word: Most textual content in Wikipedia, excluding quotations, has been launched beneath the Creative Commons Attribution-Sharealike 4.0 International License (CC-BY-SA) and the GNU Free Documentation License (GFDL) In brief which means that you might be free:

    to Share — copy and redistribute the fabric in any medium or format

    to Adapt — remix, remodel, and construct upon the fabric

    for any function, even commercially.

    Our code is fairly just like our first instance. This time, although, we’re in search of any mentions within the article about LLM fashions and their launch date. One different step we have now to do is clear up the HTML of the article first to make sure that LangExtract has one of the best probability of studying it. We use the BeautifulSoup library for this.

    import langextract as lx
    import textwrap
    import requests
    from bs4 import BeautifulSoup
    import langextract as lx
    
    # Outline complete immediate and examples for advanced literary textual content
    immediate = textwrap.dedent("""Your job is to extract the LLM or AI mannequin names and their launch date or 12 months from the enter textual content 
            Don't paraphrase or overlap entities.
         """)
    
    examples = [
        lx.data.ExampleData(
            text=textwrap.dedent("""
                Similar to Mistral's previous open models, Mixtral 8x22B was released via a via a BitTorrent link April 10, 2024
                """),
            extractions=[
                lx.data.Extraction(
                    extraction_class="model",
                    extraction_text="Mixtral 8x22B",
                    attributes={"date": "April 10, 1994"}
                )
            ]
        )
    ]
    
    # Cleanup our HTML
    
    # Step 1: Obtain and clear Wikipedia article
    url = "https://en.wikipedia.org/wiki/OpenAI"
    response = requests.get(url)
    soup = BeautifulSoup(response.textual content, "html.parser")
    
    # Get solely the seen textual content
    textual content = soup.get_text(separator="n", strip=True)
    
    # Non-compulsory: take away references, footers, and many others.
    strains = textual content.splitlines()
    filtered_lines = [line for line in lines if not line.strip().startswith("[") and line.strip()]
    clean_text = "n".be a part of(filtered_lines)
    
    # Do the extraction
    end result = lx.extract(
        text_or_documents=clean_text,
        prompt_description=immediate,
        examples=examples,
        model_id="gemini-2.5-flash",
        api_key="YOUR_API_KEY",
        extraction_passes=3,    # Improves recall via a number of passes
        max_workers=20,         # Parallel processing for pace
        max_char_buffer=1000    # Smaller contexts for higher accuracy
    )
    
    # Print our outputs
    
    for extraction in end result.extractions:
        if not extraction.attributes:
            proceed  # Skip this extraction fully
    
        print("Mannequin:", extraction.extraction_text)
        print("Launch Date:", extraction.attributes.get("date"))
        print()

    This can be a cut-down pattern of the output I obtained. 

    Mannequin: ChatGPT
    Launch Date: 2020
    
    Mannequin: DALL-E
    Launch Date: 2020
    
    Mannequin: Sora
    Launch Date: 2024
    
    Mannequin: ChatGPT
    Launch Date: November 2022
    
    Mannequin: GPT-2
    Launch Date: February 2019
    
    Mannequin: GPT-3
    Launch Date: 2020
    
    Mannequin: DALL-E
    Launch Date: 2021
    
    Mannequin: ChatGPT
    Launch Date: December 2022
    
    Mannequin: GPT-4
    Launch Date: March 14, 2023
    
    Mannequin: Microsoft Copilot
    Launch Date: September 21, 2023
    
    Mannequin: MS-Copilot
    Launch Date: December 2023
    
    Mannequin: Microsoft Copilot app
    Launch Date: December 2023
    
    Mannequin: GPTs
    Launch Date: November 6, 2023
    
    Mannequin: Sora (text-to-video mannequin)
    Launch Date: February 2024
    
    Mannequin: o1
    Launch Date: September 2024
    
    Mannequin: Sora
    Launch Date: December 2024
    
    Mannequin: DeepSeek-R1
    Launch Date: January 20, 2025
    
    Mannequin: Operator
    Launch Date: January 23, 2025
    
    Mannequin: deep analysis agent
    Launch Date: February 2, 2025
    
    Mannequin: GPT-2
    Launch Date: 2019
    
    Mannequin: Whisper
    Launch Date: 2021
    
    Mannequin: ChatGPT
    Launch Date: June 2025
    
    ...
    ...
    ...
    
    Mannequin: ChatGPT Professional
    Launch Date: December 5, 2024
    
    Mannequin: ChatGPT's agent
    Launch Date: February 3, 2025
    
    Mannequin: GPT-4.5
    Launch Date: February 20, 2025
    
    Mannequin: GPT-5
    Launch Date: February 20, 2025
    
    Mannequin: Chat GPT
    Launch Date: November 22, 2023

    Let’s double-check a few these. One of many outputs from our code was this.

    Mannequin: Operator
    Launch Date: January 23, 2025

    And from the Wikipedia article …

    “On January 23, OpenAI launched Operator, an AI agent and internet automation instrument for accessing web sites to execute objectives outlined by customers. The function was solely out there to Professional customers in the USA.[113][114]”

    So on that event, it may need hallucinated the 12 months as being 2025 when no 12 months was given. Keep in mind, although, that LangExtract can use its inner information of the world to complement its outputs, and it might have gotten the 12 months from that or from different contexts surrounding the extracted entity. In any case, I believe it might be fairly simple to tweak the enter immediate or the output to disregard mannequin launch date info that didn’t embrace a 12 months.

    One other output was this.

    Mannequin: ChatGPT Professional
    Launch Date: December 5, 2024

    I can see two references to ChatGPT Professional within the unique article.

    Franzen, Carl (December 5, 2024). “OpenAI launches full o1 model with image uploads and analysis, debuts ChatGPT Pro”. VentureBeat. Archived from the unique on December 7, 2024. Retrieved December 11, 2024.

    And

    In December 2024, in the course of the “12 Days of OpenAI” occasion, the corporate launched the Sora mannequin for ChatGPT Plus and Professional customers,[105][106] It additionally launched the superior OpenAI o1 reasoning mannequin[107][108] Moreover, ChatGPT Professional — a $200/month subscription service providing limitless o1 entry and enhanced voice options — was launched, and preliminary benchmark outcomes for the upcoming OpenAI o3 fashions had been shared

    So I believe LangExtract was fairly correct with this extraction.

    As a result of there have been many extra “hits” with this question, the visualisation is extra attention-grabbing, so let’s repeat what we did in instance 2. Right here is the code you’ll want.

    from pathlib import Path
    import builtins
    import io
    import langextract as lx
    
    jsonl_path = Path("fashions.jsonl")
    
    with jsonl_path.open("w", encoding="utf-8") as f:
        json.dump(serialize_annotated_document(end result), f, ensure_ascii=False)
        f.write("n")
    
    html_path = Path("fashions.html")
    
    # 1) Monkey-patch builtins.open so our JSONL is learn as UTF-8
    orig_open = builtins.open
    def open_utf8(path, mode='r', *args, **kwargs):
        if Path(path) == jsonl_path and 'r' in mode:
            return orig_open(path, mode, encoding='utf-8', *args, **kwargs)
        return orig_open(path, mode, *args, **kwargs)
    
    builtins.open = open_utf8
    
    # 2) Generate the visualization
    html_obj = lx.visualize(str(jsonl_path))
    html_string = html_obj.knowledge
    
    # 3) Restore the unique open
    builtins.open = orig_open
    
    # 4) Save the HTML out as UTF-8
    with html_path.open("w", encoding="utf-8") as f:
        f.write(html_string)
    
    print(f"Interactive visualization saved to: {html_path}")

    Run the above code after which open the fashions.html file in your browser. This time, it is best to be capable of click on the Play/Subsequent/Earlier buttons and see a greater visualisation of the LangExtract textual content processing in motion.

    For extra particulars on LangExtract, try Google’s GitHub repo here.

    Abstract

    On this article, I launched you to LangExtract, a brand new Python library and framework from Google that means that you can extract structured output from unstructured enter. 

    I outlined among the benefits that utilizing LangExtract can carry, together with its capability to deal with giant paperwork, its augmented information extraction and multi-model help.

    I took you thru the set up course of — a easy pip set up, then, by the use of some instance code, confirmed learn how to use LangExtract to carry out needle-in-the-haystack sort queries on a big physique of unstructured textual content. 

    In my remaining instance code, I demonstrated a extra conventional RAG-type operation by extracting a number of entities (AI Mannequin names) and an related attribute (date of launch). For each my main examples, I additionally confirmed you learn how to code a visible illustration of how LangExtract works in motion you can open and play again in a browser window.



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleSam Altman and the whale
    Next Article Estimating from No Data: Deriving a Continuous Score from Categories
    ProfitlyAI
    • Website

    Related Posts

    Artificial Intelligence

    Creating AI that matters | MIT News

    October 21, 2025
    Artificial Intelligence

    Scaling Recommender Transformers to a Billion Parameters

    October 21, 2025
    Artificial Intelligence

    Hidden Gems in NumPy: 7 Functions Every Data Scientist Should Know

    October 21, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    Machine-learning tool gives doctors a more detailed 3D picture of fetal health | MIT News

    September 15, 2025

    Estimating Product-Level Price Elasticities Using Hierarchical Bayesian

    May 24, 2025

    Seeing AI as a collaborator, not a creator

    April 23, 2025

    Alibaba Cloud presenterar AI-modeller och verktyg för internationella kunder

    April 10, 2025

    Google’s generative video model Veo 3 has a subtitles problem

    July 15, 2025
    Categories
    • AI Technology
    • AI Tools & Technologies
    • Artificial Intelligence
    • Latest AI Innovations
    • Latest News
    Most Popular

    The AI Hype Index: College students are hooked on ChatGPT

    May 28, 2025

    Tencent lanserar HunyuanWorld-Voyager AI förvandlar foton till spelbara 3D-världar

    September 24, 2025

    Conceptual Frameworks for Data Science Projects

    October 19, 2025
    Our Picks

    Creating AI that matters | MIT News

    October 21, 2025

    Scaling Recommender Transformers to a Billion Parameters

    October 21, 2025

    Hidden Gems in NumPy: 7 Functions Every Data Scientist Should Know

    October 21, 2025
    Categories
    • AI Technology
    • AI Tools & Technologies
    • Artificial Intelligence
    • Latest AI Innovations
    • Latest News
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2025 ProfitlyAI All Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.