affaan-m/ECC · error · ValueError

Template content must be a non-empty string

Error message

Template content must be a non-empty string

What it means

_validate_template_input also checks content: when a template value is provided (register_template), it must be a non-empty, non-whitespace string. register_template passes the template through this guard, so blank or non-string bodies are rejected.

Solutions

  1. Pass the actual template text as a non-empty string
  2. Read the template file first: register_template(name, Path(p).read_text())
  3. Validate content length before registering; check the loader that returned empty content

Example fix

// before
register_template('greeting', open(tpl_path).read() if tpl_path else '')
// after
content = Path(tpl_path).read_text()
if not content.strip():
    raise ValueError(f'template file {tpl_path} is empty')
register_template('greeting', content)
Defensive patterns

Strategy: validation

Validate before calling

content = Path(tpl_path).read_text() if tpl_path else ''
if not content.strip():
    raise ValueError(f'template content for {name} is empty')

Type guard

def is_valid_template_body(v: object) -> bool:
    return isinstance(v, str) and bool(v.strip())

Try / catch

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

Prevention

When it happens

Trigger: register_template('greeting', ''), register_template('greeting', ' '), register_template('greeting', None) (template=None is treated as 'not provided' by the validator, but register requires a real template), or passing a bytes/Path object instead of str.

Common situations: Template loaded from a file that failed to read (empty string); YAML/JSON config where the template key is missing so content is None; passing a pathlib.Path rather than reading it to text.

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/40f0ec3c04a6f88f. Report an issue: GitHub.

Appendix: source

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

"""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."""
    _TEMPLATE_REGISTRY.clear()

View on GitHub (pinned to 8321021c54)