Course Content
LangChain Mastery
7 sections · 109 lessons
How do you pass inputs to a LangChain chain?
What you need to know
A chain's input type is the input type of its first step.
| First step | What invoke expects |
|---|---|
ChatPromptTemplate with {a}, {b} | {"a": ..., "b": ...} |
| Template with one variable | a dict, or just the value |
| Chat model | a string, a list of messages, or a prompt value |
| A dict of Runnables | whatever each of them expects, usually a dict |
RunnableLambda(fn) | whatever fn accepts |
1chain.invoke({"question": "What is the refund window?", "plan": "Pro"})23chain.batch([{"question": q, "plan": "Pro"} for q in questions],4 config={"max_concurrency": 5})56for chunk in chain.stream({"question": "...", "plan": "Pro"}):7 print(chunk, end="")89answer = await chain.ainvoke({"question": "...", "plan": "Pro"})Data versus config
Input (data)
- Template variables
- The user's question, documents
- Changes the answer
config (run settings)
callbacks,tags,metadata,run_namemax_concurrencyfor batchconfigurablevalues, e.g. which model
1chain.invoke(2 {"question": "..."},3 config={"run_name": "faq_answer", "tags": ["support-bot", "v3"],4 "metadata": {"user_id": "u_812"}, "callbacks": [handler]},5)Tags and metadata show up in LangSmith, so you can filter all runs for one user or one prompt version. Keeping them out of the input dict means they never leak into the prompt.
Carrying inputs forward
RunnablePassthrough.assign(docs=retriever_step) keeps question and adds docs. itemgetter("question") from Python's operator module picks one key out of the dict, which is useful at the start of a branch that needs only the question.
When inputs are wrong
A missing variable raises a KeyError naming the variable, before any model call. chain.input_schema.model_json_schema() shows what a chain expects, which helps when you inherit someone else's chain.
A real-life example
A support bot over a SaaS company's help-centre docs passed user_id inside the input dict so it would appear in logs. One day a prompt change added {input} to the template, and the whole input dict, including user_id and an internal account tier, was pasted into the prompt and once echoed back to a customer. The fix was to move user_id and tier into config["metadata"], where LangSmith records them but the prompt never sees them, and to keep the input dict to question and history only.
Follow-up questions to expect
- "How do you pass a value that only a later step needs?" — Include it in the input dict and use
assignso it survives, or fetch it inside that step fromconfig. - "How do you see a chain's expected input?" —
chain.input_schema.model_json_schema(), or read the first step's variables. - "How do you choose the model per call?" — Make it configurable and pass
config={"configurable": {"model": "..."}}.