nautechsystems/nautilus_trader · critical
invalid `OptionKind` enum string value, was '{value}'
Error message
invalid `OptionKind` enum string value, was '{value}' What it means
option_kind_from_cstr converts a C string into an OptionKind enum via FromStr. Unrecognized strings cause a panic with this message; abort_on_panic escalates the panic to a process abort so nothing unwinds across the C ABI. The library accepts only the exact variant strings produced by OptionKind's string conversion.
Source
Thrown at crates/model/src/ffi/enums.rs:647
pub extern "C" fn option_kind_to_cstr(value: OptionKind) -> *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 `OptionKind` variant.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn option_kind_from_cstr(ptr: *const c_char) -> OptionKind {
abort_on_panic(|| {
let value = unsafe { cstr_as_str(ptr) };
OptionKind::from_str(value)
.unwrap_or_else(|_| panic!("invalid `OptionKind` enum string value, was '{value}'"))
})
}
#[unsafe(no_mangle)]
pub extern "C" fn oto_trigger_mode_to_cstr(value: OtoTriggerMode) -> *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 `OtoTriggerMode` variant.
#[unsafe(no_mangle)]View on GitHub (pinned to 18893faf8b)
Solutions
- Compare the '{value}' from the panic message with the valid OptionKind variant strings and pass the exact one (e.g. 'CALL'/'PUT' as defined by the enum).
- Add an explicit mapping from feed symbols ('C'/'P') to canonical variant strings before the FFI call.
- Whitelist-validate the string in the host language prior to calling.
- Ensure the C string is NUL-terminated with no surrounding whitespace.
- Rebuild against the current crate version if the enum vocabulary changed.
Example fix
// before
option_kind_from_cstr("C"); // panics: feed abbreviation
// after
option_kind_from_cstr("CALL"); // canonical OptionKind variant string Defensive patterns
Strategy: validation
Validate before calling
const OPTION_KIND_MAP = { C: "CALL", P: "PUT" };
const canonical = OPTION_KIND_MAP[raw] ?? raw;
if (canonical !== "CALL" && canonical !== "PUT") throw new Error(`unsupported OptionKind: ${raw}`); Type guard
function isOptionKind(v) { return v === 'CALL' || v === 'PUT'; } Try / catch
Not applicable: abort_on_panic terminates the process; catch blocks never run. Translate and validate before calling option_kind_from_cstr.
Prevention
- Translate feed codes ('C'/'P') to canonical variant strings at ingestion time.
- Never forward raw exchange strings into FFI enum parsers.
- Validate both case and content; matching is exact.
- Test with real feed samples during integration.
When it happens
Trigger: Calling option_kind_from_cstr(ptr) with a string that is not a valid OptionKind variant, e.g. 'CALL_OPTION', 'Put/Call', 'C', or an empty string. Only strings equal to the enum's variant representations (as defined by its FromStr impl) parse successfully.
Common situations: Ingesting option-chain data where the feed uses 'C'/'P' or 'call'/'put' abbreviations instead of the Rust variant spelling; passing a Python enum repr; case mismatches; version drift between caller and library.
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/5a0faf26961e5079.
Report an issue: GitHub.