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
- Print the exact string being passed and compare against the `PriceType` variants in crates/model/src/enums (use the documented 'LIMIT', 'MARKET', etc. serializations).
- Use the provided `price_type_to_cstr` round-trip to generate valid strings instead of hand-writing them.
- In Python bindings, construct the enum via the `PriceType` PyO3 class rather than passing raw strings across FFI.
- 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
- Always derive strings via price_type_to_cstr rather than hand-written literals
- Whitelist-validate enum strings in adapter/config layers before FFI calls
- Keep bindings' constant tables in sync when upgrading nautilus_model
- Watch for casing and whitespace in cross-language string handling
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
- invalid `AggressorSide` enum string value, was '{value}'
- invalid `AssetClass` enum string value, was '{value}'
- invalid `InstrumentClass` enum string value, was '{value}'
- invalid `BarAggregation` enum string value, was '{value}'
- invalid `BookAction` enum string value, was '{value}'
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/6df402e8d4f146cb.
Report an issue: GitHub.