Skip to content

refactor: use Zod schemas for CLI provider JSON parsing #96

@christso

Description

@christso

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions