Skip to content

DSPy Assertions: computational constraints for self-refining pipelines

optionalextendedfundamentalpaperrubric 6/122023 · arXiv · ~15 min

Arnav Singhvi, Manish Shetty, Shangyin Tan, Christopher Potts, Koushik Sen, Matei Zaharia, Omar Khattab

Read first: DSPy: compiling declarative LM calls into self-improving pipelines

Status

TL;DR. “Prompt engineering for rules” becomes code. dspy.Assert(cond, msg) and dspy.Suggest(cond, msg) declare constraints, for example “each query must be under 100 characters” or “every claim must cite a passage”. On failure, the module is retried with the error in its prompt, and compilation uses the constraints too (below).

  • Before: DSPy, Self-Refine, Reflexion.
  • After: GEPA, which turns textual feedback into prompt updates rather than one-off retries.

Questions to think through

These questions have no single right answer. Write or say your answer before you open the hints. Mark a question once you could defend your answer in a reading group.

  1. Mechanism

    When a DSPy Assertion or Suggestion fails, the module is retried with its past failing output and the error message added to the prompt. Why should this beat simply resampling at a higher temperature? Use the TweetGen results (no-hashtag rate 21.3 → 71.7 with inference-time assertions, but faithfulness 78.3 → 67.7) to explain the trade-off it creates.

    What a strong answer covers
    • Resampling draws again from the same distribution, so a likely failure tends to repeat. The feedback conditions the next attempt on what went wrong, which shifts the distribution.
    • The error message works as a targeted instruction that appears only when needed, so the base prompt stays short instead of listing every constraint up front.
    • Trade-off: retries optimize only the constraint being checked. Rewriting to drop hashtags or fit a length limit can change or drop content, so unchecked qualities such as faithfulness can fall.
    • Remedies: assert the quality you care about if it can be checked, or compile with assertions so the first attempt already satisfies constraints. In TweetGen, Compile w/ Assert and C+Infer w/ Assert raise engagement and overall quality.
  2. Derive / compute

    In a 3-call multi-hop program, each of the two query modules is wrapped in a Suggest with R = 2 retries; the answer module is not. Assume each attempt independently satisfies the constraint with probability p = 0.7. (a) What is the expected number of LM calls per query module and per program run? (b) What pass rates does independence predict, per module and for both modules? (c) The paper's MultiHopQA suggestion pass rate rises from 66.3% to 89.7% on dev with inference-time assertions. What does the gap to your prediction suggest?

    What a strong answer covers
    • (a) 1 + 0.3 + 0.3² = 1.39 calls per query module, so 2 × 1.39 + 1 = 3.78 calls per run, about 26% more than the plain 3.
    • (b) Per module 1 − 0.3³ = 97.3%; for both modules about 0.973² ≈ 94.7%.
    • (c) The observed rate is lower than predicted, which suggests retries are correlated: some inputs fail repeatedly because the constraint is hard for that question, not because of sampling noise.
    • Probe the setup before trusting the comparison: it is not obvious whether the reported pass rate is per check or per run, and p is not observed directly, so treat p and R as assumptions to check against logged retry counts.
    • Counterexample bootstrapping, which turns failed traces and their fixes into demos, aims to raise first-attempt p so that inference-time retries are rarely needed.
  3. What if…

    On LongFormQA, compiling with assertions plus inference-time assertions (C+Infer w/ Assert) raises faithfulness from 77.3 to 92.7 and citation precision from 58.1 to 80.1 on dev, but citation recall of gold titles falls from 51.5 to 43.8 and Has Answer on test falls from 60.2 to 55.4. How could the asserted constraints produce this pattern, and what ablation would test your explanation?

    What a strong answer covers
    • The Suggestions require a citation every one or two sentences and text that is faithful to its citation. The cheapest way to comply is to say less and cite safely, so precision and faithfulness rise.
    • Saying less means fewer gold titles cited and sometimes leaving out the answer, so recall and Has Answer fall. Assertions optimize what they check, not coverage.
    • Ablation: enable each Suggestion alone (citation frequency only, faithfulness only) and log answer length and citations per output next to the metrics.
    • Add a coverage constraint (the answer must appear, or at least k distinct sources must be cited) and check whether recall recovers without losing precision.
    • Compare against Compile w/ Assert without inference-time retries to see whether the drop comes from retry rewriting or from the constraints shaping the demos.

Done with this page?

Status