Skip to content

Commit 97cd41d

Browse files
author
Krish Patel
committed
fix: wrap gate/calibrate/transform in try/except — zero raw tracebacks
Stress test with corrupted/edge-case inputs found 8 raw tracebacks: - gate: NaN/Inf vectors, empty vectors, wrong dims, single vector, bad .npy - calibrate: NaN vectors, single pair (K=1), wrong dims - transform: dimension mismatch All now show clean, actionable error messages. No raw tracebacks. Also added early validation in gate for empty vectors and dim mismatch before the evaluation runs.
1 parent 4ca3187 commit 97cd41d

2 files changed

Lines changed: 116 additions & 15 deletions

File tree

‎isotrieve-python/src/isotrieve/cli.py‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,25 @@ def calibrate_cmd(
202202
raise typer.Exit(2)
203203

204204
mapping = RidgeMapping(alpha="auto", seed=seed)
205-
mapping.fit(X, Y)
205+
try:
206+
mapping.fit(X, Y)
207+
except ValueError as exc:
208+
msg = str(exc)
209+
if "NaN" in msg or "Inf" in msg:
210+
console.print(
211+
f"[red]Calibration vectors contain NaN or Inf values.[/red]\n"
212+
f" Check your source/target vector files for corrupt data."
213+
)
214+
elif "Need at least 2" in msg:
215+
console.print(f"[red]{msg}[/red]")
216+
elif "zero samples" in msg.lower():
217+
console.print(f"[red]{msg}[/red]")
218+
else:
219+
console.print(f"[red]Calibration failed: {exc}[/red]")
220+
raise typer.Exit(1) from exc
221+
except Exception as exc:
222+
console.print(f"[red]Calibration failed: {exc}[/red]")
223+
raise typer.Exit(1) from exc
206224

207225
# Fit score recalibrator from holdout calibration data
208226
from isotrieve.mapping.base import l2_normalize
@@ -328,7 +346,19 @@ def transform_cmd(
328346
def gen():
329347
for batch in src.iter_vectors(batch_size=batch_size):
330348
vecs = np.stack([r.vector for r in batch], axis=0)
331-
mapped = mapping.transform(vecs)
349+
try:
350+
mapped = mapping.transform(vecs)
351+
except ValueError as exc:
352+
msg = str(exc)
353+
if "Dimension" in msg or "dim" in msg.lower():
354+
console.print(f"[red]Transform failed: {msg}[/red]")
355+
elif "NaN" in msg or "Inf" in msg:
356+
console.print(
357+
f"[red]Stored vectors contain NaN or Inf values.[/red]"
358+
)
359+
else:
360+
console.print(f"[red]Transform failed: {exc}[/red]")
361+
raise typer.Exit(1) from exc
332362
yield [
333363
VectorRecord(
334364
id=batch[i].id,
@@ -339,7 +369,13 @@ def gen():
339369
for i in range(len(batch))
340370
]
341371

342-
n = dst.write_vectors(gen(), batch_size=batch_size)
372+
try:
373+
n = dst.write_vectors(gen(), batch_size=batch_size)
374+
except SystemExit:
375+
raise
376+
except Exception as exc:
377+
console.print(f"[red]Write failed: {exc}[/red]")
378+
raise typer.Exit(1) from exc
343379
if as_json:
344380
_print_json({"written": n, "target_dir": str(target_dir)})
345381
return

‎isotrieve-python/src/isotrieve/cli_gate.py‎

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,22 @@ def gate_cmd(
7878
f"[red]Target vectors not found: {target_vectors}[/red]"
7979
)
8080
raise typer.Exit(1)
81-
X_sample = np.load(source_vectors)
82-
Y_sample = np.load(target_vectors)
81+
try:
82+
X_sample = np.load(source_vectors)
83+
except Exception as exc:
84+
console.print(
85+
f"[red]Failed to load source vectors: {exc}[/red]\n"
86+
f" Ensure {source_vectors} is a valid .npy file."
87+
)
88+
raise typer.Exit(1) from exc
89+
try:
90+
Y_sample = np.load(target_vectors)
91+
except Exception as exc:
92+
console.print(
93+
f"[red]Failed to load target vectors: {exc}[/red]\n"
94+
f" Ensure {target_vectors} is a valid .npy file."
95+
)
96+
raise typer.Exit(1) from exc
8397
elif queries is not None and corpus is not None:
8498
if not queries.exists():
8599
console.print(f"[red]Queries file not found: {queries}[/red]")
@@ -89,27 +103,78 @@ def gate_cmd(
89103
raise typer.Exit(1)
90104
# Queries-only mode: use query embeddings as source,
91105
# corpus embeddings as target
92-
X_sample = np.load(queries)
93-
Y_sample = np.load(corpus)
106+
try:
107+
X_sample = np.load(queries)
108+
except Exception as exc:
109+
console.print(
110+
f"[red]Failed to load queries file: {exc}[/red]\n"
111+
f" Ensure {queries} is a valid .npy file."
112+
)
113+
raise typer.Exit(1) from exc
114+
try:
115+
Y_sample = np.load(corpus)
116+
except Exception as exc:
117+
console.print(
118+
f"[red]Failed to load corpus file: {exc}[/red]\n"
119+
f" Ensure {corpus} is a valid .npy file."
120+
)
121+
raise typer.Exit(1) from exc
94122
else:
95123
console.print(
96124
"[red]Provide --source-vectors/--target-vectors OR "
97125
"--queries/--corpus[/red]"
98126
)
99127
raise typer.Exit(2)
100128

129+
# Validate vector dimensions and emptiness
130+
if len(X_sample) == 0 or len(Y_sample) == 0:
131+
console.print(
132+
"[red]Vector file is empty — need at least one vector.[/red]"
133+
)
134+
raise typer.Exit(1)
135+
if X_sample.shape[1] != mapping.d_src:
136+
console.print(
137+
f"[red]Source vector dim mismatch: expected {mapping.d_src} "
138+
f"(source model dim), got {X_sample.shape[1]}.[/red]\n"
139+
f" Vectors must be from the source embedding model."
140+
)
141+
raise typer.Exit(1)
142+
101143
# Run gate
102144
gate = QualityGate()
103-
report = gate.evaluate(mapping, X_sample, Y_sample)
145+
try:
146+
report = gate.evaluate(mapping, X_sample, Y_sample)
147+
except ValueError as exc:
148+
msg = str(exc)
149+
if "NaN" in msg or "Inf" in msg:
150+
console.print(
151+
f"[red]Vectors contain NaN or Inf values.[/red]\n"
152+
f" Check your source/target vector files for corrupt data."
153+
)
154+
elif "Dimension" in msg or "dim" in msg.lower():
155+
console.print(f"[red]{msg}[/red]")
156+
else:
157+
console.print(f"[red]Gate evaluation failed: {exc}[/red]")
158+
raise typer.Exit(1) from exc
159+
except Exception as exc:
160+
console.print(f"[red]Gate evaluation failed: {exc}[/red]")
161+
raise typer.Exit(1) from exc
104162

105163
# Bootstrap confidence intervals on retention metrics
106-
ci = _bootstrap_retention_ci(
107-
mapping,
108-
X_sample,
109-
Y_sample,
110-
n_resamples=bootstrap_resamples,
111-
seed=seed,
112-
)
164+
try:
165+
ci = _bootstrap_retention_ci(
166+
mapping,
167+
X_sample,
168+
Y_sample,
169+
n_resamples=bootstrap_resamples,
170+
seed=seed,
171+
)
172+
except Exception as exc:
173+
console.print(
174+
f"[yellow]Warning: bootstrap CI failed ({exc}). "
175+
f"Showing point estimates only.[/yellow]"
176+
)
177+
ci = {}
113178

114179
# Format output
115180
if output_format == "json":

0 commit comments

Comments
 (0)