nautechsystems/nautilus_trader · error

Cannot extract selection ID from

Error message

Cannot extract selection ID from {instrument_id}

What it means

extract_selection_id splits the Betfair instrument symbol on '-' and expects the second part to be the numeric selection ID. If the symbol has fewer than two parts, or the second part fails to parse as a SelectionId (with a 'invalid selection ID' context), this error is raised.

Solutions

  1. Use symbol format '<marketId>-<selectionId>[-<handicap>]' with a numeric selection ID
  2. Verify the selection ID matches the Betfair runner ID from the market catalog
  3. Pre-validate with extract_market_id and parse the selection part as an integer before constructing the instrument

Example fix

// before
let id = InstrumentId::from("BETFAIR.1.23456789-runner");
// after
let id = InstrumentId::from("BETFAIR.1.23456789-12345");
Defensive patterns

Strategy: validation

Validate before calling

fn valid_selection_part(symbol: &str) -> bool {
    symbol.splitn(3, '-').nth(1).map_or(false, |s| s.parse::<u64>().is_ok())
}

Type guard

fn as_betfair_selection(id: &InstrumentId) -> Option<(SelectionId, Decimal)> {
    extract_selection_id(id).ok()
}

Try / catch

match extract_selection_id(&instrument_id) {
    Ok((selection_id, handicap)) => submit(selection_id, handicap),
    Err(e) => { log::error!("invalid Betfair selection in {instrument_id}: {e}"); Err(e) }
}

Prevention

When it happens

Trigger: Calling extract_selection_id (directly or via submit_order/submit_order_list) with a symbol lacking a '-'-separated selection part, or where the part after the first '-' is not a valid number, e.g. '1.23456789-ABC'.

Common situations: Malformed hand-built instrument IDs; using the market ID twice ('1.23456789-1.23456789' parse failure path); symbols reordered or truncated.

Understand the failure class

Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/4bb23f2039096820. Report an issue: GitHub.

Appendix: source

Thrown at crates/adapters/betfair/src/common/parse.rs:508

    if parts.len() >= 2 {
        Ok(parts[0].to_string())
    } else {
        anyhow::bail!("Cannot extract market ID from {instrument_id}")
    }
}

/// Extracts the selection ID and handicap from a Nautilus instrument ID.
///
/// # Errors
///
/// Returns an error if the symbol cannot be parsed into the expected format.
pub fn extract_selection_id(
    instrument_id: &InstrumentId,
) -> anyhow::Result<(SelectionId, Decimal)> {
    let symbol = instrument_id.symbol.as_str();
    let parts: Vec<&str> = symbol.splitn(3, '-').collect();
    if parts.len() < 2 {
        anyhow::bail!("Cannot extract selection ID from {instrument_id}");
    }

    let selection_id: SelectionId = parts[1]
        .parse()
        .with_context(|| format!("invalid selection ID in {instrument_id}"))?;

    let handicap = if parts.len() == 3 {
        parts[2]
            .parse::<Decimal>()
            .with_context(|| format!("invalid handicap in {instrument_id}"))?
    } else {
        Decimal::ZERO
    };

    Ok((selection_id, handicap))
}

#[cfg(test)]

View on GitHub (pinned to 18893faf8b)