TypeError
builtins.TypeError
Stack trace
Traceback (most recent call last):
File "pipeline.py", line 45, in run_step
processed = next_step(output)
File "pipeline.py", line 30, in next_step
result = step_function(data)
TypeError: expected dict, got str Why it happens
AI pipelines often chain multiple processing steps with strict input/output type expectations. When a step returns output in a different type or format than the next step expects (e.g., string instead of dict), Python raises a TypeError. This usually happens due to missing serialization, incorrect parsing, or inconsistent schema enforcement between steps.
Detection
Add type assertions or validation checks after each pipeline step to verify output types before passing to the next step, logging mismatches for early detection.
Causes & fixes
A pipeline step returns a raw string instead of a parsed dictionary expected by the next step
Parse the string output into a dictionary (e.g., using json.loads) before passing it to the next step
Mismatch between the declared output type in the pipeline and the actual returned data type
Update the pipeline step's output type annotation and ensure the returned data matches this type exactly
Missing or incorrect deserialization of AI model output before further processing
Add explicit deserialization logic immediately after receiving AI output to convert it into the expected Python data structure
Code: broken vs fixed
def step1():
return '{"key": "value"}' # returns JSON string, not dict
def step2(data: dict):
print(data['key'])
output = step1()
step2(output) # TypeError: expected dict, got str import os
import json
def step1():
return '{"key": "value"}' # returns JSON string
def step2(data: dict):
print(data['key'])
output = step1()
output_dict = json.loads(output) # fix: parse string to dict
step2(output_dict) # works correctly
# API keys not needed here but os.environ usage shown for consistency
print("Pipeline step output type fixed and processed successfully.") Workaround
Wrap the pipeline step call in try/except TypeError, then attempt to parse or convert the output to the expected type before retrying the next step.
Prevention
Design pipeline steps with strict input/output type contracts and validate outputs immediately after each step, using schema validation libraries or type hints enforced at runtime.