nautechsystems/nautilus_trader · error

filter is not a string or array

Error message

filter {key:?} is not a string or array

What it means

filter_values extracts string values for a named exchange instrument filter (e.g. PRICE_FILTER's quote assets, PERMISSIONS) from the exchangeInfo JSON. The JSON value must be either a string (single value) or an array of strings; any other JSON shape (number, object, bool, null) hits the catch-all bail. Sibling code produces the similar 'contains a non-string value' error for arrays holding non-strings.

Solutions

  1. Verify you are fetching the correct exchangeInfo for your market type (spot vs futures vs testnet URLs) so the filter schema matches.
  2. Update the adapter if Binance changed the filter field's shape — add handling for the new JSON type.
  3. Log the raw filter JSON for the offending key to confirm the actual shape before parsing.
  4. If using fixtures/mocks, regenerate them from a live exchangeInfo response.

Example fix

// before
_ => anyhow::bail!("filter {key:?} is not a string or array"),
// after
_ => {
    log::warn!("unexpected shape for filter {key:?}: {value:?}; skipping");
    return Ok(None); // degrade gracefully instead of failing instrument loading
}
Defensive patterns

Strategy: type-guard

Validate before calling

fn is_string_or_string_array(v: &serde_json::Value) -> bool {
    v.is_string() || v.as_array().map_or(false, |a| a.iter().all(|e| e.is_string()))
}

Type guard

fn filter_value_shape(value: &serde_json::Value) -> Option<Vec<String>> {
    match value {
        serde_json::Value::String(s) => Some(vec![s.clone()]),
        serde_json::Value::Array(a) if a.iter().all(|e| e.is_string()) =>
            Some(a.iter().filter_map(|e| e.as_str().map(String::from)).collect()),
        _ => None,
    }
}

Try / catch

match parse_exchange_info(&body) {
    Err(e) if e.to_string().contains("is not a string or array") => {
        log::error!("exchangeInfo schema changed or wrong endpoint; inspect raw filter JSON");
        Err(e.into())
    }
    other => other,
}

Prevention

When it happens

Trigger: Parsing a Binance exchangeInfo filter where the value at `key` is neither a string nor an array of strings — e.g. an API change emitting an object or number for a filter field, or fetching exchangeInfo from an unexpected endpoint/version (testnet, futures vs spot, a proxy returning an error JSON).

Common situations: Binance API/schema changes renaming or restructuring a filter field; pointing the instruments loader at the wrong market type so the response has a different schema; a cached or mocked exchangeInfo fixture with hand-written shapes.

Understand the failure class

Background: "invalid response format", "malformed payload", "missing data field": when an API returns 200 but the response shape is wrong — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at crates/adapters/binance/src/common/instruments.rs:103

fn filter_values(
    config: &BinanceInstrumentProviderConfig,
    key: &str,
) -> anyhow::Result<Option<AHashSet<String>>> {
    let Some(value) = config.filters.get(key) else {
        return Ok(None);
    };

    let values = match value {
        serde_json::Value::String(value) => vec![value.as_str()],
        serde_json::Value::Array(values) => values
            .iter()
            .map(|value| {
                value
                    .as_str()
                    .ok_or_else(|| anyhow::anyhow!("filter {key:?} contains a non-string value"))
            })
            .collect::<anyhow::Result<Vec<_>>>()?,
        _ => anyhow::bail!("filter {key:?} is not a string or array"),
    };

    Ok(Some(
        values
            .into_iter()
            .map(|value| value.trim().to_ascii_uppercase())
            .collect(),
    ))
}

fn matches_filter(values: &Option<AHashSet<String>>, value: &str) -> bool {
    values.as_ref().is_none_or(|values| contains(values, value))
}

fn contains(values: &AHashSet<String>, value: &str) -> bool {
    values.contains(&value.to_ascii_uppercase())
}

View on GitHub (pinned to 18893faf8b)