High severity beginner · Fix: 2-5 min

TypeError

builtins.TypeError

What this error means
An AI pipeline step returned output of a different type than the next step expects, causing a TypeError during processing.

Stack trace

traceback
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
QUICK FIX
Add explicit type conversion or validation after each pipeline step to ensure output matches the expected input type of the next step.

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

1

A pipeline step returns a raw string instead of a parsed dictionary expected by the next step

✓ Fix

Parse the string output into a dictionary (e.g., using json.loads) before passing it to the next step

2

Mismatch between the declared output type in the pipeline and the actual returned data type

✓ Fix

Update the pipeline step's output type annotation and ensure the returned data matches this type exactly

3

Missing or incorrect deserialization of AI model output before further processing

✓ Fix

Add explicit deserialization logic immediately after receiving AI output to convert it into the expected Python data structure

Code: broken vs fixed

Broken - triggers the error
python
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
Fixed - works correctly
python
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.")
Added json.loads to convert the string output from step1 into a dict before passing it to step2, matching the expected input type and preventing TypeError.

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.

Python 3.7+ · none (core Python) >=3.7 · tested on 3.9
Verified 2026-04
Verify ↗

Community Notes

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