nautechsystems/nautilus_trader · error
invalid `TradingState` enum string value, was '{value}'
Error message
invalid `TradingState` enum string value, was '{value}' What it means
`trading_state_from_cstr` parses a C string into the `TradingState` enum (e.g. ACTIVE, REDUCED, HALTED, HOLD). Invalid strings cause a panic wrapped in `abort_on_panic`, terminating the process, because the C ABI offers no error channel. Only exact variant serializations from `TradingState::as_ref()` are accepted.
Source
Thrown at crates/model/src/ffi/enums.rs:882
pub extern "C" fn trading_state_to_cstr(value: TradingState) -> *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 `TradingState` variant.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn trading_state_from_cstr(ptr: *const c_char) -> TradingState {
abort_on_panic(|| {
let value = unsafe { cstr_as_str(ptr) };
TradingState::from_str(value)
.unwrap_or_else(|_| panic!("invalid `TradingState` enum string value, was '{value}'"))
})
}
#[unsafe(no_mangle)]
pub extern "C" fn trailing_offset_type_to_cstr(value: TrailingOffsetTypeOptional) -> *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 `TrailingOffsetTypeOptional` variant.
#[unsafe(no_mangle)]View on GitHub (pinned to 18893faf8b)
Solutions
- Diff the failing string against the `TradingState` variant list and use the exact canonical name.
- Produce strings via `trading_state_to_cstr(TradingState::Active)` rather than literals.
- Whitelist/validate trading-state strings in the adapter or config layer before FFI calls.
- If migrating versions, remap old variant names to the current ones in your data pipeline.
Example fix
// before
trading_state_from_cstr(str_to_cstr("active"));
// after
trading_state_from_cstr(str_to_cstr("ACTIVE")); Defensive patterns
Strategy: validation
Validate before calling
fn is_valid_trading_state(s: &str) -> bool {
TradingState::from_str(s).is_ok()
} Type guard
fn as_trading_state(s: &str) -> Option<TradingState> {
TradingState::from_str(s).ok()
} Prevention
- Use exact canonical variant names (e.g. ACTIVE, HALTED) in all adapters
- Whitelist trading-state strings from external systems before conversion
- Remap old variant names when migrating nautilus versions
When it happens
Trigger: Calling the exported `trading_state_from_cstr` with any string not exactly equal to a `TradingState` variant serialization, such as "active" (lowercase) or "trading".
Common situations: Custom adapters emitting their own state names; stale persisted state files from older versions with renamed variants; copy-pasted enum strings from a different nautilus enum.
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/f2adcf39a2c539c5.
Report an issue: GitHub.