agentscope-ai/agentscope · error · ValueError

The injection template must contain the '{runtime_state}' pl

Error message

The injection template must contain the '{runtime_state}' placeholder, got {value!r}.

What it means

Pydantic field validator error raised when configuring an injection template (context/state injection) that lacks the required '{runtime_state}' placeholder. Without the placeholder, injected runtime state would be silently dropped, so configuration is rejected up front.

Source

Thrown at src/agentscope/agent/_config.py:244

at this point of the conversation. Anything stated earlier is outdated, and a \
later reminder, if any, supersedes this one:
{runtime_state}
</system-reminder>""",
        description=(
            "The template to wrap the injected runtime state, where the "
            "'{runtime_state}' placeholder will be replaced by the injected "
            "fields."
        ),
    )
    """The template to wrap the injected runtime state, which must contain the
    ``{runtime_state}`` placeholder."""

    @field_validator("template")
    @classmethod
    def _check_template(cls, value: str) -> str:
        """Ensure the template won't silently drop the injected fields."""
        if "{runtime_state}" not in value:
            raise ValueError(
                "The injection template must contain the '{runtime_state}' "
                f"placeholder, got {value!r}.",
            )
        return value

    injection_source: str = Field(
        title="Injection Source",
        default='{"label": "System", "sublabel": "Runtime State"}',
        description=(
            "The source of the injected hint block, which is also used to "
            "identify the previous injections within the context."
        ),
    )
    """The source of the injected hint block, used to identify the agent's own
    injections when scanning the context."""

    task_tool_names: list[str] = Field(
        title="Task Tool Names",

View on GitHub (pinned to e90f1c7592)

Solutions

  1. Add the literal '{runtime_state}' placeholder to your template string
  2. If building the template with f-strings or .format, escape other braces and keep '{runtime_state}' intact
  3. Check the config field docs for the exact expected placeholder name

Example fix

# before
config = InjectionConfig(template="Context:\n{history}")

# after
config = InjectionConfig(template="Context:\n{runtime_state}")
Defensive patterns

Strategy: validation

Validate before calling

def template_ok(template: str) -> bool:
    return "{{runtime_state}}".replace("{{", "{").replace("}}", "}") in template or "{runtime_state}" in template

Try / catch

from pydantic import ValidationError
try:
    cfg = InjectionConfig(template=t)
except ValidationError as e:
    if "runtime_state" in str(e):
        t = t + "\n{runtime_state}"
        cfg = InjectionConfig(template=t)

Prevention

When it happens

Trigger: Setting the template field of the injection config to a string that does not contain '{runtime_state}', e.g. 'User context: {history}' or escaping braces accidentally.

Common situations: Customizing the state-injection prompt template and forgetting the placeholder; using doubled braces '{{runtime_state}}' from an f-string that escapes it; copying a template from an older version with a different placeholder name.

Related errors


AI-assisted analysis of agentscope-ai/agentscope@e90f1c7592 (2026-08-28). Data as JSON: /api/errors/1d7638d94720585f. Report an issue: GitHub.