nautechsystems/nautilus_trader · error

`durations_seconds` request parameter must contain non-negat

Error message

`durations_seconds` request parameter must contain non-negative finite values, was {value}

What it means

parse_time_range_duration parses each element of the `durations_seconds` params array into a DurationNanos. Values must be finite JSON numbers >= 0; NaN/Infinity or negative seconds are rejected. Note null entries are allowed and mean 'no duration'.

Source

Thrown at crates/data/src/engine/time_range.rs:760

        .as_array()
        .context("`durations_seconds` request parameter must be an array")?;
    values
        .iter()
        .map(parse_time_range_duration)
        .collect::<anyhow::Result<Vec<_>>>()
}

fn parse_time_range_duration(value: &Value) -> anyhow::Result<Option<DurationNanos>> {
    if value.is_null() {
        return Ok(None);
    }

    let seconds = value.as_f64().with_context(|| {
        format!("`durations_seconds` request parameter must contain numbers or null, was {value}")
    })?;

    if !seconds.is_finite() || seconds < 0.0 {
        anyhow::bail!(
            "`durations_seconds` request parameter must contain non-negative finite values, was {value}"
        );
    }

    let nanos = seconds * NANOSECONDS_IN_SECOND as f64;
    if nanos < 1.0 {
        anyhow::bail!(
            "`durations_seconds` request parameter must contain values of at least one nanosecond, was {value}"
        );
    }

    if nanos > u64::MAX as f64 {
        anyhow::bail!("`durations_seconds` value is too large, was {value}");
    }

    Ok(Some(DurationNanos::new(nanos as u64)))
}

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Use non-negative finite numbers for every durations_seconds element
  2. Clamp computed durations to >= 0 before building the request
  3. Use null for 'absent duration' entries instead of a sentinel negative number

Example fix

// before
json!(["durations_seconds" => [-1.0]])
// after
json!(["durations_seconds" => [0.5, null]])
Defensive patterns

Strategy: validation

Validate before calling

fn valid_duration(v: &serde_json::Value) -> bool {
    v.is_null() || matches!(v.as_f64(), Some(s) if s.is_finite() && s >= 0.0)
}

Type guard

let ok = values.iter().all(valid_duration);

Prevention

When it happens

Trigger: Supplying durations_seconds entries of -1, NaN, or +Infinity/-Infinity (serde_json rejects non-finite floats, but Infinity can arise from other producers), or any negative number, in a time-range request's params.

Common situations: Config typo with a negative duration; dividing to compute a duration and getting a negative/NaN result; hand-written JSON with invalid numeric literals.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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