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'])
More Related questions...