RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Fredoline Eruo
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /How-to
  5. /How to Implement Semantic Chunking with LangChain
HOW-TO · RAG

How to Implement Semantic Chunking with LangChain

intermediate·20 min·By Fredoline Eruo
Target environment
Ubuntu 24.04 · Ollama 0.4.x
PREREQUISITES

LangChain installed, embedding model

What this does

Semantic chunking splits documents at natural topic or meaning boundaries rather than fixed character counts. The algorithm embeds sentences, measures cosine similarity between consecutive embeddings, and breaks the document whenever similarity drops below a threshold. This produces chunks that cohere around single ideas.

Steps

  1. Confirm the embedding model is available.

    ollama pull nomic-embed-text
    
  2. Import the semantic chunker.

    from langchain_experimental.text_splitter import SemanticChunker
    from langchain_ollama import OllamaEmbeddings
    
    embeddings = OllamaEmbeddings(model="nomic-embed-text")
    chunker = SemanticChunker(embeddings, breakpoint_threshold_type="gradient")
    
  3. Load a document and split it semantically.

    from langchain_community.document_loaders import TextLoader
    
    loader = TextLoader("/data/article.txt")
    docs = loader.load()
    semantic_chunks = chunker.split_documents(docs)
    print(f"Produced {len(semantic_chunks)} semantic chunks")
    
  4. Adjust sensitivity with a custom threshold.

    fine_chunker = SemanticChunker(
        embeddings,
        breakpoint_threshold_type="percentile",
        breakpoint_threshold_amount=50,
    )
    fine_chunks = fine_chunker.split_documents(docs)
    print(f"Fine-grained: {len(fine_chunks)} chunks")
    

Verification

python -c "
from langchain_experimental.text_splitter import SemanticChunker
from langchain_ollama import OllamaEmbeddings
e = OllamaEmbeddings(model='nomic-embed-text')
c = SemanticChunker(e)
print('SemanticChunker instantiated OK')
"
# Expected: SemanticChunker instantiated OK

Common failures

  • ConnectionError on embed call. Ollama not listening on port 11434. Check with curl http://localhost:11434/api/tags.
  • One monolithic chunk returned. Threshold too high. Lower breakpoint_threshold_amount or switch to "gradient" type.
  • Slow on long documents. Per-sentence embedding is O(n). Batch sentences or pre-fetch embeddings into a cache.
  • ImportError for langchain_experimental. Install with pip install langchain-experimental.
  • Version mismatch - The installed package or runtime differs from the command shown; check the version first and rerun the smallest verification command.
  • Local environment drift - Another service, virtual environment, model, or path is being used; print the active binary path and configuration before changing the guide steps.

Operator checkpoint

Before treating this as solved, write down the local runtime, model or package version, hardware/backend if relevant, and the verification output. This keeps the guide useful as a Will-It-Run style decision instead of a one-off command transcript.

Related guides

  • use-recursive-character-text-splitter
  • implement-hybrid-chunking-semantic-fixed
RELATED GUIDES
RAG
How to Implement Hybrid Chunking (Semantic + Fixed Size)
RAG
How to Use Recursive Character Text Splitter
← All how-to guidesCourses →