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
- 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.
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
- 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.
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.
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- No JSON objects found
- Failed to parse JSON
- Depth limit reached. Likely a circular reference.
- No markdown blocks found
- Mismatched brackets
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/43d52ab06ea46219.
Report an issue: GitHub.