affaan-m/ECC · error · ValueError

Template name must be a non-empty string

Error message

Template name must be a non-empty string

What it means

_validate_template_input is the shared guard for the prompt template registry. It rejects any name that is not a non-empty, non-whitespace string, raising ValueError before the registry is mutated. deregister_template passes template=None, so only the name check applies there.

Solutions

  1. Pass a non-empty string as the template name
  2. Strip/validate the name at the call site before registering
  3. Fix the config/lookup that produced a None or empty name
  4. Coerce non-strings with str() only if the value is genuinely a name

Example fix

// before
register_template(cfg.get('template_name'), tpl)
// after
name = (cfg.get('template_name') or '').strip()
if not name:
    raise ValueError('template_name is required in config')
register_template(name, tpl)
Defensive patterns

Strategy: validation

Validate before calling

def valid_template_name(name) -> bool:
    return isinstance(name, str) and bool(name.strip())

Type guard

def is_nonempty_str(v: object) -> TypeGuard[str]:
    return isinstance(v, str) and bool(v.strip())

Try / catch

try:
    register_template(name, tpl)
except ValueError as e:
    if 'name must be a non-empty string' in str(e):
        logger.error('invalid template name %r', name)
        raise

Prevention

When it happens

Trigger: register_template(None, t), register_template('', t), register_template(' ', t), deregister_template(42) — any call where name is non-string, empty, or whitespace-only.

Common situations: Template names built dynamically from config that resolves to None; reading template names from JSON/YAML where missing keys become empty; passing an enum/int instead of str.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of affaan-m/ECC@8321021c54 (2026-09-16). Data as JSON: /api/errors/a66e574104c550ff. Report an issue: GitHub.

Appendix: source

Thrown at src/llm/prompt/templates/__init__.py:12

"""Provider-specific prompt template helpers."""

from __future__ import annotations

_TEMPLATE_REGISTRY: dict[str, str] = {}
TEMPLATES = _TEMPLATE_REGISTRY


def _validate_template_input(name: str, template: str | None = None) -> None:
    """Validate template registry inputs before mutating the registry."""
    if not isinstance(name, str) or not name.strip():
        raise ValueError("Template name must be a non-empty string")
    if template is not None and (not isinstance(template, str) or not template.strip()):
        raise ValueError("Template content must be a non-empty string")


def register_template(name: str, template: str) -> None:
    """Register or replace a named prompt template."""
    _validate_template_input(name, template)
    _TEMPLATE_REGISTRY[name] = template


def deregister_template(name: str) -> None:
    """Remove a named prompt template if it is registered."""
    _validate_template_input(name)
    _TEMPLATE_REGISTRY.pop(name, None)


def clear_templates() -> None:
    """Remove all registered prompt templates."""

View on GitHub (pinned to 8321021c54)