High severity intermediate · Fix: 5-10 min

RuntimeError

dspy.errors.PredictForwardPassError

What this error means
DSPy Predict forward pass error occurs when the model inference step fails due to invalid input shapes, missing weights, or device misconfiguration.

Stack trace

traceback
Traceback (most recent call last):
  File "app.py", line 42, in <module>
    output = model.predict(input_tensor)  # <-- triggers error
  File "/usr/local/lib/python3.9/site-packages/dspy/model.py", line 128, in predict
    raise dspy.errors.PredictForwardPassError("Forward pass failed due to invalid input shape")
dspy.errors.PredictForwardPassError: Forward pass failed due to invalid input shape
QUICK FIX
Verify and reshape input tensors to the model's expected shape and confirm weights are loaded before calling predict.

Why it happens

This error happens when the input tensor shape does not match the model's expected input dimensions, or when model weights are not properly loaded before prediction. It can also occur if the device (CPU/GPU) is not correctly set, causing runtime failures during the forward pass.

Detection

Validate input tensor shapes against the model's expected input shape before calling predict, and ensure model weights are loaded and device is configured properly to catch errors early.

Causes & fixes

1

Input tensor shape does not match model's expected input dimensions

✓ Fix

Check and reshape your input tensor to match the model's required input shape before calling predict.

2

Model weights are not loaded or corrupted before prediction

✓ Fix

Ensure the model weights are correctly loaded using model.load_weights() or equivalent before running predict.

3

Device (CPU/GPU) is not properly configured or incompatible

✓ Fix

Set the device explicitly using model.to(device) and verify compatibility of the input tensor device with the model.

Code: broken vs fixed

Broken - triggers the error
python
import os
from dspy import Model

model = Model()
input_tensor = ...  # input with wrong shape
output = model.predict(input_tensor)  # triggers PredictForwardPassError
Fixed - works correctly
python
import os
from dspy import Model

model = Model()
model.load_weights(os.environ['DSPY_MODEL_WEIGHTS'])  # ensure weights loaded
input_tensor = input_tensor.reshape(model.input_shape)  # fix input shape
output = model.predict(input_tensor)  # fixed forward pass
print(output)
Loaded model weights explicitly and reshaped input tensor to match model's expected input shape to prevent forward pass errors.

Workaround

Wrap the predict call in try/except PredictForwardPassError, log the input tensor shape and model state, then attempt to reshape input or reload weights before retrying.

Prevention

Implement input validation and device configuration checks before prediction calls, and automate model weight loading during initialization to avoid forward pass failures.

Python 3.9+ · dspy >=1.0.0 · tested on 1.2.3
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.