BoundaryML/baml · error · LogError

JSON serialization error: {0}

Error message

JSON serialization error: {0}

What it means

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.

Source

Thrown at engine/baml-lib/baml-log/src/logger.rs:415

}

lazy_static! {
    /// Thread-safe configuration with runtime modification support
    static ref CONFIG: RwLock<LogConfig> = RwLock::new(LogConfig::from_env());
    static ref LOGGED_LINES: RwLock<HashSet<(Option<String>, Option<String>, Option<u32>)>> = RwLock::new(HashSet::new());
    /// Optional log file path — when set, log output goes to this file instead of stdout.
    static ref LOG_FILE: Mutex<Option<PathBuf>> = Mutex::new(None);
}

/// Error type for logging operations
#[derive(Debug, Error)]
pub enum LogError {
    /// Error writing to output
    #[error("IO error: {0}")]
    Io(#[from] io::Error),

    /// Error serializing to JSON
    #[error("JSON serialization error: {0}")]
    Json(#[from] serde_json::Error),

    /// Error acquiring lock
    #[error("Failed to acquire lock")]
    LockError,

    /// Configuration error
    #[error("Configuration error: {0}")]
    Config(String),
}

// /// JSON-serializable log entry
// #[derive(Serialize)]
// struct LogEntry<'a> {
//     /// Timestamp in ISO 8601 format
//     timestamp: String,
//     /// Log level as a string
//     level: &'a str,

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Ensure logged values are JSON-serializable (string keys, supported types)
  2. Fix or wrap custom Serialize implementations
  3. Pre-sanitize or stringify problematic fields before logging
  4. Match on LogError::Json and log the inner serde_json error to find the offending field

Example fix

// before
logger.log(json!({ 42: "value" }))?; // non-string key
// after
logger.log(json!({ "42": "value" }))?;
Defensive patterns

Strategy: try-catch

Validate before calling

// Verify payload serializes before handing it to the logger
serde_json::to_value(&payload).map_err(|e| format!("unserializable log payload: {e}"))?;

Type guard

fn is_json_err(e: &LogError) -> bool { matches!(e, LogError::Json(_)) }

Try / catch

match logger.log(payload) {
    Err(LogError::Json(e)) => eprintln!("bad log payload: {e}"),
    Err(other) => return Err(other),
    Ok(_) => {}
}

Prevention

When it happens

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

Common situations: Logging unserializable structured metadata, custom types with faulty Serialize implementations, or serialization of keys serde_json cannot represent.

Understand the failure class

Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/ba61e72a1526cad0. Report an issue: GitHub.