High severity beginner · Fix: 2-5 min

ValueError

langchain.chains.base.ChainValueError

What this error means
LangChain raises ValueError when a required input variable is missing from the input dictionary passed to a chain.

Stack trace

traceback
Traceback (most recent call last):
  File "app.py", line 25, in <module>
    result = chain.run({"question": "What is AI?"})  # Missing required input 'context'
  File "/usr/local/lib/python3.9/site-packages/langchain/chains/base.py", line 120, in run
    raise ValueError(f"Missing input variables: {missing_vars}")
ValueError: Missing input variables: {'context'}
QUICK FIX
Add all required input variables to the dictionary passed to chain.run() matching the chain's input_variables exactly.

Why it happens

LangChain chains require all input variables defined in their input schema to be provided when calling run or invoke. If any required input variable is missing from the input dictionary, the chain raises a ValueError to prevent incomplete or invalid execution.

Detection

Before calling chain.run(), assert that all required input keys are present in your input dictionary or catch ValueError and log the missing keys for debugging.

Causes & fixes

1

Input dictionary passed to chain.run() is missing one or more required keys defined in the chain's input variables.

✓ Fix

Ensure the input dictionary includes all required keys exactly as defined in the chain's input_variables list before calling run.

2

Mismatch between input variable names expected by the chain and the keys used in the input dictionary (e.g., case sensitivity or typos).

✓ Fix

Verify that input variable names in your input dictionary match exactly the chain's expected input variable names, including case.

3

Using a chain composed of multiple subchains without properly mapping or passing all required inputs to each subchain.

✓ Fix

When using nested or composite chains, explicitly map and provide all required inputs for each subchain to avoid missing variables.

Code: broken vs fixed

Broken - triggers the error
python
from langchain.chains import LLMChain
from langchain.llms import OpenAI

llm = OpenAI()
chain = LLMChain(llm=llm, prompt="What is {context}? Answer briefly.", input_variables=["context"])

# Missing 'context' key in input dict triggers ValueError
result = chain.run({"question": "What is AI?"})  # This line raises ValueError
print(result)
Fixed - works correctly
python
import os
from langchain.chains import LLMChain
from langchain.llms import OpenAI

os.environ["OPENAI_API_KEY"] = "your_api_key_here"  # Set your API key securely

llm = OpenAI()
chain = LLMChain(llm=llm, prompt="What is {context}? Answer briefly.", input_variables=["context"])

# Provide the required 'context' key exactly as expected
result = chain.run({"context": "AI"})  # Fixed input dictionary
print(result)
Added the missing required input variable 'context' to the input dictionary passed to chain.run(), matching the chain's input_variables to prevent ValueError.

Workaround

Wrap the chain.run() call in try/except ValueError, catch the missing input variable error, log the missing keys, and prompt the user or fallback to default values before retrying.

Prevention

Always check and validate input dictionaries against the chain's input_variables list before execution, or use typed input models to enforce input completeness at development time.

Python 3.9+ · langchain >=0.1.0 · tested on 0.2.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.