liveBook · Manning

Instruction

As explained in Building LLM Applications with DSPy

Text within a prompt that directs a language model’s behavior or specifies how it should perform a task.

An instruction is prompt text that tells a language model what task to perform or how to perform it. For an intent classifier, “Classify the message into one of the possible labels” states the task; adding what each label means can clarify distinctions that label names alone cannot capture. section 5.1 section 6.1.2

In DSPy, a signature supplies a program’s starting instructions. An unoptimized class-based signature uses its docstring; without one, DSPy falls back to a description of the input and output fields. Instructions appear in the System message after the field descriptions and before demonstrations and the current task. You can inspect them with `print(intent_classifier.signature.instructions)`. section 6.1.2

There are two practical ways to improve instructions:

  • Write a useful signature docstring rather than relying on field names alone. It provides a stronger starting point even if an optimizer later rewrites it. section 6.1.2
  • Evaluate optimized alternatives. DSPy treats instruction optimization and demonstration selection as distinct processes; MIPROv2 can generate candidate instructions and combine them with demonstrations. section 5.1 section 7.6.2

Clarifying intent labels

“Flight” can be defined as queries about available flights and their details, while “Booking” covers requests to book or modify a reservation. This adds a classification rule where short label names leave room for ambiguity. section 5.1

Listing 7.11 Custom module to add a closing section
from dspy.primitives.module import Module
from dspy.signatures.signature import Signature, ensure_signature
from typing import Any

class Closer_Example(Module):
    def __init__(self, signature: str, **config: dict[str, Any]):
        super().__init__()
        signature = ensure_signature(signature)
        signature.instructions += " Answer very carefully."
        self.predict = dspy.Predict(signature, **config)  #1

    def forward(self, **kwargs):
        return self.predict(**kwargs)

    async def aforward(self, **kwargs):
        return await self.predict.acall(**kwargs)

prog = Closer_Example("question -> answer")
prog(question="What is the capital of Spain?")
print(lm.inspect_history(n=1))

The Python custom module appends “Answer very carefully.” to `signature.instructions` before creating a `dspy.Predict` instance. This is a direct way to add a closing direction, though an optimizer may later rewrite it. section 7.4.1

How Building LLM Applications with DSPy covers it

How deep it goes
Explains placement, defaults, inspection, manual modification, and optimization, without descending into language-model internals.
Examples
Uses flight-intent label definitions, instruction inspection, and a Python module that appends a closing direction.
What it assumes
Uses Python and DSPy signatures while explicitly showing the default instruction produced when no docstring is supplied.
What you can do after
Operate/configure it by writing and inspecting signature instructions, or optimize it alongside demonstrations with MIPROv2.
How it connects
Connects instructions to DSPy signatures, docstrings, demonstrations, custom modules, and prompt optimization.

Also explained in

Compare how 5 books explain instruction →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building LLM Applications with DSPy. The section links point to the parts of the book it draws on.