BerriAI/litellm · error · ValueError

Master key must be 32 bytes for AES-256-GCM

Error message

Master key must be 32 bytes for AES-256-GCM

What it means

Constructor guard in AppCrypto: AES-256-GCM requires exactly a 32-byte key, and the supplied master_key has a different length, so the cipher cannot be initialized safely.

Solutions

  1. Set LITELLM_MASTER_KEY (or the configured master key) to a value that is exactly 32 bytes for AES-256-GCM.
  2. Generate a 32-byte key, e.g. openssl rand -hex 16, and use it consistently.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at litellm/litellm_core_utils/app_crypto.py:12 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/47dea8f07b9f315a. Report an issue: GitHub.

Appendix: source

Thrown at litellm/litellm_core_utils/app_crypto.py:12

import base64
import json
import os
from typing import Final

from cryptography.hazmat.primitives.ciphers.aead import AESGCM


class AppCrypto:
    def __init__(self, master_key: bytes):
        if len(master_key) != 32:
            raise ValueError("Master key must be 32 bytes for AES-256-GCM")
        self.key = master_key

    def encrypt_json(self, data: dict, aad: bytes | None = None) -> dict:
        aes: Final = AESGCM(self.key)
        nonce: Final = os.urandom(12)
        plaintext: Final = json.dumps(data).encode("utf-8")
        ct: Final = aes.encrypt(nonce, plaintext, aad)
        ciphertext, tag = ct[:-16], ct[-16:]
        return {
            "nonce": base64.b64encode(nonce).decode(),
            "ciphertext": base64.b64encode(ciphertext).decode(),
            "tag": base64.b64encode(tag).decode(),
        }

    def decrypt_json(self, enc: dict, aad: bytes | None = None) -> dict:
        aes: Final = AESGCM(self.key)
        nonce: Final = base64.b64decode(enc["nonce"])
        ct: Final = base64.b64decode(enc["ciphertext"])

View on GitHub (pinned to 77b7c6c40c)