High severity intermediate · Fix: 2-5 min

TypeError

dspy.exceptions.TypeError

What this error means
DSPy raises a TypeError when the declared InputField type does not match the OutputField type expected by the pipeline.

Stack trace

traceback
Traceback (most recent call last):
  File "app.py", line 42, in <module>
    pipeline.run(input_data)
  File "dspy/pipeline.py", line 88, in run
    raise TypeError(f"InputField and OutputField type mismatch: {input_type} != {output_type}")
dspy.exceptions.TypeError: InputField and OutputField type mismatch: str != int
QUICK FIX
Align the InputField type declaration exactly with the OutputField type to resolve the mismatch immediately.

Why it happens

DSPy enforces strict type consistency between InputField and OutputField definitions to ensure data integrity in pipelines. This error occurs when the type declared for an InputField does not match the type expected by the corresponding OutputField, causing a runtime conflict.

Detection

Validate field types during pipeline construction or before execution by asserting InputField and OutputField types match exactly to catch mismatches early.

Causes & fixes

1

InputField declared as string but OutputField expects an integer type

✓ Fix

Change the InputField type declaration to int to match the OutputField type exactly.

2

OutputField type changed in the pipeline but InputField type was not updated accordingly

✓ Fix

Update the InputField type to reflect the new OutputField type to maintain consistency.

3

Using incompatible custom types or classes for InputField and OutputField without proper serialization

✓ Fix

Ensure both InputField and OutputField use compatible primitive types or implement serialization methods to align types.

Code: broken vs fixed

Broken - triggers the error
python
from dspy import Pipeline, InputField, OutputField

class MyPipeline(Pipeline):
    input_field = InputField(str)  # InputField type is str
    output_field = OutputField(int)  # OutputField type is int

pipeline = MyPipeline()
pipeline.run('123')  # This line triggers the TypeError due to type mismatch
Fixed - works correctly
python
import os
from dspy import Pipeline, InputField, OutputField

class MyPipeline(Pipeline):
    input_field = InputField(int)  # Fixed: InputField type changed to int to match OutputField
    output_field = OutputField(int)

pipeline = MyPipeline()
result = pipeline.run(123)  # Now runs without error
print('Pipeline output:', result)
Changed InputField type from str to int to match OutputField type, resolving the type mismatch error.

Workaround

Catch the TypeError during pipeline execution, convert input data to the expected OutputField type manually before passing it to the pipeline.

Prevention

Define and enforce consistent types for InputField and OutputField at design time, and add type validation checks in pipeline constructors to prevent mismatches.

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.