buildfastwithaibuildfastwithai
AI WorkshopsAll blogsPartner with usFDE Program
Unrot mascotGet yourselfupskilled in AIfor free.Get the appNext live workshopFree AI WorkshopLive session, recording includedReserve a seat

Newsletter

Stay ahead

AI tools and tips. No spam.

Share
Back to blogs
Tutorials
Coding

Extract Structured Data from Unstructured Text Using LangExtract + Gemini

By Satvik Paramkusham
August 6, 2025
5 min read
Share:
Extract Structured Data from Unstructured Text Using LangExtract + Gemini
Share:

Getting Started with LangExtract: Unlocking Text Analysis with AI

LangExtract is a powerful Python library that simplifies extracting structured data from unstructured text, leveraging advanced AI models like Google’s Gemini and OpenAI’s GPT-4o. Whether you’re analyzing literature, news articles, or medical records, LangExtract enables you to identify specific entities—such as names, products, emotions, or medications—while preserving their context through attributes. Its interactive visualization interface, accessible via Google Colab, makes it an ideal tool for developers, researchers, and enthusiasts looking to streamline text analysis.

This guide, based on the provided LangExtract notebook, walks you through setting up and using the library to extract meaningful insights from text. We’ll cover installation, defining extraction tasks, running extractions, visualizing results, and using both Gemini and OpenAI models, ensuring you can apply LangExtract to diverse use cases.

Why LangExtract?

LangExtract stands out for its ability to transform raw text into structured data with minimal effort. Key features include:

  • Flexible Extraction: Extract entities like names, AI models, products, or medical details with customizable attributes.

  • AI Model Support: Compatible with powerful models like Gemini-2.5-Pro and GPT-4o for accurate results.

  • Interactive Visualization: Displays extracted entities in an interactive HTML interface, highlighting their positions and attributes.

  • Ease of Use: Runs seamlessly in Google Colab, requiring no local setup.

Whether you’re a beginner or an experienced developer, LangExtract’s clear prompts and example-driven approach make it accessible and powerful.

Prerequisites

Before starting, ensure you have:

  • Google Colab Account: For running the notebook in a cloud-based environment.

  • API Keys: Obtain keys for Google’s Gemini (via Google) and/or OpenAI’s GPT-4o (via OpenAI).

  • Basic Python Knowledge: Familiarity with Python and Jupyter notebooks helps, but the process is beginner-friendly.

Step 1: Installation

To use LangExtract, install the library in Google Colab:

!pip -q install langextract

Set up your API keys securely using Colab’s userdata feature:

import os

os.environ["LANGEXTRACT_API_KEY"] = 'GOOGLE_API_KEY'
os.environ["OPENAI_API_KEY"] = 'OPENAI_API_KEY'

Add your API keys in Colab’s secrets panel (🔑 icon) to keep them secure and avoid hardcoding.

Step 2: Defining Extraction Tasks

LangExtract relies on a clear prompt and high-quality examples to guide the AI model.

import textwrap
import langextract as lx

prompt = textwrap.dedent("""
Extract characters, emotions, and relationships in order of appearance.
Use exact text for extractions. Do not paraphrase or overlap entities.
Provide meaningful attributes for each entity to add context.
""")

examples = [
    lx.data.ExampleData(
        text="ROMEO. But soft! What light through yonder window breaks? It is the east, and Juliet is the sun.",
        extractions=[
            lx.data.Extraction(extraction_class="character", extraction_text="ROMEO", attributes={"emotional_state": "wonder"}),
            lx.data.Extraction(extraction_class="emotion", extraction_text="But soft!", attributes={"feeling": "gentle awe"}),
            lx.data.Extraction(extraction_class="relationship", extraction_text="Juliet is the sun", attributes={"type": "metaphor"}),
        ]
    )
]
LLM AGENTSRAG PIPELINESTOOL CALLINGDEPLOYMENT
Let's build

Start building AI agents with Build Fast

Explore ProgramJoin Waitlist

Step 3: Running Extractions

input_text = "Lady Juliet gazed longingly at the stars, her heart aching for Romeo"

result = lx.extract(
    text_or_documents=input_text,
    prompt_description=prompt,
    examples=examples,
    model_id="gemini-2.5-pro"
)

Step 4: Visualizing Results

lx.io.save_annotated_documents([result], output_name="extraction_results.jsonl", output_dir=".")
html_object = lx.visualize("extraction_results.jsonl")

custom_css = """
<style>
body { background-color: white !important; color: black !important; }
.entity.character { background-color: #DDEEFF !important; color: black !important; }
.entity.emotion { background-color: #C0F5C0 !important; color: black !important; }
.entity.relationship { background-color: #FFEDB3 !important; color: black !important; }
</style>
"""

html = custom_css + html_object.data
with open("visualization.html", "w") as f:
    f.write(html)

from IPython.display import HTML
HTML(html)

Step 5: Using OpenAI Models

input_text = """
The patient was prescribed Lisinopril and Metformin last month.
He takes the Lisinopril 10mg daily for hypertension, but often misses
his Metformin 500mg dose which should be taken twice daily for diabetes.
"""

prompt_description = """
Extract medications with their details, using attributes to group related information:
1. Extract entities in the order they appear in the text
2. Each entity must have a 'medication_group' attribute linking it to its medication
3. All details about a medication should share the same medication_group value
"""

examples = [
    lx.data.ExampleData(
        text="Patient takes Aspirin 100mg daily for heart health and Simvastatin 20mg at bedtime.",
        extractions=[
            lx.data.Extraction(extraction_class="medication", extraction_text="Aspirin", attributes={"medication_group": "Aspirin"}),
            lx.data.Extraction(extraction_class="dosage", extraction_text="100mg", attributes={"medication_group": "Aspirin"}),
            lx.data.Extraction(extraction_class="frequency", extraction_text="daily", attributes={"medication_group": "Aspirin"}),
            lx.data.Extraction(extraction_class="condition", extraction_text="heart health", attributes={"medication_group": "Aspirin"}),
            lx.data.Extraction(extraction_class="medication", extraction_text="Simvastatin", attributes={"medication_group": "Simvastatin"}),
            lx.data.Extraction(extraction_class="dosage", extraction_text="20mg", attributes={"medication_group": "Simvastatin"}),
            lx.data.Extraction(extraction_class="frequency", extraction_text="at bedtime", attributes={"medication_group": "Simvastatin"})
        ]
    )
]

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

Step 6: Visualizing Results

lx.io.save_annotated_documents([result], output_name="extraction_results.jsonl", output_dir=".")
html_object = lx.visualize("extraction_results.jsonl")

custom_css = """
<style>
body { background-color: white !important; color: black !important; }
.entity.character { background-color: #DDEEFF !important; color: black !important; }
.entity.emotion { background-color: #C0F5C0 !important; color: black !important; }
.entity.relationship { background-color: #FFEDB3 !important; color: black !important; }
</style>
"""

html = custom_css + html_object.data
with open("visualization.html", "w") as f:
    f.write(html)

from IPython.display import HTML
HTML(html)

Cohort program

Claude MasteryCowork & Code

Explore programJoin waitlistNo coding needed

Practical Applications

  • Literature Analysis

  • News Entity Extraction

  • Medical Record Processing

  • Business Intelligence

Troubleshooting

  • API Errors: Check API key setup.

  • Bad Results: Improve prompts or add more examples.

  • Visualization Not Working: Recheck file paths and HTML write logic.

Unrot mascot

Build Fast With AI

Learn AI in just 5 minutes a day.

4.8
Get the app

References

  • LangExtract Github : https://github.com/google/langextract

  • LangExtract Build Fast With AI Colab Notebook : https://git.new/LangExtract

Conclusion

LangExtract empowers users to extract structured insights from text with ease, leveraging AI models to handle complex analysis tasks. By defining clear prompts, providing examples, and using interactive visualizations, you can unlock the potential of text data in literature, news, medical records, and more.

Whether you’re a beginner exploring AI or a developer building advanced applications, LangExtract in Google Colab offers a seamless way to get started. Dive in, experiment with your own texts, and discover the power of structured text analysis!


Resources and Community

Join our community of 12,000+ AI enthusiasts and learn to build powerful AI applications! Whether you're a beginner or an experienced developer, this tutorial will help you understand and implement AI agents in your projects.

  • Website: www.buildfastwithai.com

  • LinkedIn: linkedin.com/company/build-fast-with-ai

  • Instagram: instagram.com/buildfastwithai

  • Twitter (X): x.com/BuildFastWithAI

  • Telegram: t.me/BuildFastWithAI

Satvik Paramkusham
Written by

Satvik Paramkusham

Founder at Build Fast with AI. AI Engineer, educator, and researcher passionate about agentic AI workflows, LLM application architecture, and training the next generation of AI builders.

Share:

Partner with Build Fast with AI

Guest posts, sponsored articles, tool reviews and in-blog CTAs. See what we offer and get in touch.

Partner with us

Topics

  • Claude AI
  • Best AI Models
  • AI Coding Tools
  • AI Agent Frameworks
  • Agentic AI
  • Open-Source LLMs
  • RAG & Vector Databases
  • AI Industry News

Popular guides

  • 100 Best AI Photo Editing Prompts
  • 75 Best ChatGPT Images 2.5 Prompts
  • 100 Best AI Video Prompts
  • Best AI Models 2026, Tested
  • Claude AI Complete Guide
  • What Is MCP?

Build Fast with AI

  • Build Fast with AI
  • AI Workshops
  • Corporate Training
  • FDE Program
  • Partner with Us
  • Contact
© 2026 Intellify Edventures Private Limited. All rights reserved.
    You Might Also Like
    ChatGPT Images 2.6: Features, Pricing and Review
    Tutorials
    ChatGPT Images 2.6: Features, Pricing and Review

    ChatGPT Images 2.6 review covering Flare vs Sunburst, GIFs, SVG output, image editing, API pricing and practical prompts for creators.

    50 Best Diwali AI Photo Prompts 2026 for Gemini, ChatGPT & Nano Banana
    Tools
    50 Best Diwali AI Photo Prompts 2026 for Gemini, ChatGPT & Nano Banana

    Create stunning Diwali photos with 50 copy-paste AI prompts for Gemini, ChatGPT and Nano Banana. Try realistic portraits, couple photos, saree looks, diyas, rangoli and festive posters.