nautechsystems/nautilus_trader · critical

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

Error message

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

What it means

`book_action_from_cstr` converts a C string to the Rust `BookAction` enum and panics when the string is not a recognized variant. Because the function crosses the C FFI boundary, the panic is contained by `abort_on_panic` and aborts the host process. Only exact canonical serializations from `book_action_to_cstr` are valid input.

Source

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

pub extern "C" fn book_action_to_cstr(value: BookAction) -> *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 `BookAction` variant.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn book_action_from_cstr(ptr: *const c_char) -> BookAction {
    abort_on_panic(|| {
        let value = unsafe { cstr_as_str(ptr) };
        BookAction::from_str(value)
            .unwrap_or_else(|_| panic!("invalid `BookAction` enum string value, was '{value}'"))
    })
}

#[unsafe(no_mangle)]
pub extern "C" fn book_type_to_cstr(value: BookType) -> *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 `BookType` variant.
#[unsafe(no_mangle)]

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Log the exact string and compare with the canonical `BookAction` variant spellings in the model enums.
  2. Map feed-specific action names explicitly (feed action -> BookAction string) at the adapter boundary.
  3. Normalize case/whitespace before calling; the match is exact.
  4. Generate strings via `book_action_to_cstr` from enum values instead of literals.
  5. Validate the string against the known set before the FFI call, since failure aborts the process.

Example fix

// before
action = "remove"
book_action_from_cstr(action.encode())
// after
action = "DELETE"  # canonical BookAction variant string
book_action_from_cstr(action.encode())
Defensive patterns

Strategy: validation

Validate before calling

VALID_BOOK_ACTIONS = {"ADD", "UPDATE", "DELETE", "CLEAR"}  # verify against BookAction definition
assert action_str in VALID_BOOK_ACTIONS, f"bad BookAction: {action_str!r}"
book_action_from_cstr(action_str.encode())

Type guard

def is_valid_book_action(s: str) -> bool:
    return s in VALID_BOOK_ACTIONS  # canonical strings from BookAction::as_ref

Prevention

When it happens

Trigger: Calling `book_action_from_cstr(ptr)` with a non-canonical action string (e.g. 'insert'/'remove' instead of the canonical ADD/DELETE spellings), an empty string, or a string from a different order-book action vocabulary.

Common situations: Mapping a market data feed's book event actions to Nautilus's `BookAction`; typos in adapter code; case mismatches; version drift after enum renames.

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/9a0f6889f09b8b2b. Report an issue: GitHub.