{"record":{"id":"4c2140c7a50d0821","repo":"sgl-project/sglang","slug":"invalid-format-keys-must-be-integers-or-string-r","errorCode":null,"errorMessage":"Invalid format: keys must be integers (or string representations of integers) and values must be strings","messagePattern":"Invalid format: keys must be integers \\(or string representations of integers\\) and values must be strings","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/distributed/device_communicators/mooncake_transfer_engine.py","lineNumber":65,"sourceCode":"            raise RuntimeError(\n                f\"Failed to read JSON file {normalized_input}: {exc}\"\n            ) from exc\n    else:\n        try:\n            mapping = json.loads(normalized_input)\n        except json.JSONDecodeError as exc:\n            raise ValueError(f\"Invalid JSON mapping: {normalized_input}\") from exc\n\n    if not isinstance(mapping, dict):\n        raise ValueError(\n            \"Invalid format: expected a mapping from GPU id to IB device string\"\n        )\n\n    normalized_mapping: Dict[int, str] = {}\n    for gpu_key, ib_devices in mapping.items():\n        normalized_key = int(gpu_key) if str(gpu_key).isdigit() else None\n        if normalized_key is None or not isinstance(ib_devices, str):\n            raise ValueError(\n                \"Invalid format: keys must be integers (or string \"\n                \"representations of integers) and values must be strings\"\n            )\n        normalized_mapping[normalized_key] = ib_devices.strip()\n\n    if not normalized_mapping:\n        raise ValueError(\"No valid GPU mappings found in JSON\")\n\n    return normalized_mapping\n\n\ndef get_ib_devices_for_gpu(ib_device_str: Optional[str], gpu_id: int) -> Optional[str]:\n    \"\"\"\n    Parse IB device string and get IB devices for a specific GPU ID.\n\n    Supports all the following formats:\n    1. Old format: \"ib0, ib1, ib2\"\n    2. New format: {0: \"ib0, ib1\", 1: \"ib2, ib3\", 2: \"ib4\"}","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/distributed/device_communicators/mooncake_transfer_engine.py#L47-L83","documentation":"The mapping parsed into a dict, but at least one entry violates the schema: keys must be integers or digit-strings convertible to int (checked via str(gpu_key).isdigit()), and each value must be a string. parse_ib_device_config raises ValueError while normalizing entries into Dict[int, str].","triggerScenarios":"Keys like \"gpu0\", 0.5, or null, or values that are numbers/arrays (e.g. {\"0\": [\"mlx5_0\"]} or {0: 5}). str.isdigit() also rejects negative keys and non-ASCII digits.","commonSituations":"Modeling the value as a list of devices per GPU, using float GPU ids, or a mapping template with wrong value types.","solutions":["Make every key a plain non-negative integer or its string form and every value a comma-separated string: {\"0\": \"mlx5_0,mlx5_1\"}","Lint the mapping before launch: all(str(k).isdigit() and isinstance(v, str) for k, v in json.load(open(path)).items())"],"exampleFix":"# before\n{\"0\": [\"mlx5_0\", \"mlx5_1\"]}\n\n# after\n{\"0\": \"mlx5_0,mlx5_1\"}","handlingStrategy":"validation","validationCode":"import json\n\ndef lint_ib_mapping(path: str) -> None:\n    m = json.load(open(path))\n    for k, v in m.items():\n        assert str(k).isdigit(), f\"key {k!r} must be an integer\"\n        assert isinstance(v, str) and v.strip(), f\"value for {k!r} must be a non-empty string\"","typeGuard":"def valid_entries(m: dict) -> bool:\n    return all(str(k).isdigit() and isinstance(v, str) for k, v in m.items())","tryCatchPattern":"parsed = json.loads(cfg) if cfg.strip().startswith(\"{\") else cfg\nif isinstance(parsed, dict) and not valid_entries(parsed):\n    raise ConfigError(\"IB mapping values must be strings; use 'dev0,dev1' not lists\")\nparse_ib_device_config(cfg)","preventionTips":["Encode multiple devices per GPU as a single comma-separated string","Never use list values or float/negative GPU keys","Add a JSON-schema check to the config pipeline"],"tags":["json","config","schema","mooncake","type-mismatch"],"backgroundTag":"schema-validation-failed","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}