Skip to main content

DSPy

DSPy is a programming framework, originally from Stanford, that treats prompts as functions with typed signatures rather than strings. You declare input and output types for a task — for example, "question -> answer" or "context, question -> rationale, answer" — and DSPy compiles optimized prompts, including few-shot examples selected from training data and optional rationales. Because the program is the signature, not a hand-written string, you can swap the underlying model without rewriting prompts and re-compile to let the framework reselect examples and instructions for the new model. DSPy fits teams that want to iterate on prompting like code, with versioning, tests, and automated optimization, rather than by copy-paste.

Example

A classification module is declared as `classify = dspy.Predict("ticket -> category, reason")`. The team provides 40 labeled tickets as a training set; DSPy's compiler picks an effective few-shot subset and wraps the final prompt. When the team moves from one provider to another, they re-run the compiler without editing any prompt string — the signature stays, the compiled prompt is regenerated against the new model.

Put this into practice

Build polished, copy-ready prompts in under 60 seconds with SurePrompts.

Try SurePrompts