{"record":{"id":"fca56c0ab7f65151","repo":"BoundaryML/baml","slug":"depth-limit-reached-likely-a-circular-reference","errorCode":null,"errorMessage":"Depth limit reached. Likely a circular reference.","messagePattern":"Depth limit reached\\. Likely a circular reference\\.","errorType":"error_code","errorClass":"JsonishError","httpStatus":null,"severity":"error","filePath":"baml_language/crates/bex_sap/src/jsonish/mod.rs","lineNumber":15,"sourceCode":"//! This module implements parsing JSON-like data into a structured representation.\n//!\n//! 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":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bex_sap/src/jsonish/mod.rs#L1-L28","documentation":"`JsonishError::DepthLimitReached` is raised by the jsonish parser when parsing recurses past its configured depth limit. The parser imposes a maximum nesting depth to protect against runaway recursion, and exceeding it almost always indicates a circular or pathologically nested structure in the LLM output being parsed.","triggerScenarios":"Calling `bex_sap::jsonish::parse` (with ParseOptions) on model output containing extremely deeply nested JSON, or a structure whose recursive descent exceeds the parser's depth limit.","commonSituations":"LLM emits deeply nested or self-referential-looking JSON; a prompt-injected payload designed to blow the parser's stack; default depth limits too low for legitimately nested data.","solutions":["Inspect the raw text being parsed and fix or sanitize the deeply nested/circular structure before parsing.","Raise the parser depth limit via `ParseOptions` if the input legitimately requires deeper nesting.","Pre-truncate or flatten overly nested input before handing it to the parser.","Treat this as untrusted-input signal: validate LLM output structure before jsonish parsing."],"exampleFix":"// before\nlet v = jsonish::parse(raw, &ParseOptions::default())?;\n// after\nlet opts = ParseOptions { max_depth: 256, ..ParseOptions::default() };\nlet v = jsonish::parse(sanitize_deep_nesting(raw), &opts)?;","handlingStrategy":"validation","validationCode":"fn nesting_depth(s: &str) -> usize {\n    let (mut d, mut max) = (0i32, 0usize);\n    for c in s.chars() { match c { '{' | '[' => { d += 1; max = max.max(d as usize); } '}' | ']' => d -= 1, _ => {} } }\n    max\n}\n// reject before parsing: nesting_depth(raw) > max_depth","typeGuard":null,"tryCatchPattern":"match jsonish::parse(&raw, &opts) {\n    Ok(v) => handle(v),\n    Err(JsonishError::DepthLimitReached) => log::warn!(\"input too deeply nested; sanitizing\"),\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Bound nesting depth of expected model output in prompts.","Sanitize or flatten deeply nested inputs before parsing.","Set an explicit, adequate depth limit in ParseOptions."],"tags":["parsing","json","recursion"],"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"}