Ultralytics YOLO27: Join the waitlist.
License YOLO
Back to Ultralytics Glossary

DSPy

DSPy is an open-source framework that builds LLM pipelines from typed signatures and modules, then optimizes their prompts against a metric.

DSPy is an open-source Python framework for building applications on large language models (LLMs) as code instead of hand-written prompts. A developer declares what each step takes in and returns, composes those steps into a program, and lets a DSPy optimizer write and tune the prompts, or the model's weights, to maximize a metric on a set of examples. The name stands for Declarative Self-improving Python, and the project describes itself as the framework for programming, rather than prompting, language models. Omar Khattab and colleagues at Stanford introduced it in the October 2023 paper DSPy: Compiling Declarative Language Model Calls into Self-Improving Pipelines. Within minutes of compiling, a few lines of DSPy let GPT-3.5 and Llama-2-13B-chat build pipelines that beat standard few-shot prompting by over 25% and 65% respectively, and pipelines using expert-written demonstrations by up to 46% and 40%.

Signatures, Modules, and Optimizers#

  • Signatures: Typed declarations of a step's inputs and outputs, written inline as "question -> answer" or as a class whose docstring states the task. They state what a step does and leave the prompt wording to DSPy.
  • Modules: Strategies for running a signature. dspy.Predict calls the model once, dspy.ChainOfThought adds a reasoning field before the answer as in chain-of-thought prompting, and dspy.ReAct runs a tool-using loop in the style of ReAct prompting. Modules compose like PyTorch layers into multistep programs, such as a retrieval-augmented generation (RAG) pipeline that writes a search query, retrieves passages, and answers from them.
  • Adapters: Translate a signature into chat messages for the model and parse its reply back into typed fields.
  • Optimizers: Algorithms that improve a program against a metric. BootstrapFewShot runs the program on training examples and keeps the traces that pass the metric as demonstrations, MIPROv2 searches over proposed instructions and demonstration sets, and BootstrapFinetune distills a program into model weights through fine-tuning.

The optimization loop mirrors training a model, with prompts as the parameters:

The 2025 GEPA optimizer, available as dspy.GEPA, reads the program's execution traces and textual feedback, has an LLM reflect on what went wrong, and evolves the instructions from there. Across six tasks it beat the reinforcement learning method GRPO by 6% on average and up to 20% while using up to 35 times fewer rollouts, and beat MIPROv2 by over 10%.

From Signature to Prompt#

DSPy writes the prompt from the signature, so the same program can be re-optimized for a new model instead of rewritten. Here the input is a detection summary from Ultralytics YOLO26: Results.verbose() returns a predict mode result as per-class counts in a short string, which a signature can take as text so that an optimizer tunes the reasoning step over real object detection output. Formatting the messages needs no model or API key:

import dspy
from ultralytics import YOLO

model = YOLO("yolo26n.pt")
results = model("https://ultralytics.com/images/bus.jpg")


class SceneQA(dspy.Signature):
    """Answer a question about a photo from the objects a detector found in it."""

    detections: str = dspy.InputField()
    question: str = dspy.InputField()
    answer: str = dspy.OutputField()


# The adapter turns the signature and inputs into the chat messages the model will receive
inputs = {"detections": results[0].verbose(), "question": "How many people are in the photo?"}
messages = dspy.ChatAdapter().format(SceneQA, demos=[], inputs=inputs)
for message in messages:
    print(f"--- {message['role']} ---\n{message['content']}")

The system message lists the input and output fields, the [[ ## field ## ]] markers DSPy uses to parse the reply, and the docstring as the objective, and the user message carries the detector's counts. An optimizer improves this program by rewriting that objective and adding demonstrations to the messages, leaving the Python code unchanged.

  • Prompt engineering: Writing and revising prompts by hand. DSPy turns that work into an optimization problem with a metric and examples.
  • LangChain: A framework of integrations, tools, and agent loops for composing LLM applications, with prompts maintained by the developer.
  • Hyperparameter tuning: Searches over training settings against a validation metric, the closest analogy to how DSPy searches over prompts.
  • Compound AI systems: Applications built from several model calls, retrievers, and tools. DSPy was designed to optimize such pipelines end to end, tuning every step's prompt against the final metric.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Turn drone and aerial imagery into real-time insights for agriculture, aquaculture, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Apply computer vision in aerospace with Ultralytics YOLO for aircraft inspection, runway FOD detection, airport ground operations, and composite defect detection.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning