nautechsystems/nautilus_trader · error

Failed to build BybitSwitchModeParams

Error message

Failed to build BybitSwitchModeParams

What it means

`switch_mode` builds `BybitSwitchModeParams` (category/symbol plus optional coin) and unwraps `build()` with `expect`. Since derive_builder `build()` only errs on missing required (no-default) fields, the panic means `category` or `symbol` was never set on the builder before `build()`.

Source

Thrown at crates/adapters/bybit/src/http/client.rs:1321

        mode: BybitPositionMode,
        symbol: Option<String>,
        coin: Option<String>,
    ) -> Result<BybitSwitchModeResponse, BybitHttpError> {
        let mut builder = BybitSwitchModeParamsBuilder::default();
        builder.category(product_type);
        builder.mode(mode);

        if let Some(s) = symbol {
            builder.symbol(s);
        }

        if let Some(c) = coin {
            builder.coin(c);
        }

        let params = builder
            .build()
            .expect("Failed to build BybitSwitchModeParams");

        let body = serde_json::to_vec(&params)?;
        self.send_request::<_, ()>(
            Method::POST,
            "/v5/position/switch-mode",
            None,
            Some(body),
            true,
        )
        .await
    }

    /// Sets trading stop parameters including trailing stops.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Credentials are missing.

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Ensure `.category(...)` and `.symbol(...)` are called on the builder before `.build()`
  2. Replace `expect` with `?` error propagation returning `BybitHttpError`
  3. If calling the public `switch_mode`, verify the category/symbol arguments are not being dropped before the builder
  4. File a bug if reproducible through the public API

Example fix

// before
let params = builder.build().expect("Failed to build BybitSwitchModeParams");
// after
let params = builder.build().map_err(BybitHttpError::InvalidParams)?;
Defensive patterns

Strategy: validation

Validate before calling

// category and symbol are required for switch_mode
assert!(!symbol.is_empty(), "symbol required for switch_mode");

Try / catch

client.switch_mode(cat, sym, coin).await.map_err(|e| { log::error!("switch_mode: {e}"); e })?;

Prevention

When it happens

Trigger: A code path reaching `switch_mode` where `builder.category(...)` or `builder.symbol(...)` was skipped — e.g. a refactor dropping a setter, or calling the raw builder without those fields.

Common situations: Directly using `BybitSwitchModeParamsBuilder` and forgetting `category`/`symbol`; merge conflicts that removed a setter line; internal code drift after API model changes.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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