JuliusBrussee/caveman · error

usage export row

Error message

usage export row: %w

What it means

While iterating data rows, csv.Reader.Read() returned a non-EOF error; parseUsageExport wraps it as "usage export row". Reader.FieldsPerRecord is set to -1 so ragged rows are allowed; this error covers malformed CSV structure (unbalanced quotes, bare quotes) and mid-file I/O errors.

Solutions

  1. Open the file, locate the malformed row at the error's line/column, and fix quoting (double embedded quotes)
  2. Re-export the CSV from the provider instead of hand-editing
  3. Validate the CSV with a parser/linter (e.g. python -m csv) before running reconcile

Example fix

// before
model,input_tokens
"gpt-4o,1000   // unclosed quote
// after
model,input_tokens
"gpt-4o",1000
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-validate with a strict CSV reader
r := csv.NewReader(f); r.FieldsPerRecord = -1; if _, err := r.ReadAll(); err != nil { return fmt.Errorf("malformed usage CSV: %w", err) }

Try / catch

if _, err := store.BuildLearnReconcile(ctx, opts); err != nil { if strings.Contains(err.Error(), "usage export row") { log.Error("malformed CSV row; fix quoting or re-export: ", err) } }

Prevention

When it happens

Trigger: A data row in the usage CSV has malformed quoting (e.g. an unclosed ", a quote not doubled inside a quoted field), or the underlying file read fails partway through.

Common situations: Hand-edited CSV rows; exports containing quoted model names that were mangled by another tool; copy-pasting rows into the CSV with broken quoting.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/2f5def527a6d676a. Report an issue: GitHub.

Appendix: source

Thrown at proxy/internal/store/learn_reconcile.go:157

	defer file.Close()
	reader := csv.NewReader(file)
	reader.FieldsPerRecord = -1
	header, err := reader.Read()
	if err != nil {
		return nil, fmt.Errorf("usage export has no header row: %w", err)
	}
	columns, err := mapReconcileHeader(header)
	if err != nil {
		return nil, err
	}
	billed := map[string]int64{}
	for {
		record, err := reader.Read()
		if err == io.EOF {
			break
		}
		if err != nil {
			return nil, fmt.Errorf("usage export row: %w", err)
		}
		modelIndex := columns["model"]
		if modelIndex >= len(record) {
			continue
		}
		model := strings.TrimSpace(record[modelIndex])
		if model == "" {
			continue
		}
		total := reconcileCell(record, columns, "input") +
			reconcileCell(record, columns, "output") +
			reconcileCell(record, columns, "cache_read") +
			reconcileCell(record, columns, "cache_write")
		if total <= 0 {
			continue
		}
		billed[model] += total
	}

View on GitHub (pinned to 3ee70a1026)