sgl-project/sglang · error · ValueError

Invalid JSON mapping: {normalized_input}

Error message

Invalid JSON mapping: {normalized_input}

What it means

The IB-device config string started with '{' (detected as an inline JSON mapping) but json.loads could not parse it as JSON. parse_ib_device_config raises ValueError with the raw input embedded, chained from JSONDecodeError identifying the syntax position.

Source

Thrown at python/sglang/srt/distributed/device_communicators/mooncake_transfer_engine.py:54

    if normalized_input.endswith(".json"):
        if not os.path.isfile(normalized_input):
            raise RuntimeError(f"File {normalized_input} does not exist.")
        try:
            with open(normalized_input, "r", encoding="utf-8") as file:
                mapping = json.load(file)
        except json.JSONDecodeError as exc:
            raise RuntimeError(
                f"Failed to parse JSON content from file {normalized_input}"
            ) from exc
        except (IOError, OSError) as exc:
            raise RuntimeError(
                f"Failed to read JSON file {normalized_input}: {exc}"
            ) from exc
    else:
        try:
            mapping = json.loads(normalized_input)
        except json.JSONDecodeError as exc:
            raise ValueError(f"Invalid JSON mapping: {normalized_input}") from exc

    if not isinstance(mapping, dict):
        raise ValueError(
            "Invalid format: expected a mapping from GPU id to IB device string"
        )

    normalized_mapping: Dict[int, str] = {}
    for gpu_key, ib_devices in mapping.items():
        normalized_key = int(gpu_key) if str(gpu_key).isdigit() else None
        if normalized_key is None or not isinstance(ib_devices, str):
            raise ValueError(
                "Invalid format: keys must be integers (or string "
                "representations of integers) and values must be strings"
            )
        normalized_mapping[normalized_key] = ib_devices.strip()

    if not normalized_mapping:
        raise ValueError("No valid GPU mappings found in JSON")

View on GitHub (pinned to 0132848349)

Solutions

  1. Use strict JSON with double quotes and no trailing commas: --mooncake-ip-device '{"0": "mlx5_0", "1": "mlx5_2"}'
  2. Test the literal locally: echo '<value>' | python -m json.tool
  3. Simplify: if a single IB device applies to all GPUs, pass the plain device string without braces

Example fix

# before
--mooncake-ip-device "{'0': 'mlx5_0'}"   # Python-style -> ValueError

# after
--mooncake-ip-device '{"0": "mlx5_0"}'  # strict JSON
Defensive patterns

Strategy: validation

Validate before calling

import json

def validate_inline_ib_mapping(cfg: str) -> dict:
    s = cfg.strip()
    if s.startswith("{"):
        return json.loads(s)  # fail fast with exact position
    return s

Try / catch

try:
    mapping = parse_ib_device_config(cfg)
except ValueError as e:
    raise ConfigError(f"inline IB mapping is not strict JSON: {e}") from e

Prevention

When it happens

Trigger: Passing an inline mapping string like "{'0': 'mlx5_0'}" (single quotes), trailing commas, or a stray '{' prefix, instead of strict double-quoted JSON in the CLI/file argument.

Common situations: Copy-pasting a Python dict literal into --mooncake-ip-device, shell quoting that mangles or truncates the JSON, or forgetting to close the braces.

Understand the failure class

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/eafe7c6222ab3652. Report an issue: GitHub.