nautechsystems/nautilus_trader · critical

invalid `AssetClass` enum string value, was '{value}'

Error message

invalid `AssetClass` enum string value, was '{value}'

What it means

This panic is raised by `asset_class_from_cstr`, the C FFI converter from a C string to the Rust `AssetClass` enum. It fires when the input string does not parse as an `AssetClass` variant via `FromStr`. The wrapper executes inside `abort_on_panic`, so an unrecognized value aborts the process instead of returning an error code. Only exact canonical variant strings (as emitted by `asset_class_to_cstr`) are accepted.

Source

Thrown at crates/model/src/ffi/enums.rs:365

pub extern "C" fn asset_class_to_cstr(value: AssetClass) -> *const c_char {
    str_to_cstr(value.as_ref())
}

/// Returns an enum from a C string.
///
/// # Safety
///
/// Assumes `ptr` is a valid C string pointer.
///
/// # Panics
///
/// Panics if the C string does not correspond to a valid `AssetClass` variant.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn asset_class_from_cstr(ptr: *const c_char) -> AssetClass {
    abort_on_panic(|| {
        let value = unsafe { cstr_as_str(ptr) };
        AssetClass::from_str(value)
            .unwrap_or_else(|_| panic!("invalid `AssetClass` enum string value, was '{value}'"))
    })
}

#[unsafe(no_mangle)]
pub extern "C" fn instrument_class_to_cstr(value: InstrumentClass) -> *const c_char {
    str_to_cstr(value.as_ref())
}

/// Returns an enum from a C string.
///
/// # Safety
///
/// Assumes `ptr` is a valid C string pointer.
///
/// # Panics
///
/// Panics if the C string does not correspond to a valid `InstrumentClass` variant.
#[unsafe(no_mangle)]

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Print the offending string and diff it against the canonical `AssetClass` variant strings in the model enums.
  2. Construct the value via the library's `AssetClass` enum and serialize with `asset_class_to_cstr` rather than typing strings by hand.
  3. Normalize case and strip whitespace before the call; the parse is exact-match.
  4. Add an explicit mapping from external feed vocabulary to `AssetClass` strings at the adapter boundary.
  5. Validate against the accepted variant list before calling, since invalid input aborts the process.

Example fix

// before
asset_class = "future"
asset_class_from_cstr(asset_class.encode())
// after
asset_class = "FUTURE"  # exact canonical variant spelling
asset_class_from_cstr(asset_class.encode())
Defensive patterns

Strategy: validation

Validate before calling

VALID_ASSET_CLASSES = {"FX", "CFD", "INDEX", "EQUITY", "FUTURE", "OPTION"}  # from AssetClass definition
assert asset_class_str in VALID_ASSET_CLASSES, f"bad AssetClass: {asset_class_str!r}"
asset_class_from_cstr(asset_class_str.encode())

Type guard

def is_valid_asset_class(s: str) -> bool:
    return s in VALID_ASSET_CLASSES  # canonical strings from AssetClass::as_ref

Prevention

When it happens

Trigger: Calling `asset_class_from_cstr(ptr)` with a string like 'future' in the wrong case, an external feed's own classification term, an empty string, or any value not exactly matching an `AssetClass` serialization.

Common situations: Hard-coding asset class strings in config files; passing exchange/broker asset classifications through unmodified; version drift after a variant rename; typos or case mismatches.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/18591602bd262865. Report an issue: GitHub.