Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Stop Writing Prompts and Start Programming Them with DSPy
Artificial IntelligenceProgramming

Stop Writing Prompts and Start Programming Them with DSPy

A Practical Guide to Building Robust AI Systems with Python

by Arsenii Drobotenko

Data Scientist, Ml Engineer

Feb, 2026
6 min read

facebooklinkedintwitter
copy
Stop Writing Prompts and Start Programming Them with DSPy

You have likely experienced "Prompt Engineering Hell". You spend hours tweaking a prompt, adding phrases like "You are a helpful assistant" or "Think step by step", and finally get the result you want. But then, you change the model from GPT-4 to a smaller open-source model, or you change one sentence in the input, and the entire system breaks.

This approach is fragile. It treats building AI software like creative writing rather than engineering.

DSPy (declarative self-improving language programs) is a framework from Stanford that changes this paradigm. It argues that you should stop writing prompts entirely. Instead, you should program your AI using Python classes and logic.

The Core Concept: Logic vs Parameters

In traditional AI development, the "prompt" is a messy mix of three things:

  1. Logic (what the task is);
  2. Instruction (how to behave);
  3. Examples (few-shot learning).

DSPy separates these. You write the logic in Python. The DSPy compiler then figures out the optimal instructions and examples for you.

Step 1 Defining Signatures ( The "Interface" )

In DSPy, you do not write long text instructions. Instead, you define a Signature. This is a declarative specification of what goes in and what comes out. It looks very similar to defining a Pydantic model or a dataclass.

Here is how you define a basic Question-Answering signature:

copy

Syntax Breakdown

  • Docstring ("""..."""): this is crucial. DSPy uses this string to tell the model the high-level goal of the task;
  • dspy.InputField: defines what data you will send to the model. The desc argument adds specific hints to the prompt;
  • dspy.OutputField: defines what you expect back.

Step 2 Building Modules ( The "Logic" )

Signatures define the "what". Modules define the "how". A Module in DSPy is a class that uses one or more signatures to perform a task. If you have used PyTorch, this structure will feel very familiar.

Let's build a module that uses ChainOfThought (reasoning before answering) to solve our BasicQA task.

copy

Syntax Breakdown

  • __init__: you define the sub-modules you need. dspy.ChainOfThought is a built-in module that automatically adds "Let's think step by step" logic to your signature;
  • forward: this is the execution method. You pass the input arguments (matches the InputField name) and return the result.

Running the code:

copy

Step 3 Compilation ( The "Magic" )

This is the most practical part. You have written the code, but you haven't provided any examples (few-shot learning). In a standard workflow, you would manually paste examples into the prompt. In DSPy, you use an Optimizer (formerly called Teleprompter).

The Optimizer runs your program against a training set, checks which examples help the model the most, and compiles a new, optimized program.

copy

image

Comparison of Manual Prompting vs DSPy

FeatureManual Prompt EngineeringDSPy Programming
Development ApproachTrial and error with text strings ("art")Modular coding with Python classes ("engineering")
Code StructureGiant f-strings with variablesClean `Signature` and `Module` classes
OptimizationManual tweaking of adjectivesAlgorithmic compilation (`optimizer.compile`)
ReproducibilityLow (Prompts break easily)High (Logic remains stable)

Practical Use Cases

DSPy is particularly powerful for complex pipelines where a single prompt is not enough.

Multi-Hop Retrieval (RAG)

If a user asks a question that requires searching for information, summarizing it, and then searching again, maintaining context in a single string is difficult. With DSPy, you can define this as a Python loop inside the forward method. The compiler will optimize the instructions for each step of the search independently.

Structured Data Extraction

When you need to extract specific fields (like names, dates, amounts) from messy text, manual prompts often fail on edge cases. You can define a Signature with strict output fields, and DSPy's optimizers will find the specific combination of examples that minimizes parsing errors.

Was dit artikel nuttig?

Delen:

facebooklinkedintwitter
copy

Was dit artikel nuttig?

Delen:

facebooklinkedintwitter
copy

Inhoud van dit artikel

Volg ons

trustpilot logo

Adres

codefinity
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?
some-alt