nautechsystems/nautilus_trader · error

serialize WS order request: {e}

Error message

serialize WS order request: {e}

What it means

The Kraken spot WebSocket adapter failed to serialize the outgoing order request envelope into JSON before sending it to the WS handler. serde_json cannot convert the envelope struct to a string (e.g. a serialization error in a field type), so the adapter bails with an anyhow error wrapping the serde message. This prevents the order from ever reaching Kraken, and it is raised before the request is queued in `pending`.

Source

Thrown at crates/adapters/kraken/src/websocket/dispatch/spot_orders.rs:257

            },
            identity,
            ts_now_ns,
        )
    }

    fn send(
        self: &Arc<Self>,
        mut envelope: KrakenWsRequest,
        mut identity: PendingRequest,
        ts_now_ns: u64,
    ) -> anyhow::Result<u64> {
        let req_id = self.next_req_id();
        envelope.req_id = Some(req_id);
        identity.ts_sent_ns = ts_now_ns;

        let payload = SecretString::from(
            serde_json::to_string(&envelope)
                .map_err(|e| anyhow::anyhow!("serialize WS order request: {e}"))?,
        );

        let cmd_tx = self
            .cmd_tx()
            .ok_or_else(|| anyhow::anyhow!("WS handler command sender unavailable"))?;

        self.pending.insert(req_id, identity);

        if let Err(e) = cmd_tx.send(SpotHandlerCommand::SendOrderRequest { req_id, payload }) {
            self.pending.remove(&req_id);
            anyhow::bail!("handler command channel closed: {e}");
        }

        let state_for_timeout = Arc::downgrade(self);
        let task_spawner = self.task_spawner.read().clone();
        let cancel = task_spawner.cancellation_token();
        let timeout = self.timeout;

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Check the wrapped serde error message (`{e}`) in the log to identify the exact field that failed to serialize.
  2. Verify the order parameters passed into `send` (price, quantity, type fields) are valid domain values before the envelope is built.
  3. Ensure you are on a consistent adapter version where the envelope struct and its field types match; upgrade/patch if a serialization bug exists.
  4. If you maintain the adapter, ensure no field in the envelope has a Serialize impl that can fail (use plain strings/numbers instead of maps with non-string keys).

Example fix

// before
let payload = SecretString::from(
    serde_json::to_string(&envelope)
        .map_err(|e| anyhow::anyhow!("serialize WS order request: {e}"))?,
);
// after
// Validate fields before serializing so serde can never fail:
assert!(!envelope.order_payload.is_empty());
let payload = SecretString::from(
    serde_json::to_string(&envelope)
        .map_err(|e| anyhow::anyhow!("serialize WS order request: {e}"))?,
);
Defensive patterns

Strategy: try-catch

Validate before calling

// Validate envelope fields before serializing
assert!(!envelope.order_payload.is_empty(), "order payload must not be empty");

Try / catch

match serde_json::to_string(&envelope) {
    Ok(json) => SecretString::from(json),
    Err(e) => { log::error!("WS order serialize failed: {e}"); return; }
}

Prevention

When it happens

Trigger: Calling `send` on the Kraken spot orders WS dispatcher when `serde_json::to_string(&envelope)` returns an Err — i.e. the constructed order envelope contains data serde cannot serialize (typically a non-string-key map, NaN/Infinity-style value, or a custom Serialize impl returning an error).

Common situations: Constructing an order request with an unexpected field type after an adapter upgrade; custom or modified envelope structs whose Serialize impl fails; rarely, corrupted order parameters flowing into the envelope.

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


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