nautechsystems/nautilus_trader · error · anyhow::Error
Failed to serialize subscription: {e}
Error message
Failed to serialize subscription: {e} What it means
`handle_subscribe` serializes the OKXSubscription (op=subscribe with args) to JSON before sending. Serialization of these strongly-typed structs should never fail in practice; if it does (corrupt/invalid subscription args producing unserializable data), the error is surfaced wrapped in this message.
Source
Thrown at crates/adapters/okx/src/websocket/handler.rs:538
self.handle_subscription_ack(&event, arg, Some(code), Some(msg))
}
async fn handle_subscribe(&self, args: Vec<OKXSubscriptionArg>) -> anyhow::Result<()> {
for arg in &args {
log::debug!(
"Subscribing to channel: channel={:?}, inst_id={:?}",
arg.channel,
arg.inst_id
);
}
let message = OKXSubscription {
op: OKXWsOperation::Subscribe,
args,
};
let json_txt = serde_json::to_string(&message)
.map_err(|e| anyhow::anyhow!("Failed to serialize subscription: {e}"))?;
self.send_with_retry(json_txt, Some(OKX_RATE_LIMIT_KEY_SUBSCRIPTION.as_slice()))
.await
.map_err(|e| anyhow::anyhow!("Failed to send subscription after retries: {e}"))?;
Ok(())
}
async fn handle_unsubscribe(&self, args: Vec<OKXSubscriptionArg>) -> anyhow::Result<()> {
for arg in &args {
log::debug!(
"Unsubscribing from channel: channel={:?}, inst_id={:?}",
arg.channel,
arg.inst_id
);
}
let message = OKXSubscription {
op: OKXWsOperation::Unsubscribe,View on GitHub (pinned to 18893faf8b)
Solutions
- Inspect the inner `{e}` and the args being subscribed; fix the arg construction to use plain strings/numbers.
- Validate subscription parameters (inst_id, channel, depth) are normal UTF-8 strings before subscribing.
- Report as a bug if it occurs with standard subscription args; serde serialization of these structs is expected to always succeed.
Defensive patterns
Strategy: validation
Validate before calling
// rust
// ensure args are plain strings before subscribing
assert!(args.iter().all(|a| !a.inst_id.as_deref().unwrap_or("").is_empty())); Prevention
- Build subscriptions only through the adapter's public subscribe API.
- Use plain string/numeric values in subscription args.
- Report persistent serialization failures as a library bug.
When it happens
Trigger: `serde_json::to_string(&OKXSubscription{...})` returns Err while handling a subscribe command — e.g. arg values containing types that fail serialization.
Common situations: Extremely rare; usually indicates a bug in constructed subscription args (e.g. non-finite floats or invalid map keys in custom arg fields) rather than a user configuration issue.
Understand the failure class
Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.
Related errors
- Failed to send subscription after retries: {e}
- Failed to serialize unsubscription: {e}
- Invalid OKX websocket channel
- Failed to send subscribe command: {e}
- Invalid `BarSpecification` for channel, was {bar_spec}
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/b1e27a147a5f5ef7.
Report an issue: GitHub.