Unstable AI? How to use DSPy to structure prompts
Learn how to replace manual prompt engineering with declarative programming using DSPy, ensuring stability and accuracy in language models.
Summary
- Manual prompt engineering fails when switching models because it relies on brittle free text and trial-and-error.
- Declarative programming separates business intent from the specific formatting details required by each LLM.
- DSPy treats prompts as compiled modules that are automatically optimized through code.
- Defining clear signatures replaces ambiguous text with well-typed input and output contracts.
- Internal optimizers automatically tune examples and instructions based on real validation metrics.
Why manual prompt engineering has reached its limits
Anyone who has tried to build a corporate system using generative artificial intelligence quickly realizes the frustration of manual prompt tuning. Changing a single word in a massive prompt can make the model forget critical rules, break the expected output format, or invent information out of nowhere. This happens because we treat language models as if they were humans sensitive to poetry, when in fact they require predictable software engineering. To understand the risks of this instability, it is helpful to review the techniques discussed in how to build strict validation guardrails to contain unwanted behaviors.
What declarative programming means in artificial intelligence
Declarative programming is a concept where you tell the computer what you want to achieve, rather than dictating step-by-step how it should perform each task. In the context of language models, this means stopping the practice of writing long texts full of complex instructions and starting to define clean signatures, such as inputs, outputs, and expected data types. Instead of hoping the intelligence understands your formatting, you provide a strict data structure. This brings the same peace of mind to AI development that we get from using static typing in languages like TypeScript or Go.
How DSPy turns prompts into reusable code
Created at Stanford University, DSPy is a framework that removes the manual labor of crafting and tuning text for language models. It turns your prompts into code modules that can be chained, tested, and optimized automatically. In practice, you define a signature stating what goes in and what comes out, and the framework takes care of assembling the ideal prompt for whichever model you decide to use. If you need to migrate from an expensive model to a smaller local model, for example, your code keeps working perfectly without requiring you to rewrite hundreds of instruction phrases.
Implementing a declarative signature in practice
To see this logic in action, imagine we need to extract customer sentiment and key entities from user messages in a fully structured way. Instead of creating a giant text explaining the task, we define a simple declarative block using Python. The following code demonstrates how to structure this task using DSPy, ensuring the result returns precisely in the format expected by your backend system.
import dspy
class ExtractCustomerAnalysis(dspy.Signature):
"""Analyzes customer text and extracts sentiment and key points."""
customer_text = dspy.InputField(desc='Support ticket message received')
sentiment = dspy.OutputField(desc='Positive, Neutral, or Negative')
key_points = dspy.OutputField(desc='Summary list of mentioned issues')
analyzer = dspy.Predict(ExtractCustomerAnalysis)
result = analyzer(customer_text='The system crashed during checkout and I lost precious time.')
print(result.sentiment, result.key_points)
The role of compilers and automatic optimizers
One of the greatest advantages of adopting this code-driven approach is the ability to optimize system behavior mathematically. Much like a programming language compiler optimizes machine code to run faster, DSPy features optimizers that tune the examples provided to the model. It tests different prompt variations and automatically selects which examples yield the most accurate responses for your actual database. To ensure this evolution remains safe, the process can be integrated into continuous testing routines, similar to the concepts in how to build tests with Ragas to measure response reliability.
Operational benefits of abandoning free text
Adopting declarative programming instead of handcrafted engineering brings clear maintenance and scalability gains for engineering teams. When an AI model gets updated by its provider and changes its behavior, tweaking a manual prompt scattered across dozens of system files is a logistical nightmare. With DSPy, if the structural logic changes, you update the central signature or run the optimizer again to adapt the examples. This reduces support costs, cuts down rework, and lets the team focus on business logic rather than guessing magic words to convince the AI to cooperate. To explore these architectural concepts further in production, see our related technical analysis on Unstable Node.js API? How to apply circuit breaker.
Final thoughts on the future of prompts
Trial-and-error prompt engineering has hit a ceiling of sustainability in real software projects. Treating instructions for artificial intelligence models as structured, testable, and compilable code is the natural path to stabilizing corporate applications. Tools like DSPy show that the future of interaction with language models lies in traditional software engineering, where clear contracts, strict validations, and automation replace human intuition and free text.