Esta página se muestra en inglés. Una traducción al español está en curso.
PromptsMar 31, 2026·2 min de lectura

DSPy — Program LLMs Instead of Prompting

DSPy is a Python framework for programming language models instead of prompting them. 33.3K+ GitHub stars. Build modular AI systems — classifiers, RAG pipelines, agent loops — and let DSPy optimize pr

Listo para agents

Instalación lista para agent

Este activo puede instalarse después de elegir el runtime, revisar el plan y ejecutar el comando correspondiente.

Native · 96/100Política: permitir
Superficie agent
Cualquier agent MCP/CLI
Tipo
Prompt
Instalación
Single
Confianza
Confianza: Established
Entrada
DSPy — Program LLMs Instead of Prompting
Comando de instalación directa
npx -y tokrepo@latest install 88a2e60f-54d1-48cb-9173-23d56a4f3f20 --target codex

Ejecutar después de confirmar el plan con dry-run.

TL;DR
DSPy lets you write modular AI programs and auto-optimizes the prompts behind them.
§01

What it is

DSPy is a Python framework that replaces manual prompt engineering with programmatic modules. Instead of writing fragile prompt strings, you define typed signatures (input/output fields), compose modules into pipelines, and let DSPy optimize the underlying prompts or fine-tuning weights automatically against a metric you specify.

DSPy targets ML engineers and AI application developers building classifiers, RAG pipelines, agent loops, and multi-step reasoning systems. It decouples your application logic from the specific prompt text, making systems portable across models and more robust to model updates.

§02

Why it saves time or tokens

Manual prompt engineering is trial-and-error. DSPy's optimizers (formerly 'teleprompters') search over prompt variations, few-shot example selections, and even fine-tuning data to find the configuration that maximizes your metric. This automated search replaces hours of manual tweaking. Because the optimizer finds efficient prompts, the final system often uses fewer tokens per call than hand-written prompts that include unnecessary instructions.

§03

How to use

  1. Install DSPy: pip install dspy
  2. Define a signature: class QA(dspy.Signature): question = dspy.InputField(); answer = dspy.OutputField()
  3. Create a module, compile it with an optimizer, and run inference
§04

Example

import dspy

lm = dspy.LM('openai/gpt-4o-mini')
dspy.configure(lm=lm)

class Summarize(dspy.Signature):
    '''Summarize the document in one paragraph.'''
    document = dspy.InputField()
    summary = dspy.OutputField()

summarizer = dspy.ChainOfThought(Summarize)
result = summarizer(document='DSPy is a framework for...')
print(result.summary)

This defines a typed summarization module with chain-of-thought reasoning. DSPy generates the actual prompt from the signature and docstring.

ConceptPurpose
SignatureTyped input/output contract
ModuleComposable building block (ChainOfThought, ReAct)
OptimizerAuto-tunes prompts against a metric
MetricEvaluation function you define
§05

Related on TokRepo

§06

Common pitfalls

  • DSPy optimizers need labeled examples or a reliable metric function; without good evaluation data, optimization produces inconsistent results
  • The abstraction hides prompt text, which makes debugging harder; use dspy.inspect_history() to see what prompts DSPy actually sends
  • Compiling with a large optimizer on an expensive model can burn significant API credits; start with a small training set and a cheap model

Preguntas frecuentes

How does DSPy differ from LangChain?+

LangChain provides building blocks (chains, tools, retrievers) that you wire together with manually written prompts. DSPy replaces the manual prompts with typed signatures and auto-optimizes them. DSPy focuses on making prompts a compiled artifact rather than a hand-written string, while LangChain focuses on the orchestration and integration layer.

What optimizers does DSPy provide?+

DSPy includes BootstrapFewShot (selects few-shot examples), BootstrapFewShotWithRandomSearch (adds random search), MIPRO (multi-instruction proposal optimizer), and others. Each optimizer searches over different prompt configurations to maximize your specified metric. You can also write custom optimizers.

Can DSPy work with local models?+

Yes. DSPy supports any model accessible through a supported LM class, including local models via Ollama, vLLM, or Hugging Face Transformers. You configure the model endpoint and DSPy handles prompt formatting. Local models benefit from DSPy optimization just as cloud models do.

What is a DSPy signature?+

A signature is a typed contract defining the inputs and outputs of an AI module. It replaces the prompt template concept. You declare input fields and output fields as Python class attributes with optional descriptions. DSPy compiles these declarations into actual prompts during optimization.

Is DSPy production-ready?+

DSPy is used in production by multiple organizations for classification, RAG, and agent workflows. The compiled modules are deterministic given the same optimizer output. However, the API surface is still evolving, so pin your DSPy version and test thoroughly when upgrading.

Referencias (3)
  • DSPy GitHub— DSPy is a framework for programming language models
  • DSPy Docs— DSPy replaces prompt engineering with typed signatures and optimizers
  • Stanford NLP— DSPy originated from Stanford NLP research
🙏

Fuente y agradecimientos

Created by Stanford NLP. Licensed under MIT. stanfordnlp/dspy — 33,300+ GitHub stars

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados