MemPalace/mempalace · error · ValueError

{field_name} contains invalid characters

Error message

{field_name} contains invalid characters

What it means

Raised by sanitize_name() when the name contains characters outside the safe-name character set defined by _SAFE_NAME_RE in mempalace/config.py. Wing/room names become filesystem directories and storage ids, so only a conservative allowed set (broadly: letters, digits, underscore, hyphen, space, and similar safe punctuation) is permitted; anything else — punctuation like ':' or '*', control chars, emoji in some configs — is rejected.

Source

Thrown at mempalace/config.py:96

    if not isinstance(value, str) or not value.strip():
        raise ValueError(f"{field_name} must be a non-empty string")

    value = value.strip()

    if len(value) > MAX_NAME_LENGTH:
        raise ValueError(f"{field_name} exceeds maximum length of {MAX_NAME_LENGTH} characters")

    # Block path traversal
    if ".." in value or "/" in value or "\\" in value:
        raise ValueError(f"{field_name} contains invalid path characters")

    # Block null bytes
    if "\x00" in value:
        raise ValueError(f"{field_name} contains null bytes")

    # Enforce safe character set
    if not _SAFE_NAME_RE.match(value):
        raise ValueError(f"{field_name} contains invalid characters")

    return value


def sanitize_kg_value(value: str, field_name: str = "value") -> str:
    """Validate a knowledge-graph entity name (subject or object).

    More permissive than sanitize_name — allows punctuation like commas,
    colons, and parentheses that are common in natural-language KG values.
    Only blocks null bytes and over-length strings.

    Not used for wing/room names (which have filesystem constraints) or
    predicates (which should be simple relationship identifiers).
    """
    if not isinstance(value, str) or not value.strip():
        raise ValueError(f"{field_name} must be a non-empty string")

    value = value.strip()

View on GitHub (pinned to 06cb6987f0)

Solutions

  1. Normalize the name to the safe slug style the library itself uses: lowercase, spaces/hyphens to underscores (see the slug helper above sanitize_name in config.py)
  2. Check the _SAFE_NAME_RE pattern in your installed mempalace/config.py to know the exact allowed set
  3. Move punctuation-heavy display titles into content/metadata and use a clean slug as the name

Example fix

# before
create_room("Room: #1 (draft)*")

# after
create_room("room_1_draft")
Defensive patterns

Strategy: validation

Validate before calling

# Normalize to the library's own slug convention before calling:
name = name.lower().replace(" ", "_").replace("-", "_").strip("_")
# then check the safe set with the same rule the library uses

Type guard

from mempalace.config import _SAFE_NAME_RE  # if exported/accessible

def is_safe_name(value: str) -> bool:
    return bool(_SAFE_NAME_RE.match(value.strip())) if hasattr(globals().get('_SAFE_NAME_RE', None), 'match') else None
# Prefer copying the regex from your installed config.py into your validator.

Try / catch

try:
    safe = sanitize_name(name)
except ValueError as exc:
    if "invalid characters" in str(exc):
        safe = sanitize_name(re.sub(r"[^\w\- ]", "_", name))
    else:
        raise

Prevention

When it happens

Trigger: Passing a name with characters not matched by _SAFE_NAME_RE, e.g. "room:1", "bug*fix", names with control characters, or other symbols; the exact allowed set is whatever the compiled regex in config.py permits.

Common situations: Natural-language titles with punctuation used as names; LLM-generated names with colons or asterisks; locale-specific characters not covered by the safe set; copy-paste introducing invisible control characters.

Understand the failure class

Related errors


AI-assisted analysis of MemPalace/mempalace@06cb6987f0 (2026-08-15). Data as JSON: /api/errors/83cd1e28ed507ac7. Report an issue: GitHub.