JuliusBrussee/caveman · error
not valid JSON
Error message
not valid JSON: %w
What it means
extractTools parses a tool catalog for Lint and SelectionProfile and requires the input to be valid JSON in one of the supported shapes (MCP {"tools":[...]}, OpenAI array, or {"functions":[...]}). If json.Unmarshal fails, the input is not JSON at all and the parse error is wrapped as 'not valid JSON'.
Solutions
- Validate the input with `jq . input.json` to see the exact syntax error and fix it
- Convert non-JSON formats first (yaml2json, toml2json) before passing to Lint/SelectionProfile
- Strip Markdown code fences or BOM from the input file
- Ensure the file contains a single JSON document, not JSONL or concatenated documents
Example fix
// before cat server.yaml | caveman-shrink lint - // after yq -o=json . server.yaml | caveman-shrink lint -
Defensive patterns
Strategy: validation
Validate before calling
let parsed: unknown
try { parsed = JSON.parse(input) } catch (e) { throw new Error(`input is not valid JSON: ${e}`) } Type guard
function isJSON(s: string): boolean { try { JSON.parse(s); return true } catch { return false } } Try / catch
tools, err := extractTools(input)
if err != nil {
var sintaxErr *json.SyntaxError
if errors.As(err, &sintaxErr) {
return fmt.Errorf("catalog JSON syntax error at offset %d", sintaxErr.Offset)
}
return err
} Prevention
- Validate input with jq/JSON.parse before passing it to Lint or SelectionProfile
- Convert YAML/TOML sources to JSON first
- Strip BOMs and Markdown code fences from copied content
When it happens
Trigger: Calling Lint or SelectionProfile with input that isn't JSON: YAML configs, TOML files, BOM-prefixed files, JSONL (multiple JSON documents), truncated output, or raw prose/log content piped in.
Common situations: Pasting a tool list from a Markdown doc including code fences; feeding a YAML MCP server config instead of JSON; shell heredoc with comments; double-encoded JSON (a JSON string containing JSON).
Understand the failure class
Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.
Related errors
- assembly slot content is not JSON-serializable
- cave_output_schema_mismatch
- colon
- comma
- device authorization failed
AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20).
Data as JSON: /api/errors/3e02fcbe1840439e.
Report an issue: GitHub.
Appendix: source
Thrown at shrink/shrink.go:243
out[e.name] = profileOf(e.schema)
}
return out, nil
}
// --- catalog parsing --------------------------------------------------------
type toolEntry struct {
name string
raw []byte // the tool object's JSON
schema map[string]any // its inputSchema/parameters, if any
}
// extractTools handles the MCP ({"tools":[…]}), OpenAI (array of {function:…} or
// flat) and {"functions":[…]} shapes.
func extractTools(input []byte) ([]toolEntry, error) {
var top any
if err := json.Unmarshal(input, &top); err != nil {
return nil, fmt.Errorf("not valid JSON: %w", err)
}
var rawList []any
switch t := top.(type) {
case map[string]any:
if v, ok := t["tools"].([]any); ok {
rawList = v
} else if v, ok := t["functions"].([]any); ok {
rawList = v
} else {
rawList = []any{t} // a single tool object
}
case []any:
rawList = t
default:
return nil, fmt.Errorf("unsupported catalog shape")
}
var entries []toolEntryView on GitHub (pinned to 3ee70a1026)