Contrast declarative and imperative LLM frameworks on what each optimizes for, and pick which to use for a metric driven extraction pipeline
Contrast declarative LLM frameworks (DSPy, BAML) with imperative ones (LangChain, LlamaIndex) on what each optimizes for. Then pick the family you would use for a metric driven information extraction pipeline that runs on a labeled trainset, and defend the choice.
Declarative frameworks compile prompts against a metric and a trainset, imperative ones execute prompts you wrote, so a labeled extraction job is a near-perfect fit for DSPy or BAML.
Imagine you want to bake bread. The imperative approach is a recipe you write down: do step 1, then 2, then 3. If the bread is bad, you tweak the recipe by hand. The declarative approach is more like telling an apprentice 'here are 200 loaves I judged good or bad, and here is the taste test I care about, go figure out a recipe that scores well.' The apprentice tries variations and reports back the best one. With labeled data and a clear metric, the apprentice wins easily. Without them, the recipe you write yourself is more readable and easier to fix.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Declarative vs imperative is the single most useful framework-level distinction in 2026, and it routinely gets muddled with composition styles or syntax preferences. The clean framing: declarative frameworks treat the prompt as the output of a compile process driven by a metric and examples; imperative frameworks treat the prompt as the input you author and the chain executes.
Most teams adopt LangChain or LlamaIndex first because composition and observability are concrete on day one. Declarative frameworks like DSPy and BAML feel abstract until you have a labeled trainset and an evaluation metric, at which point the optimizer becomes the obvious leverage. The question in the prompt is exactly that flip-point.
Mental model: ask 'who tunes the prompt?' If the answer is 'the developer,' you want imperative. If the answer is 'an optimizer against a metric,' you want declarative.
What declarative actually means in DSPy and BAML
DSPy: program + optimizer + metric
DSPy is the canonical declarative LLM framework in 2026. You write three things:
- A Signature that names input and output fields with semantic descriptions, e.g.
question -> answerwith docstrings. - A Module like
dspy.Predictordspy.ChainOfThoughtthat turns the Signature into a callable LLM step. - An Optimizer like
BootstrapFewShot,COPRO, orMIPROv2that takes the Module plus a metric and a trainset and produces a compiled program with frozen instructions and demonstrations.
You never write the final prompt. The optimizer searches the prompt and demonstration space and emits one that scores well on the metric.
BAML: schema-first codegen
BAML is declarative in a different sense. You declare function signatures in a BAML file (a typed schema with input and output shapes, plus prompts as a separate concern). BAML's compiler generates client code in TypeScript or Python with parsing, retries, and validation baked in. The 'declarative' part is the schema is the source of truth and the call sites get type-checked at compile time. There is no metric-driven optimization built in, but there is no hand-written parsing either.
The common thread
Both move work from runtime authorship to compile-time generation. DSPy moves the prompt itself. BAML moves the parsing and the type contract. Both reduce the surface area you tune by hand and ask you to commit to a higher-level declaration.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- DSPy's STORM (Stanford) compiles multi-hop research agents from a metric on draft quality, the prompts are not hand-written.
- BAML at Lyft and Notion generates typed extraction functions across TS and Python from one schema, with parsing built in.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow does DSPy's MIPROv2 differ from BootstrapFewShot, and when do you pick each?
BootstrapFewShot freezes prompt instructions and only searches over demonstrations, fast and good for tight extraction tasks. MIPROv2 jointly optimizes instructions and demonstrations using a proposer LLM, more expensive but better when the prompt instructions themselves need to evolve. Use BootstrapFewShot first, escalate to MIPROv2 when it plateaus.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Treating DSPy as 'just another chain library.' The whole point is the compile step: signature + module + optimizer + metric, not pipe-operator composition.
60 second bullets to scan on the way to the call.
The declarative vs imperative axis in one sentence each
What DSPy's compile step actually does (Signature, Module, Optimizer, Metric, trainset)
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.