Skip to content

Commit 4bda6bb

Browse files
committed
fix(router): raise ValueError for scalar embedding input instead of IndexError
Fixes #69: Router.predict() raised IndexError when given a scalar embedding input. _to_embedding() returned a 0-D numpy array with shape=(), and accessing shape[-1] crashed with IndexError. Changes: - Add ndim == 0 check in _to_embedding() - Raise clear ValueError with helpful message for scalar inputs - Suggests reshaping to (1, -1) or passing a list
1 parent 813821e commit 4bda6bb

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

‎src/tracer/runtime/router.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,13 @@ def _to_embedding(self, input) -> np.ndarray:
101101
" router = tracer.load_router('.tracer', embedder=embedder)"
102102
)
103103
return self.embedder.embed_one(input)
104-
return np.asarray(input, dtype=np.float32)
104+
arr = np.asarray(input, dtype=np.float32)
105+
if arr.ndim == 0:
106+
raise ValueError(
107+
"embedding must be a 1-D array, got a scalar. "
108+
"Reshape it to (1, -1) or pass a list."
109+
)
110+
return arr
105111

106112
def _to_embeddings(self, inputs) -> np.ndarray:
107113
"""Convert inputs (list of texts or 2D array) to (n, dim) float32."""

0 commit comments

Comments
 (0)