{"record":{"id":"7255b34d6d795b27","repo":"BoundaryML/baml","slug":"mismatched-brackets","errorCode":null,"errorMessage":"Mismatched brackets","messagePattern":"Mismatched brackets","errorType":"error_code","errorClass":"JsonishError","httpStatus":null,"severity":"error","filePath":"baml_language/crates/bex_sap/src/jsonish/mod.rs","lineNumber":21,"sourceCode":"//! The main entry point is the [`parse`] function, which takes a string and returns a [`Value`].\n//! This is basically the jsonish equivalent of [`serde_json::from_str`] and [`serde_json::Value`].\n\nmod parser;\nmod 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":3,"sourceCodeEnd":28,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bex_sap/src/jsonish/mod.rs#L3-L28","documentation":"`JsonishError::MismatchedBrackets` indicates the parser encountered unbalanced or incorrectly paired delimiters (`{}`, `[]`) while extracting JSON from the input. It signals malformed JSON structure that the lenient jsonish extractor could not repair.","triggerScenarios":"Calling `bex_sap::jsonish::parse` on model output where braces/brackets are unbalanced — e.g. truncated JSON, prose interleaved with JSON fragments, or the model writing `[1, 2` without a closing bracket.","commonSituations":"Token-limit truncation cutting off the closing delimiters; the model mixing commentary into the JSON; prompt asking for multiple JSON snippets that get merged incorrectly.","solutions":["Check for truncation (finish_reason / max tokens) and raise limits or shorten the requested output.","Inspect the raw text around the failure and repair unbalanced delimiters before parsing.","Ask the model to re-emit the JSON completely with no commentary.","Use a JSON-repair pass or a stricter prompt (\"output must be one complete JSON value\")."],"exampleFix":"// before\nlet v = jsonish::parse(truncated_output, &opts)?;\n// after\nlet complete = if !is_balanced(&truncated_output) { request_completion(&truncated_output)? } else { truncated_output };\nlet v = jsonish::parse(&complete, &opts)?;","handlingStrategy":"validation","validationCode":"fn is_balanced(s: &str) -> bool {\n    let mut stack = Vec::new();\n    for c in s.chars() {\n        match c { '{' | '[' => stack.push(c), '}' => if stack.pop() != Some('{') { return false }, ']' => if stack.pop() != Some('[') { return false }, _ => {} }\n    }\n    stack.is_empty()\n}\n// reject or repair before parsing if !is_balanced(raw)","typeGuard":null,"tryCatchPattern":"match jsonish::parse(&raw, &opts) {\n    Ok(v) => Ok(v),\n    Err(JsonishError::MismatchedBrackets) => jsonish::parse(&repair_brackets(&raw), &opts),\n    Err(e) => Err(e.into()),\n}","preventionTips":["Check finish_reason for truncation before parsing.","Ask for one complete JSON value with no commentary.","Keep a bracket-balance sanity check in your ingestion pipeline."],"tags":["parsing","json","syntax"],"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"}