AI Hints
Home›LangChain Tutorial›Runnables
LangChain · Lesson 19 of 120

Runnables

One interface.

⏱ 2 min read▶ Video at 19:38🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

Prompts, models, parsers, and chains all have something in common. They're runnables. Every runnable has the same methods, so you use them all the same way.

Runnables
From the video: Runnables
  • Prompts, models, parsers
  • All runnables
  • Same methods

The runnable interface

Every runnable has invoke, for one input. Batch, for a list of inputs. Stream, to get output in pieces. And async versions of each, starting with a, like a invoke. Because a whole chain is also a runnable, you can stream or batch an entire chain, just like a single model.

MethodDoes
invoke(x)one input, one output
batch([x, y])many inputs, in parallel
stream(x)output in pieces
ainvoke, abatch, astreamasync versions

Batching a whole chain

Here's a chain of prompt, model, and parser. Because it's a runnable, we can batch it, just like we batched a model. Run it. Three topics go in, three plain strings come out, in order. We didn't write any extra code to make the chain support batching.

runnable_batch.pypython
from langchain.chat_models import init_chat_model
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)
prompt = ChatPromptTemplate.from_template("One word that rhymes with {word}. Word only.")
chain = prompt | model | StrOutputParser()

words = chain.batch([{"word": "book"}, {"word": "page"}, {"word": "shelf"}])
print(words)
Output
['Cook', 'Sage', 'Self']

Summary

Everything you pipe together is a runnable, with invoke, batch, stream, and async versions. Chains get all of them for free. Next, turning your own functions into runnables.

Key takeaways
  • invoke, batch, stream
  • Async versions
  • Chains are runnables
Next up: RunnableLambda — your own steps