Python / Python Modern Generative AI and Agents Interview Questions
What are LangChain's core abstractions — Chains, Runnables, and the LangChain Expression Language?
LangChain's modern design (LangChain v0.2+) revolves around the Runnable interface: any component that can be invoked (prompts, LLMs, parsers, retrievers, custom functions) implements invoke(), stream(), and batch(). The LangChain Expression Language (LCEL) composes Runnables with the pipe operator |, producing a new Runnable that executes components left-to-right, automatically supporting streaming, async, and batch invocation.
This replaces the legacy LLMChain class with a more composable and transparent design. Every step is inspectable, every component is swappable, and the chain is serialisable for deployment with LangServe.
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser, JsonOutputParser
from langchain_core.runnables import RunnableLambda, RunnableParallel
llm = ChatOpenAI(model='gpt-4o-mini')
# ── Simple chain: prompt | llm | parser
prompt = ChatPromptTemplate.from_messages([
('system', 'You are a concise technical writer.'),
('user', 'Write a one-sentence definition of {concept}.'),
])
chain = prompt | llm | StrOutputParser()
print(chain.invoke({'concept': 'transformer attention'}))
# ── Streaming output
for chunk in chain.stream({'concept': 'gradient descent'}):
print(chunk, end='', flush=True)
# ── Batch invocation (runs concurrently)
results = chain.batch([
{'concept': 'RAG'},
{'concept': 'fine-tuning'},
{'concept': 'embeddings'},
])
# ── Parallel execution: run two chains simultaneously
summary_chain = (
ChatPromptTemplate.from_template('Summarise: {text}') | llm | StrOutputParser()
)
keywords_chain = (
ChatPromptTemplate.from_template('List 5 keywords from: {text}') | llm | StrOutputParser()
)
parallel = RunnableParallel(
summary=summary_chain,
keywords=keywords_chain,
)
result = parallel.invoke({'text': 'Attention mechanisms allow models to focus...'})
print(result['summary'])
print(result['keywords'])
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
