{"record":{"id":"0a3f8c7432d406c0","repo":"sgl-project/sglang","slug":"invalid-mode-mode-must-be-one-of-write-rea","errorCode":null,"errorMessage":"Invalid mode: {mode}, must be one of 'write', 'read', 'skip'","messagePattern":"Invalid mode: (.+?), must be one of 'write', 'read', 'skip'","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/models/dits/glm_image.py","lineNumber":218,"sourceCode":"    def clear(self):\n        self.k_cache = None\n        self.v_cache = None\n        self.mode = None\n\n\nclass GlmImageKVCache:\n    \"\"\"Container for all layers' KV caches.\"\"\"\n\n    def __init__(self, num_layers: int):\n        self.num_layers = num_layers\n        self.caches = [GlmImageLayerKVCache() for _ in range(num_layers)]\n\n    def __getitem__(self, layer_idx: int) -> GlmImageLayerKVCache:\n        return self.caches[layer_idx]\n\n    def set_mode(self, mode: Optional[str]):\n        if mode is not None and mode not in [\"write\", \"read\", \"skip\"]:\n            raise ValueError(\n                f\"Invalid mode: {mode}, must be one of 'write', 'read', 'skip'\"\n            )\n        for cache in self.caches:\n            cache.mode = mode\n\n    def clear(self):\n        for cache in self.caches:\n            cache.clear()\n\n\nclass GlmImageTimestepEmbedding(nn.Module):\n    \"\"\"\n    Replacement for diffusers TimestepEmbedding using ReplicatedLinear.\n    Structure: linear_1 -> act(silu) -> linear_2\n    \"\"\"\n\n    def __init__(\n        self,","sourceCodeStart":200,"sourceCodeEnd":236,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/models/dits/glm_image.py#L200-L236","documentation":"GlmImageLayerKVCache.set_mode validates the per-layer KV cache mode used in glm_image generation (write = populate cache during prefill/encoding, read = reuse cached keys/values, skip = bypass). Any mode string other than 'write', 'read', 'skip' (or None to clear) raises ValueError; it is called from forward.","triggerScenarios":"forward() computes a mode string and passes it to set_mode; a mode like 'WRITE', 'cache', 'prefill', or an unexpected enum value triggers the error. Directly calling caches.set_mode('invalid') also triggers it.","commonSituations":"Refactoring the generation loop and renaming modes without updating set_mode call sites; passing an enum/str subclass whose value doesn't match; case mismatches ('Write' vs 'write').","solutions":["Pass only 'write', 'read', 'skip', or None to set_mode; check casing","Update the caller in forward that derived the invalid mode string to emit one of the three canonical values","If adding a new mode, extend the allowed list in set_mode and the cache implementation together"],"exampleFix":"# before\ncaches.set_mode(\"WRITE\")\n# after\ncaches.set_mode(\"write\")","handlingStrategy":"type-guard","validationCode":"if mode is not None:\n    assert mode in {\"write\", \"read\", \"skip\"}, f\"bad mode {mode!r}\"","typeGuard":"def is_valid_cache_mode(mode) -> bool:\n    return mode is None or (isinstance(mode, str) and mode in {\"write\", \"read\", \"skip\"})","tryCatchPattern":"try:\n    caches.set_mode(mode)\nexcept ValueError:\n    caches.set_mode(\"skip\")  # safe fallback","preventionTips":["Centralize mode strings as module-level constants instead of inline literals","Lowercase/normalize mode inputs at the boundary before calling set_mode"],"tags":["sglang","glm-image","kv-cache","mode-validation","enum-value"],"backgroundTag":"invalid-enum-value","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}