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(¶ms)?;
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
- Ensure `.category(...)` and `.symbol(...)` are called on the builder before `.build()`
- Replace `expect` with `?` error propagation returning `BybitHttpError`
- If calling the public `switch_mode`, verify the category/symbol arguments are not being dropped before the builder
- 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
- Always supply both category and symbol when switching position mode
- Close/reduce positions first: Bybit rejects mode switches with open positions (distinct API error)
- Keep optional `coin` set for linear multi-asset mode when required
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
- Failed to build BybitOpenOrdersParams
- Failed to build BybitSetMarginModeParams
- Failed to build BybitSetLeverageParams
- Failed to build BybitBorrowParams
- Failed to build BybitNoConvertRepayParams
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/26241d3fbac3b8fa.
Report an issue: GitHub.