{"record":{"id":"ee5d8eaa286f6324","repo":"nautechsystems/nautilus_trader","slug":"invalid-bybitpositionmode-value-int-val","errorCode":null,"errorMessage":"Invalid BybitPositionMode value: {int_val}","messagePattern":"Invalid BybitPositionMode value: (.+?)","errorType":"validation","errorClass":"PyValueError","httpStatus":null,"severity":"error","filePath":"crates/adapters/bybit/src/python/enums.rs","lineNumber":298,"sourceCode":"    pub fn value(&self) -> i32 {\n        *self as i32\n    }\n\n    #[staticmethod]\n    #[must_use]\n    fn variants() -> Vec<String> {\n        Self::iter().map(|x| x.to_string()).collect()\n    }\n\n    #[classmethod]\n    #[pyo3(name = \"from_str\")]\n    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {\n        // Try to extract as integer first (for API payloads that send 0 or 3)\n        if let Ok(int_val) = data.extract::<i32>() {\n            return match int_val {\n                0 => Ok(Self::MergedSingle),\n                3 => Ok(Self::BothSides),\n                _ => Err(to_pyvalue_err(anyhow::anyhow!(\n                    \"Invalid BybitPositionMode value: {int_val}\"\n                ))),\n            };\n        }\n\n        // Fall back to string parsing for variant names\n        let data_str: String = data.str()?.extract()?;\n        Self::from_str(&data_str).map_err(to_pyvalue_err)\n    }\n}\n\n#[pymethods]\n#[pyo3_stub_gen::derive::gen_stub_pymethods]\nimpl BybitPositionIdx {\n    /// Position index values used for hedge mode payloads.\n    #[new]\n    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {\n        let t = Self::type_object(py);","sourceCodeStart":280,"sourceCodeEnd":316,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/bybit/src/python/enums.rs#L280-L316","documentation":"The Python binding for BybitPositionMode converts incoming API payload integers to enum variants: only 0 (MergedSingle) and 3 (BothSides) are accepted. Any other integer, or an unrecognized variant-name string, raises this error.","triggerScenarios":"Deserializing a Bybit position-mode value from a payload where the integer is not 0 or 3 (e.g. 1 or 2), or passing an invalid string to BybitPositionMode.from_str in Python.","commonSituations":"Feeding values from another exchange's position-mode encoding; Bybit docs/versions using a different integer mapping; hand-written config using 'hedge' instead of 'BothSides'.","solutions":["Use 0 for MergedSingle (one-way) or 3 for BothSides (hedge mode) in integer payloads","Pass exact variant names as strings: 'MergedSingle' or 'BothSides'","Check the Bybit account/position-mode API response for unexpected values and update the adapter if Bybit added new modes"],"exampleFix":"// before: wrong integer mapping\nmode = BybitPositionMode.from_str(1)\n// after\nmode = BybitPositionMode.from_str(3)  # BothSides (hedge mode)","handlingStrategy":"type-guard","validationCode":"if isinstance(v, int) and v not in (0, 3):\n    raise ValueError(f'position mode must be 0 (MergedSingle) or 3 (BothSides), got {v}')","typeGuard":"def is_valid_position_mode(v) -> bool:\n    return v in (0, 3) or str(v) in ('MergedSingle', 'BothSides')","tryCatchPattern":"try:\n    mode = BybitPositionMode.from_str(raw)\nexcept ValueError as e:\n    log.warning('bad position mode %r', raw); mode = BybitPositionMode.MergedSingle","preventionTips":["Use the documented integer mapping 0/3 only","Prefer variant-name strings in hand-written config","Centralize mode parsing in one helper"],"tags":["rust","python","bybit","enum","position-mode"],"backgroundTag":"invalid-enum-value","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}