Open Source Other Prompt OptimisationStanfordPythonFrameworkFine-Tuning

DSPy

Stanford framework that replaces prompt engineering with optimisers. Define your program logic, then DSPy compiles it into optimised prompts and fine-tunes using your examples.

stanfordnlp/dspy 24.0k stars 1.9k forks MIT

Install

$pip install dspy

About

DSPy replaces hand-written prompts with typed signatures. A signature like "question -> answer" compiles into a prompt, and an optimiser automatically improves it by running your evaluation metric on training examples.

This makes LLM programs more robust — instead of brittle string prompts, you have Python modules with defined interfaces that can be optimised, composed, and tested systematically.

Getting Started

pip install dspy

import dspy

lm = dspy.LM("anthropic/claude-sonnet-4-5", api_key="...")
dspy.configure(lm=lm)

class QA(dspy.Signature):
    """Answer questions with short factual answers."""
    question: str = dspy.InputField()
    answer: str = dspy.OutputField()

qa = dspy.ChainOfThought(QA)
result = qa(question="What is the capital of France?")
print(result.answer)

Features

Signatures — declarative input/output specs instead of prompt strings
Modules — composable LLM programs (ChainOfThought, ReAct, ProgramOfThought)
Optimisers — automatically improve prompts using labelled examples (BootstrapFewShot, MIPROv2)
Assertions — runtime constraints that trigger optimiser feedback
Evaluation harness with metric-driven optimisation
Works with any LLM (Claude, GPT-4o, Gemini, Ollama)
Fine-tuning adapter — export optimised programs to LoRA fine-tunes

References & Links

Free Open Source · MIT
View on GitHub Visit Homepage

Project Info

Language Python
License MIT
Stars 24.0k ★
Forks 1.9k
Category Other

Topics

Prompt OptimisationStanfordPythonFrameworkFine-Tuning

Ready to try DSPy?

Free and open source. Install in one command, works with Claude Code, Cursor, Windsurf, and more.

$pip install dspy
← Back to Marketplace