LangChain Mastery

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 stepWhat invoke expects
ChatPromptTemplate with {a}, {b}{"a": ..., "b": ...}
Template with one variablea dict, or just the value
Chat modela string, a list of messages, or a prompt value
A dict of Runnableswhatever each of them expects, usually a dict
RunnableLambda(fn)whatever fn accepts
Python
chain.invoke({"question": "What is the refund window?", "plan": "Pro"})chain.batch([{"question": q, "plan": "Pro"} for q in questions],            config={"max_concurrency": 5})for chunk in chain.stream({"question": "...", "plan": "Pro"}):    print(chunk, end="")answer = 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_name
  • max_concurrency for batch
  • configurable values, e.g. which model
Python
chain.invoke(    {"question": "..."},    config={"run_name": "faq_answer", "tags": ["support-bot", "v3"],            "metadata": {"user_id": "u_812"}, "callbacks": [handler]},)

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 assign so it survives, or fetch it inside that step from config.
  • "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": "..."}}.