{"record":{"id":"43d52ab06ea46219","repo":"BoundaryML/baml","slug":"failed-to-parse-json","errorCode":null,"errorMessage":"Failed to parse JSON","messagePattern":"Failed to parse JSON","errorType":"error_code","errorClass":"JsonishError","httpStatus":null,"severity":"error","filePath":"baml_language/crates/bex_sap/src/jsonish/mod.rs","lineNumber":25,"sourceCode":"mod value;\n\npub use parser::{ParseOptions, parse};\npub use value::{CompletionState, Fixes, Value};\n\n/// Error type for jsonish parsing failures.\n#[derive(Debug, thiserror::Error)]\npub enum JsonishError {\n    #[error(\"Depth limit reached. Likely a circular reference.\")]\n    DepthLimitReached,\n    #[error(\"No JSON objects found\")]\n    NoJsonObjectsFound,\n    #[error(\"No markdown blocks found\")]\n    NoMarkdownBlocksFound,\n    #[error(\"Mismatched brackets\")]\n    MismatchedBrackets,\n    #[error(\"No collection to consume token: {0:?}\")]\n    NoCollectionForToken(char),\n    #[error(\"Failed to parse JSON\")]\n    ParseFailed,\n}\n","sourceCodeStart":7,"sourceCodeEnd":28,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bex_sap/src/jsonish/mod.rs#L7-L28","documentation":"`JsonishError::ParseFailed` is the generic failure variant of the jsonish parser: after all extraction and fixing passes, the candidate text still failed to parse as JSON. It is the catch-all when none of the more specific variants (depth, brackets, missing blocks) apply.","triggerScenarios":"Calling `bex_sap::jsonish::parse` on text whose best JSON candidate is syntactically invalid — wrong token order, invalid literals (e.g. `NaN` where a number is expected in strict mode), unterminated strings.","commonSituations":"Model emits JSON-like but invalid text (single quotes, trailing commas, unquoted keys); output corrupted by encoding issues; overly lenient prompt letting the model freestyle the format.","solutions":["Log the exact candidate text and validate it with a standard JSON parser to see the precise syntax error.","Tighten the prompt: \"emit only valid RFC 8259 JSON\"; request json mode from the provider if available.","Run a JSON-repair/normalization pass (fix quotes, trailing commas) before parsing.","Retry the generation, possibly with a lower temperature."],"exampleFix":"// before\nlet v = jsonish::parse(&raw, &opts)?;\n// after\nlet repaired = repair_json(&raw); // fix quotes/trailing commas\nlet v = jsonish::parse(&repaired, &opts).map_err(|e| MyErr::BadModelJson(e))?;","handlingStrategy":"try-catch","validationCode":"serde_json::from_str::<serde_json::Value>(candidate)\n    .err()\n    .map(|e| eprintln!(\"candidate JSON invalid: {e}\")); // diagnose before jsonish parse","typeGuard":null,"tryCatchPattern":"match jsonish::parse(&raw, &opts) {\n    Ok(v) => Ok(v),\n    Err(JsonishError::ParseFailed) => { let r = repair_json(&raw); jsonish::parse(&r, &opts) }\n    Err(e) => Err(e.into()),\n}","preventionTips":["Use provider JSON mode / strict output schemas.","Run a JSON-repair pass on model output before parsing.","Lower temperature for structured-output tasks and log raw output on failure."],"tags":["parsing","json","llm-output"],"backgroundTag":"json-parse-error","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}