TypeError
dspy.exceptions.TypeError
Stack trace
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 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
InputField declared as string but OutputField expects an integer type
Change the InputField type declaration to int to match the OutputField type exactly.
OutputField type changed in the pipeline but InputField type was not updated accordingly
Update the InputField type to reflect the new OutputField type to maintain consistency.
Using incompatible custom types or classes for InputField and OutputField without proper serialization
Ensure both InputField and OutputField use compatible primitive types or implement serialization methods to align types.
Code: broken vs fixed
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 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) 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.