rohitg00/ai-engineering-from-scratch · error · TypeError
Unsupported type
Error message
Unsupported type {type(other)} What it means
Generic type guard in Tensor._elementwise_op: the operators dispatched from __add__/__sub__/__mul__ accept only scalars (int/float) or another Tensor; any other operand type (string, list, None, array) falls through to this raise.
Solutions
- Convert the operand to a Tensor before combining: t + Tensor(other_data)
- Operate with scalars or extract Python numbers with float(x)
- Add an isinstance check upstream and fail early with a clearer error
Defensive patterns
Strategy: type-guard
When it happens
Trigger: Thrown at phases/01-math-foundations/12-tensor-operations/code/tensors.py:185 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26).
Data as JSON: /api/errors/d98fffc895491c62.
Report an issue: GitHub.
Appendix: source
Thrown at phases/01-math-foundations/12-tensor-operations/code/tensors.py:185
return result
def flatten(self, start_dim=0, end_dim=-1):
if end_dim < 0:
end_dim = self.rank + end_dim
new_shape = (
list(self._shape[:start_dim])
+ [reduce(lambda a, b: a * b, self._shape[start_dim:end_dim + 1], 1)]
+ list(self._shape[end_dim + 1:])
)
return self.reshape(tuple(new_shape))
def _elementwise_op(self, other, op):
if isinstance(other, (int, float)):
result_data = [op(x, other) for x in self._data]
return Tensor(result_data, shape=self._shape)
if not isinstance(other, Tensor):
raise TypeError(f"Unsupported type {type(other)}")
if self._shape != other._shape:
raise ValueError(
f"Shape mismatch: {self._shape} vs {other._shape}. "
"Use broadcast() first."
)
result_data = [op(a, b) for a, b in zip(self._data, other._data)]
return Tensor(result_data, shape=self._shape)
def __add__(self, other):
return self._elementwise_op(other, lambda a, b: a + b)
def __mul__(self, other):
return self._elementwise_op(other, lambda a, b: a * b)
def __sub__(self, other):
return self._elementwise_op(other, lambda a, b: a - b)
def sum(self, axis=None):View on GitHub (pinned to 39ea8a1c6d)