nautechsystems/nautilus_trader · error · anyhow::Error

Failed to convert instrument to Python: {e}

Error message

Failed to convert instrument to Python: {e}

What it means

The filter callable was resolved, but converting the Rust Instrument into a Python object via instrument_any_to_pyobject failed. This conversion can fail for instrument types that have no registered Python wrapper (pyo3 conversion error wrapped in the message).

Source

Thrown at crates/adapters/interactive_brokers/src/providers/instruments.rs:1794

        #[cfg(feature = "python")]
        {
            use nautilus_model::python::instruments::instrument_any_to_pyobject;
            use pyo3::{prelude::*, types::PyModule};

            Python::attach(|py| {
                let (module_name, callable_name) =
                    filter_callable.rsplit_once('.').ok_or_else(|| {
                        anyhow::anyhow!(
                            "Invalid filter_callable path {filter_callable:?}; expected module.callable"
                        )
                    })?;
                let callable = PyModule::import(py, module_name)
                    .map_err(|e| anyhow::anyhow!("Failed to import {module_name}: {e}"))?
                    .getattr(callable_name)
                    .map_err(|e| anyhow::anyhow!("Failed to resolve {filter_callable}: {e}"))?;
                let py_instrument = instrument_any_to_pyobject(py, instrument.clone())
                    .map_err(|e| anyhow::anyhow!("Failed to convert instrument to Python: {e}"))?;
                callable
                    .call1((py_instrument,))
                    .and_then(|result| result.extract::<bool>())
                    .map_err(|e| anyhow::anyhow!("filter_callable {filter_callable} failed: {e}"))
            })
        }

        #[cfg(not(feature = "python"))]
        {
            let _ = instrument;
            anyhow::bail!(
                "filter_callable {filter_callable:?} requires the Interactive Brokers adapter to be built with the python feature"
            );
        }
    }

    /// Batch load multiple instrument IDs.
    ///

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Update nautilus to a version whose Python bindings support the instrument type
  2. Remove or narrow the filter_callable so it is not applied to the failing instrument type
  3. Check the wrapped {e} to identify which instrument type failed conversion
  4. Raise an issue / add a conversion arm for the missing type if on a recent version

Example fix

// before
.filter_callable = "mypkg.filters.keep_all"  // applied to unsupported type
// after
// guard inside the filter pipeline: only apply filter to supported types, or drop the filter for that type
Defensive patterns

Strategy: fallback

Validate before calling

// Only apply Python filters to instrument types with Python bindings:
let supported = matches!(instrument, Instrument::Any(_) ); // verify type coverage in nautilus python bindings

Try / catch

match passes_filter_callable(instrument, callable) {
    Ok(pass) => pass,
    Err(e) if e.to_string().contains("convert instrument") => {
        log::warn!("filter skipped, conversion failed: {e}");
        true // or a safe default per your domain
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: process_contract_detail applies filter_callable to an instrument whose concrete type lacks a Python conversion (unsupported instrument type in the model's Python bindings), or the instrument payload is corrupted/invalid.

Common situations: Filtering contracts for an exotic/experimental instrument type not covered by instrument_any_to_pyobject; using a filter with an instrument type the Python bindings don't support in the installed nautilus version.

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/d4cc80728fa06ef3. Report an issue: GitHub.