In a custom DSPy module, `forward()` defines what happens when the module runs. Calling the module instance invokes `forward()` behind the scenes. Define it alongside `__init__()` in a `dspy.Module` subclass; use `__init__()` to create submodules and `forward()` to execute them and return a `Prediction`. section 7.2 section 2.6
Listing 7.2 A minimal custom module
class SimpleModule(dspy.Module):
def __init__(self): #1
super().__init__()
self.query = dspy.Predict("input -> output") #2
def forward(self, input): #3
return self.query(input=input)
prog = SimpleModule() #4
prog(input="What is the capital of France?")
The minimal module stores a `Predict` submodule, then passes its `forward()` input to that submodule. The caller uses `prog(...)`, not `prog.forward(...)`. section 7.2 section 2.6
For a workflow with several steps, `forward()` can:
- Accept parameters covering the inputs needed by its submodules. section 7.2
- Prepare inputs, call submodules or tools, and pass earlier results into later calls. section 7.2
- Process the final result and return a `Prediction`. section 7.2
Keep normal execution at the module-call level: as of DSPy 3.0, explicitly calling `forward()` produces a warning. `aforward()` is the optional asynchronous counterpart for work that can run in parallel. section 2.6 section 7.2
