zed-industries/zed · error

Failed to parse example file: {} {error}

Error message

Failed to parse example file: {}
{error}

What it means

For .json inputs the loader does serde_json::from_str::<Example> on the whole file and panics with the serde error when it does not match the Example schema — missing required fields, wrong field types, or trailing content after the object. The message names the file and includes the underlying parse error.

Source

Thrown at crates/edit_prediction_cli/src/example.rs:243

            buffer
        } else {
            std::fs::read_to_string(path)
                .unwrap_or_else(|_| panic!("Failed to read path: {path:?}"))
        };
        let filename = path.file_stem().unwrap().to_string_lossy().to_string();
        let ext = if !is_stdin {
            path.extension()
                .map(|ext| ext.to_string_lossy().to_string())
                .unwrap_or_else(|| panic!("{} should have an extension", path.display()))
        } else {
            "jsonl".to_string()
        };

        match ext.as_ref() {
            "json" => {
                let mut example =
                    serde_json::from_str::<Example>(&content).unwrap_or_else(|error| {
                        panic!("Failed to parse example file: {}\n{error}", path.display())
                    });
                if example.spec.name.is_empty() {
                    example.spec.name = filename;
                }
                examples.push(example);
            }
            "jsonl" => examples.extend(
                content
                    .lines()
                    .enumerate()
                    .map(|(line_ix, line)| {
                        let mut example =
                            serde_json::from_str::<Example>(line).unwrap_or_else(|error| {
                                panic!(
                                    "Failed to parse example on {}:{}\n{error}",
                                    path.display(),
                                    line_ix + 1
                                )

View on GitHub (pinned to f4178619ac)

Solutions

  1. Read the {error} in the panic — serde names the exact missing field or type mismatch
  2. Rename genuinely line-delimited files to .jsonl so each line parses as one Example
  3. Validate the file against the Example struct with a small serde program before batching
  4. Regenerate the example from a known-good one

Example fix

# before — bad.json
{"inputs": "fn main() {}"}   // panic: Failed to parse example file: bad.json (missing field `spec`)

# after — bad.json
{"spec": {"name": "rename", "language": "rust"}, "inputs": "fn main() {}"}
Defensive patterns

Strategy: validation

Validate before calling

use std::fs;

fn validate_example_file(path: &std::path::Path) -> Result<(), String> {
    let content = fs::read_to_string(path).map_err(|e| e.to_string())?;
    serde_json::from_str::<Example>(&content).map(|_| ()).map_err(|e| e.to_string())
}

Prevention

When it happens

Trigger: A JSON object missing required spec fields; wrong field types (string where a number is expected); two concatenated objects in one file; a line-delimited file misnamed .json instead of .jsonl.

Common situations: Hand-edited examples dropping a key; schema drift after a CLI version bump; exports that are actually JSONL with the wrong extension.

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


AI-assisted analysis of zed-industries/zed@f4178619ac (2026-08-20). Data as JSON: /api/errors/f190cac164f060ee. Report an issue: GitHub.