Skip to main content

Menu

LEVEL 0
0/5 XP
HomeAboutTopicsPricingMy VaultStats

Categories

🤖 Artificial Intelligence
☁️ Cloud and Infrastructure
💾 Data and Databases
💼 Professional Skills
🎯 Programming and Development
🔒 Security and Networking
📚 Specialized Topics
HomeAboutTopicsPricingMy VaultStats
LEVEL 0
0/5 XP
GitHub
© 2026 CheatGrid™. All rights reserved.
Privacy PolicyTerms of UseAboutContact

DSPy – Declarative AI Programming Cheat Sheet

DSPy – Declarative AI Programming Cheat Sheet

Back to AI and Machine Learning
Updated 2026-05-18
Next Topic: Edge AI and TinyML Cheat Sheet

DSPy (Declarative Self-improving Python) is a Stanford-developed framework that treats prompts as compilable programs rather than brittle strings, enabling systematic optimization of LLM pipelines through algorithms instead of manual prompt engineering. Unlike traditional orchestration tools like LangChain that focus on integration patterns, DSPy emphasizes automatic prompt and weight optimization, abstracting LMs into modular, composable building blocks with signatures (input-output specs), modules (reasoning strategies), and teleprompters (optimizers) that compile programs into optimized prompts and few-shot examples tailored to specific tasks and metrics. The central insight: treating prompting as a compilation problem—writing declarative task specifications and letting DSPy's compilers search over prompt variations, instruction phrasing, and example selections to maximize objective metrics—fundamentally shifts LLM development from manual trial-and-error to data-driven, reproducible engineering.

What This Cheat Sheet Covers

This topic spans 20 focused tables and 126 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.

Table 1: Core DSPy AbstractionsTable 2: Signatures – Defining Task SpecificationsTable 3: Built-In Modules – Reasoning StrategiesTable 4: Optimizers – Automatic Prompt ImprovementTable 5: Assertions and ConstraintsTable 6: RAG (Retrieval-Augmented Generation)Table 7: Agents and Tool UseTable 8: Evaluation and MetricsTable 9: Model Configuration and AdaptersTable 10: Structured Outputs and Type SafetyTable 11: Advanced Optimization StrategiesTable 12: Caching and Performance OptimizationTable 13: Production DeploymentTable 14: Signature Field ConfigurationTable 15: Conversation History ManagementTable 16: Debugging and ObservabilityTable 17: DSPy vs AlternativesTable 18: Common Patterns and Best PracticesTable 19: Learning ResourcesTable 20: Advanced Modules and Utilities

Table 1: Core DSPy Abstractions

DSPy's three foundational primitives—signatures, modules, and optimizers—define a programming model where you declare what your system should accomplish, choose how to structure reasoning, and compile the program into optimal prompts. Understanding these building blocks is essential: signatures specify task semantics, modules provide reusable reasoning patterns, and optimizers automate the search for high-performing configurations.

ConceptExampleDescription
Signature
"question -> answer"
class QA(dspy.Signature):
question = dspy.InputField()
answer = dspy.OutputField()
Declarative input-output specification for LM behavior; can be a short string or a class with typed fields and descriptions. Acts as the contract between your program and the LM.
Module
dspy.Predict(QA)
dspy.ChainOfThought(QA)
dspy.ReAct(tools=[...])
Reusable reasoning strategy that wraps a signature; abstracts prompting techniques (e.g., chain-of-thought, ReAct) into parameterized components. All modules are subclasses of dspy.Module.
Optimizer (Teleprompter)
teleprompter = dspy.BootstrapFewShot()
compiled = teleprompter.compile(
program, trainset=data, metric=accuracy)
Algorithm that tunes prompts and/or LM weights to maximize a metric; searches over instruction phrasing, few-shot examples, and reasoning steps. Historically called "teleprompters."
Example
dspy.Example(
question="What is 2+2?",
answer="4").with_inputs("question")
Data container for train/dev/test sets; wraps input-output pairs with optional .with_inputs() to mark which fields are inputs vs labels.

More in AI and Machine Learning

  • Domain-Specific Language Models Cheat Sheet
  • Edge AI and TinyML Cheat Sheet
  • AI Bias & Fairness Cheat Sheet
  • Feature Engineering Cheat Sheet
  • ML for Tabular Data Cheat Sheet
  • PyTorch Cheat Sheet
View all 65 topics in AI and Machine Learning