Chapter 9

API Reference

A quick lookup guide for DSPy's core classes, methods, and patterns.

Initialization

dspy.configure()

Configure the global settings for DSPy.

Python
dspy.configure(lm=lm, rm=rm)

Signatures

dspy.Signature

Define the input/output structure of a task.

Python
class QA(dspy.Signature):
    """Answer questions."""
    question = dspy.InputField()
    answer = dspy.OutputField()

Inline Signatures: "question -> answer"

Modules

dspy.Predict

Basic module for using a signature.

pred = dspy.Predict("input -> output")

dspy.ChainOfThought

Adds step-by-step reasoning.

cot = dspy.ChainOfThought("input -> output")

dspy.ReAct

Agent loop for reasoning and acting.

agent = dspy.ReAct(signature, tools=[...])

Optimizers

dspy.BootstrapFewShot

Optimizes by selecting effective few-shot examples.

opt = dspy.BootstrapFewShot(metric=metric_fn)

dspy.MIPRO

Optimizes instructions and examples.

opt = dspy.MIPRO(metric=metric_fn)

Evaluation

dspy.Evaluate

Run evaluation on a dataset.

Python
evaluator = dspy.Evaluate(devset=devset, metric=metric_fn)
score = evaluator(program)