Match each framework property to the declarative or imperative paradigm it belongs to
Imperative frameworks (LangChain, LlamaIndex) make you write the prompt and wire each step; declarative frameworks (DSPy, BAML) make you declare a signature and a metric and let a compiler choose prompts and exemplars.
Imagine two cooking shows. In the first, the chef hands you a recipe step by step: chop the onion, sauté at medium, add salt at minute three. You can stop at any step and inspect what is in the pan. In the second, you tell the kitchen, 'I want a dish that scores nine out of ten on this taste test,' and a robotic chef tries combinations against your taste-test rubric until it finds the best one. You do not write the recipe. You write the scoring rubric. The first show is imperative cooking; the second is declarative cooking. Both produce dinner; they ask you for very different artefacts.
Detailed answer & concept explanation~4 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
5 minutes: who owns the prompt, why optimizer-selected exemplars are the tell, and when production stacks mix both paradigms.
| Aspect | Imperative (LangChain, LlamaIndex) | Declarative (DSPy, BAML) |
|---|---|---|
| Developer writes | The prompt + the wiring | The signature + the metric |
| Few-shot exemplars | Hand-picked | Optimizer-selected from labelled data |
| Mental model | Plumbing / Runnable composition | PyTorch-style program + optimizer + metric |
| Step-level debuggability | First-class. Every Runnable is inspectable | Limited. Compiled prompt is the artefact |
| Model migration cost | Re-tune prompts per provider quirk | Re-compile against the new model |
| Best fit | Orchestration, RAG plumbing, agents | Prompt-sensitive cores with labelled data |
Real products, models, and research that use this idea.
- DSPy's original Stanford NLP paper framed prompts as a search problem over a program + metric, popularising the declarative paradigm in 2023.
- BAML (from Boundary) ships a declarative IDL for structured-output extraction with provider-portable compiled prompts, used in production by Anthropic-adjacent teams.
- LangChain Expression Language (LCEL) is the canonical example of imperative composition: `prompt | model | StrOutputParser()` builds a Runnable you can `.invoke` or `.stream`.
- LlamaIndex's QueryEngine API is imperative in flavour, you wire retriever, postprocessors, and response synthesizer explicitly, though it shares retrieval primitives with declarative pipelines.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow would you decide whether to put a particular task on the declarative or imperative side?
QWhat happens to a DSPy program when you swap the underlying model?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Calling DSPy 'just another prompting library' and missing that the load-bearing object is the compiler that searches for prompts and exemplars against a metric.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.