nautechsystems/nautilus_trader · error

BitMEX returned inconsistent margin account IDs: {account} a

Error message

BitMEX returned inconsistent margin account IDs: {account} and {}

What it means

Raised when parsing account state: the BitMEX /user/margin endpoint returned margin rows whose 'account' field differs among the entries. The adapter derives the account ID from the first row and treats any mismatch as a corrupted or ambiguous response, refusing to pick one arbitrarily.

Source

Thrown at crates/adapters/bitmex/src/http/client.rs:2843

    Some(FundingRateUpdate::new(
        instrument_id,
        rate,
        interval,
        None,
        ts_event,
        ts_event,
    ))
}

fn account_id_from_margins(margins: &[BitmexMargin]) -> anyhow::Result<Option<AccountId>> {
    let Some(first) = margins.first() else {
        return Ok(None);
    };

    let account = first.account;
    if let Some(mismatch) = margins.iter().find(|margin| margin.account != account) {
        anyhow::bail!(
            "BitMEX returned inconsistent margin account IDs: {account} and {}",
            mismatch.account
        );
    }

    Ok(Some(bitmex_account_id(account)))
}

#[cfg(test)]
mod tests {
    use nautilus_core::UUID4;
    use nautilus_model::enums::OrderStatus;
    use rstest::rstest;
    use serde_json::json;

    use super::*;

    fn margin_with_account(account: i64) -> BitmexMargin {

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Verify the API key corresponds to a single BitMEX account and has no cross-account token issues
  2. Log and inspect the raw margin response to confirm which account IDs are present
  3. Check for BitMEX API changes or a middleman/proxy altering the response body
  4. Retry the request — a transiently corrupted response may succeed on re-fetch

Example fix

null
Defensive patterns

Strategy: try-catch

Try / catch

match client.request_account_state(...).await {
    Ok(state) => { /* handle */ }
    Err(e) if e.to_string().contains("inconsistent margin account IDs") => {
        // log raw response, retry once, then alert — indicates an API anomaly
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Fetching balances/margin for an account where the response Vec<BitmexMargin> contains rows with different account IDs — usually only possible with an API response anomaly, unexpected multi-account data, or a malformed/intercepted response.

Common situations: Live trading on BitMEX with API keys whose wallet spans unusual states; proxy or mock servers returning concatenated/mixed responses; BitMEX API changes introducing extra account fields.

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