LLMGraphTransformer

Writing an entity extractor by hand takes weeks. Regex catches the easy cases and misses everything else. LangChain ships a component called LLMGraphTransformer that hands the problem to an LLM with structured output. You give it text, it returns GraphDocuments: nodes, relationships, and the source metadata. That is the extraction pipeline you would have built by hand, done for you.

LLMGraphTransformer pipeline

Raw text goes in, GraphDocuments come out, Neo4j stores them.

ingestion.py
python
from langchain_experimental.graph_transformers import LLMGraphTransformer
from langchain_openai import ChatOpenAI

def _build_transformer():
    """Build an LLMGraphTransformer using a LangChain-compatible chat model."""
    model = os.getenv("INGESTION_MODEL") or os.getenv("OPENAI_MODEL", "gpt-4o-mini")
    api_key = os.getenv("OPENAI_API_KEY") or os.getenv("OPENROUTER_API_KEY")
    base_url = os.getenv("OPENAI_BASE_URL")

    llm = ChatOpenAI(
        model=model,
        temperature=0,
        api_key=api_key,
        base_url=base_url,
    )
    return LLMGraphTransformer(llm=llm)

Temperature zero is deliberate. Extraction is not a creative task. You want the same text to produce the same triples every run so your graph is stable. Any OpenAI-compatible chat model works here; OpenRouter, Fireworks, and Gemini all satisfy the tool-calling requirement.

It relies on tool calling or structured output to force the LLM to return valid GraphDocuments. Completion-only models do not support the schema-enforced output format. A chat model with function or tool support guarantees the response parses into nodes and relationships, so you do not waste tokens handling malformed JSON.

Quiz: Quiz

Loading practice…