{"record":{"id":"ba61e72a1526cad0","repo":"BoundaryML/baml","slug":"json-serialization-error-0","errorCode":null,"errorMessage":"JSON serialization error: {0}","messagePattern":"JSON serialization error: (.+?)","errorType":"exception","errorClass":"LogError","httpStatus":null,"severity":"error","filePath":"engine/baml-lib/baml-log/src/logger.rs","lineNumber":415,"sourceCode":"}\n\nlazy_static! {\n    /// Thread-safe configuration with runtime modification support\n    static ref CONFIG: RwLock<LogConfig> = RwLock::new(LogConfig::from_env());\n    static ref LOGGED_LINES: RwLock<HashSet<(Option<String>, Option<String>, Option<u32>)>> = RwLock::new(HashSet::new());\n    /// Optional log file path — when set, log output goes to this file instead of stdout.\n    static ref LOG_FILE: Mutex<Option<PathBuf>> = Mutex::new(None);\n}\n\n/// Error type for logging operations\n#[derive(Debug, Error)]\npub enum LogError {\n    /// Error writing to output\n    #[error(\"IO error: {0}\")]\n    Io(#[from] io::Error),\n\n    /// Error serializing to JSON\n    #[error(\"JSON serialization error: {0}\")]\n    Json(#[from] serde_json::Error),\n\n    /// Error acquiring lock\n    #[error(\"Failed to acquire lock\")]\n    LockError,\n\n    /// Configuration error\n    #[error(\"Configuration error: {0}\")]\n    Config(String),\n}\n\n// /// JSON-serializable log entry\n// #[derive(Serialize)]\n// struct LogEntry<'a> {\n//     /// Timestamp in ISO 8601 format\n//     timestamp: String,\n//     /// Log level as a string\n//     level: &'a str,","sourceCodeStart":397,"sourceCodeEnd":433,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-lib/baml-log/src/logger.rs#L397-L433","documentation":"LogError::Json wraps a serde_json::Error via #[from] and renders as 'JSON serialization error: {0}'. It occurs when the logger cannot serialize a log record or its structured fields to JSON before writing.","triggerScenarios":"Logging payloads that fail serde_json serialization — e.g. maps with non-string keys, values that serialize to invalid JSON, or custom Serialize impls that error mid-serialization.","commonSituations":"Logging unserializable structured metadata, custom types with faulty Serialize implementations, or serialization of keys serde_json cannot represent.","solutions":["Ensure logged values are JSON-serializable (string keys, supported types)","Fix or wrap custom Serialize implementations","Pre-sanitize or stringify problematic fields before logging","Match on LogError::Json and log the inner serde_json error to find the offending field"],"exampleFix":"// before\nlogger.log(json!({ 42: \"value\" }))?; // non-string key\n// after\nlogger.log(json!({ \"42\": \"value\" }))?;","handlingStrategy":"try-catch","validationCode":"// Verify payload serializes before handing it to the logger\nserde_json::to_value(&payload).map_err(|e| format!(\"unserializable log payload: {e}\"))?;","typeGuard":"fn is_json_err(e: &LogError) -> bool { matches!(e, LogError::Json(_)) }","tryCatchPattern":"match logger.log(payload) {\n    Err(LogError::Json(e)) => eprintln!(\"bad log payload: {e}\"),\n    Err(other) => return Err(other),\n    Ok(_) => {}\n}","preventionTips":["Log only JSON-safe types (string-keyed maps, primitives)","Test custom Serialize impls with serde_json::to_value in unit tests","Stringify complex objects before logging"],"tags":["json","serialization","logging","serde"],"backgroundTag":"json-serialization-failed","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"}