BoundaryML/baml · error · JsonishError

Failed to parse JSON

Error message

Failed to parse JSON

What it means

`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.

Source

Thrown at baml_language/crates/bex_sap/src/jsonish/mod.rs:25

mod value;

pub use parser::{ParseOptions, parse};
pub use value::{CompletionState, Fixes, Value};

/// Error type for jsonish parsing failures.
#[derive(Debug, thiserror::Error)]
pub enum JsonishError {
    #[error("Depth limit reached. Likely a circular reference.")]
    DepthLimitReached,
    #[error("No JSON objects found")]
    NoJsonObjectsFound,
    #[error("No markdown blocks found")]
    NoMarkdownBlocksFound,
    #[error("Mismatched brackets")]
    MismatchedBrackets,
    #[error("No collection to consume token: {0:?}")]
    NoCollectionForToken(char),
    #[error("Failed to parse JSON")]
    ParseFailed,
}

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Log the exact candidate text and validate it with a standard JSON parser to see the precise syntax error.
  2. Tighten the prompt: "emit only valid RFC 8259 JSON"; request json mode from the provider if available.
  3. Run a JSON-repair/normalization pass (fix quotes, trailing commas) before parsing.
  4. Retry the generation, possibly with a lower temperature.

Example fix

// before
let v = jsonish::parse(&raw, &opts)?;
// after
let repaired = repair_json(&raw); // fix quotes/trailing commas
let v = jsonish::parse(&repaired, &opts).map_err(|e| MyErr::BadModelJson(e))?;
Defensive patterns

Strategy: try-catch

Validate before calling

serde_json::from_str::<serde_json::Value>(candidate)
    .err()
    .map(|e| eprintln!("candidate JSON invalid: {e}")); // diagnose before jsonish parse

Try / catch

match jsonish::parse(&raw, &opts) {
    Ok(v) => Ok(v),
    Err(JsonishError::ParseFailed) => { let r = repair_json(&raw); jsonish::parse(&r, &opts) }
    Err(e) => Err(e.into()),
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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 BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/43d52ab06ea46219. Report an issue: GitHub.