Zenaique

Contrast declarative and imperative LLM frameworks on what each optimizes for, and pick which to use for a metric driven extraction pipeline

Short answer·Hard·4.0 · 0·~3 min·Asked atBcgInduced AiWandb
Attempt it

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.

Free · 2 AI evals / day
TL;DR

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.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

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.

Key concepts

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 -> answer with docstrings.
  • A Module like dspy.Predict or dspy.ChainOfThought that turns the Signature into a callable LLM step.
  • An Optimizer like BootstrapFewShot, COPRO, or MIPROv2 that 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.

What imperative actually means in LangChain and LlamaIndex
Why a labeled extraction pipeline is the declarative sweet spot
The practical recommendation in 2026
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.

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.
Sign in to see more production examples.

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?
A

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.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Treating DSPy as 'just another chain library.' The whole point is the compile step: signature + module + optimizer + metric, not pipe-operator composition.

Sign in to see all red flags and common mistakes.

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)

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
Defend the call to…
Short answer·Hard