nautechsystems/nautilus_trader · error

Expected BinaryOption, was {other:?}

Error message

Expected BinaryOption, was {other:?}

What it means

rebuild_instrument_with_tick_size rebuilds an existing instrument with a new tick size received from a market message, but it only supports BinaryOption instruments. If the passed InstrumentAny is any other variant, it fails with this error. It is an internal consistency check on the websocket market-update path.

Source

Thrown at crates/adapters/polymarket/src/http/parse.rs:275

            create_instrument_from_def(def, ts_init)
                .map_err(|e| log::warn!("Failed to create instrument {}: {e}", def.symbol))
                .ok()
        })
        .collect()
}

/// Rebuilds an instrument with a new active tick size and canonical price precision.
///
/// All other fields are preserved from `existing`. Returns a new `InstrumentAny`.
pub fn rebuild_instrument_with_tick_size(
    existing: &InstrumentAny,
    new_tick_size: &str,
    ts_event: UnixNanos,
    ts_init: UnixNanos,
) -> anyhow::Result<InstrumentAny> {
    let bo = match existing {
        InstrumentAny::BinaryOption(b) => b,
        other => anyhow::bail!("Expected BinaryOption, was {other:?}"),
    };

    let tick_size = parse_decimal_exact(new_tick_size)
        .map_err(|e| anyhow::anyhow!("Failed to parse tick size '{new_tick_size}': {e}"))?;
    let (min_price, max_price) = tick_relative_price_bounds(tick_size)?;
    let price_increment = min_price;

    let rebuilt = BinaryOption::builder()
        .instrument_id(bo.id)
        .raw_symbol(bo.raw_symbol)
        .asset_class(bo.asset_class)
        .currency(bo.currency)
        .activation_ns(bo.activation_ns)
        .expiration_ns(bo.expiration_ns)
        .price_precision(POLYMARKET_PRICE_PRECISION)
        .size_precision(bo.size_precision)
        .price_increment(price_increment)
        .size_increment(bo.size_increment)

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Ensure only BinaryOption instruments created by parse_gamma_market are subscribed to market channels.
  2. Check that the instrument for the given market/token ID was successfully built before processing tick-size updates.
  3. Log the unexpected instrument variant and instrument ID to find where the wrong type was registered.
  4. Add a variant check before calling rebuild_instrument_with_tick_size and skip non-binary instruments.

Example fix

// before
let rebuilt = rebuild_instrument_with_tick_size(&inst, tick, ts_event, ts_init)?;
// after
if !matches!(inst, InstrumentAny::BinaryOption(_)) {
    log::warn!("ignoring tick size update for non-binary instrument");
    return Ok(());
}
let rebuilt = rebuild_instrument_with_tick_size(&inst, tick, ts_event, ts_init)?;
Defensive patterns

Strategy: type-guard

Validate before calling

// before calling
if !matches!(existing, InstrumentAny::BinaryOption(_)) {
    return Ok(()); // ignore tick-size updates for non-binary instruments
}

Type guard

fn as_binary_option(inst: &InstrumentAny) -> Option<&BinaryOption> {
    match inst {
        InstrumentAny::BinaryOption(b) => Some(b),
        _ => None,
    }

Try / catch

match rebuild_instrument_with_tick_size(&inst, tick, ts_event, ts_init) {
    Ok(rebuilt) => cache.update(rebuilt),
    Err(e) if e.to_string().starts_with("Expected BinaryOption") => {
        log::warn!("tick-size update for unsupported instrument type ignored");
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: handle_market_message receives a tick-size change for a market whose stored instrument is not a BinaryOption (e.g. the instrument was never built as a binary option, or a subscription covers non-binary instruments).

Common situations: Subscribing to market channels for instruments parsed by a different adapter; a race where the tick-size message arrives before the binary option instrument was registered; mixing instrument types in one subscription set.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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