Skip to content
datarekha

Natural language inference

Does the hypothesis follow from the premise? Natural language inference asks a model to label a sentence pair entailment, contradiction, or neutral — a direct probe of whether it grasps meaning rather than surface words. It's a benchmark of understanding, and the entailment primitive now powers fact-checking and grounding for LLMs.

Written and reviewed by Last reviewed

7 min read Intermediate NLP & Transformers Lesson 10 of 44

What you'll learn

  • The NLI task — entailment, contradiction, neutral over a sentence pair
  • Why it probes real understanding, not surface overlap
  • Cross-encoder modeling and the dataset-artifact pitfall
  • How entailment now checks LLM groundedness and powers fact-checking

Before you start

Natural language inference (NLI) — also called textual entailment — asks a sharp question: given a premise and a hypothesis, does the hypothesis follow from the premise? The answer is one of three labels — entailment (it follows), contradiction (it can’t be true), or neutral (could go either way). It looks simple. Yet it is one of the cleanest tests of whether a model understands meaning rather than just matching words.

The three-way judgment

def nli(premise, hypothesis):                  # toy heuristic — real NLI uses a trained model
    p, h = set(premise.lower().split()), set(hypothesis.lower().split())
    if "not" in h and (h - {"not"}) <= p: return "contradiction"
    if h <= p:                            return "entailment"
    return "neutral"

pairs = [
    ("a man is playing guitar", "a man is playing guitar"),       # the hypothesis is stated
    ("a man is playing guitar", "a man is not playing guitar"),   # negated
    ("a man is playing guitar", "a woman is singing"),            # unrelated
]
for prem, hyp in pairs:
    print(f"P={prem!r}  H={hyp!r} -> {nli(prem, hyp)}")
P='a man is playing guitar'  H='a man is playing guitar' -> entailment
P='a man is playing guitar'  H='a man is not playing guitar' -> contradiction
P='a man is playing guitar'  H='a woman is singing' -> neutral
One premise, three hypotheses, three verdictsPREMISEa man is playing guitarH: “a man is playing guitar”H: “a man is NOT playing guitar”H: “a woman is singing”entailmentcontradictionneutral
Same premise, three hypotheses: it follows (entailment), it can’t be true (contradiction), or it’s undecided (neutral).

That toy heuristic gets these three right by word overlap and a not check, but it is a fraud. It would call “a person plays an instrument” neutral (no word overlap) when it’s really entailment, and it has no idea that “the man is asleep” contradicts “the man is playing.” Real NLI needs semantics: synonyms, negation scope, quantifiers, and world knowledge. That’s exactly why it became a key benchmark (the datasets SNLI and MNLI) for sentence understanding.

Modeling it — and a cautionary tale

NLI is sentence-pair classification. The strong approach is a cross-encoder: feed both sentences together — [premise] [SEP] [hypothesis] — into a BERT-style model so it can attend across the pair, then classify the three labels. (A bi-encoder, which turns each sentence into its own vector separately, is faster but weaker, since the two sentences never see each other.)

Why it still matters: entailment checks groundedness

NLI’s “does B follow from A?” is a reusable primitive, and it found a second life with LLMs. Faithfulness checking: does the model’s generated answer actually follow from the retrieved source, or did it hallucinate? That’s an entailment question — treat the source as the premise and each generated claim as a hypothesis, and an NLI model flags claims that aren’t entailed. The same primitive powers automated fact-checking (does the evidence entail the claim?) and zero-shot classification (classifying with no labeled training examples, by turning each label into a hypothesis such as “this text is about sports”).

In one breath

  • NLI (textual entailment) labels a (premise, hypothesis) pair as entailment, contradiction, or neutral — a clean probe of meaning, not word overlap.
  • Surface heuristics fail: real NLI needs synonyms, negation scope, quantifiers, and world knowledge — which is why SNLI/MNLI became key understanding benchmarks.
  • It’s sentence-pair classification, best done with a cross-encoder (premise [SEP] hypothesis into BERT) that attends across the pair.
  • It’s the textbook dataset-artifact cautionary tale — models scored well by reading only the hypothesis (negation tells leaked the label), learning the shortcut, not entailment.
  • The entailment primitive now checks LLM faithfulness/groundedness (does the answer follow from the source?), powers fact-checking, and enables zero-shot classification.

Quick check

Quick check

0/5
Q1What does natural language inference (NLI) classify?
Q2Why isn't surface word overlap enough to solve NLI?
Q3What is the dataset-artifact problem illustrated by SNLI?
Q4How is the NLI entailment primitive reused with modern LLMs?
Q5A support bot answers 'Refunds take 5 days' from a policy that says 'Refunds are processed within 5 business days.' You run NLI with the policy as premise and the answer as hypothesis. What do you expect, and what should you worry about?

Next

NLI’s entailment check underpins faithfulness in hallucination & grounding and rigorous agent benchmarks. As sentence-pair classification it builds on text classification with a BERT cross-encoder.

Sign in to track your progress

Completed lessons, your XP, level, and streak save to your account — it's free and takes a few seconds.

Practice this in an interview

All questions
What causes hallucinations in LLMs and how do you mitigate them?

LLM hallucinations are fluent claims that are false or unsupported because language models predict likely text rather than verify facts against a source of truth. The strongest mitigation is layered: retrieve authoritative evidence, constrain answers to that evidence, use tools or deterministic checks for exact facts, verify claims, and measure both correctness and unsupported-claim rates.

What is maximum likelihood estimation, and what is the intuition behind it?

Maximum likelihood estimation finds the parameter values that make the observed data most probable under the assumed model. Intuitively, you ask: given this data, which world would have been most likely to generate it?

What are behavioral tests for ML models (invariance, directional, and minimum-functionality tests)?

Behavioral tests check a model's input-output behavior against expectations rather than just aggregate accuracy, an idea popularized by the CheckList framework. Invariance tests assert that label-preserving perturbations do not change the prediction, directional tests assert a change moves the output the expected way, and minimum-functionality tests are simple cases the model must get right. They catch real-world failures that high overall accuracy can hide.

What causes LLM hallucinations and how can they be reduced?

Hallucinations occur because an LLM is trained to produce plausible next tokens, not verified facts — it has no internal truth-checking mechanism, only statistical patterns. Common causes include rare or conflicting training data, overconfident decoding, and prompts that lead the model to extrapolate beyond what it learned. Mitigation strategies include retrieval-augmented generation, grounding responses to retrieved sources, lowering temperature, and calibrated refusal training.

Related lessons

Explore further