nautechsystems/nautilus_trader · error

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

Error message

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

What it means

NautilusTrader's FFI layer exposes `price_type_from_cstr` to convert a C string into a `PriceType` enum. The library panics when the string does not match any `PriceType` variant (e.g. "LIMIT" vs "MID"). Because the FFI boundary cannot return Result types, invalid input is treated as a programming error and aborted via `abort_on_panic`.

Source

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

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

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

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Print the exact string being passed and compare against the `PriceType` variants in crates/model/src/enums (use the documented 'LIMIT', 'MARKET', etc. serializations).
  2. Use the provided `price_type_to_cstr` round-trip to generate valid strings instead of hand-writing them.
  3. In Python bindings, construct the enum via the `PriceType` PyO3 class rather than passing raw strings across FFI.
  4. If a variant was removed/renamed in a newer nautilus version, update the caller to the current variant names.

Example fix

// before
let ptr = str_to_cstr("Mid");
let pt = price_type_from_cstr(ptr);
// after
let ptr = price_type_to_cstr(PriceType::Last); // or exact variant string "LAST"
let pt = price_type_from_cstr(ptr);
Defensive patterns

Strategy: validation

Validate before calling

// Caller side (Rust)
fn is_valid_price_type(s: &str) -> bool {
    PriceType::from_str(s).is_ok()
}
// assert!(is_valid_price_type(value)); before price_type_from_cstr

Type guard

fn as_price_type(s: &str) -> Option<PriceType> {
    PriceType::from_str(s).ok()
}

Prevention

When it happens

Trigger: Calling the C FFI function `price_type_from_cstr(ptr)` with a pointer to a string that is not a valid `PriceType` serialization (typo, wrong casing, truncated string, or a value from a different enum).

Common situations: Passing Python enum `.value` strings that differ from Rust `as_ref()` names after a version upgrade; hand-built C bindings sending lowercase like "limit" instead of "LIMIT"; buffer misalignment producing garbage strings.

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/6df402e8d4f146cb. Report an issue: GitHub.