Problem
The CLI provider uses as type assertions with manual field validation when parsing JSON output:
const obj = parsed as {
text?: unknown;
output_messages?: unknown;
token_usage?: unknown;
cost_usd?: unknown;
duration_ms?: unknown;
};
const costUsd = typeof obj.cost_usd === 'number' && obj.cost_usd >= 0 ? obj.cost_usd : undefined;
This pattern:
- Bypasses TypeScript type safety with
as assertions
- Is verbose and repetitive
- Can easily miss validation for a field
- Inconsistent with rest of codebase using Zod
Proposed Solution
Replace with Zod schemas:
const cliOutputSchema = z.object({
text: z.string().optional(),
output_messages: z.array(outputMessageSchema).optional(),
token_usage: tokenUsageSchema.optional(),
cost_usd: z.number().nonnegative().optional(),
duration_ms: z.number().nonnegative().optional(),
}).passthrough();
const result = cliOutputSchema.safeParse(parsed);
if (result.success) {
// result.data is fully typed
}
Files to Update
packages/core/src/evaluation/providers/cli.ts
parseSingleOutput() (~line 329)
parseJsonlBatchOutput() (~line 526)
Benefits
- Single source of truth for schema + types
- Better error messages on parse failure
- Reusable schemas (tokenUsage, outputMessages)
- No
as assertions
- Consistent with project conventions (Zod in tech stack)
Labels
refactor, good-first-issue
Problem
The CLI provider uses
astype assertions with manual field validation when parsing JSON output:This pattern:
asassertionsProposed Solution
Replace with Zod schemas:
Files to Update
packages/core/src/evaluation/providers/cli.tsparseSingleOutput()(~line 329)parseJsonlBatchOutput()(~line 526)Benefits
asassertionsLabels
refactor, good-first-issue