langchain-ai/langchain · error · ValueError

INVALID_PROMPT_INPUT

INVALID_PROMPT_INPUT

Error message

Cannot have an input variable named 'stop', as it is used internally, please rename.

What it means

BasePromptTemplate's model validator (mode='after') rejects any prompt whose input_variables contains the name 'stop'. 'stop' is reserved because prompt templates inject stop sequences internally when invoking models, so a user variable with that name would collide. Thrown as ValueError with error code INVALID_PROMPT_INPUT at validation time (object construction), not at format time.

Source

Thrown at libs/core/langchain_core/prompts/base.py:88

    Partial variables populate the template so that you don't need to pass them in every
    time you call the prompt.
    """

    metadata: builtins.dict[str, Any] | None = None
    """Metadata to be used for tracing."""

    tags: list[str] | None = None
    """Tags to be used for tracing."""

    @model_validator(mode="after")
    def validate_variable_names(self) -> Self:
        """Validate variable names do not include restricted names."""
        if "stop" in self.input_variables:
            msg = (
                "Cannot have an input variable named 'stop', as it is used internally,"
                " please rename."
            )
            raise ValueError(
                create_message(message=msg, error_code=ErrorCode.INVALID_PROMPT_INPUT)
            )
        if "stop" in self.partial_variables:
            msg = (
                "Cannot have an partial variable named 'stop', as it is used "
                "internally, please rename."
            )
            raise ValueError(
                create_message(message=msg, error_code=ErrorCode.INVALID_PROMPT_INPUT)
            )

        overall = set(self.input_variables).intersection(self.partial_variables)
        if overall:
            msg = f"Found overlapping input and partial variables: {overall}"
            raise ValueError(
                create_message(message=msg, error_code=ErrorCode.INVALID_PROMPT_INPUT)
            )
        return self

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Rename the variable in the template and in all supplied values, e.g. {stop} -> {halt} or {stop_word}
  2. If you meant to pass stop sequences to the model, supply them via the stop parameter of invoke/generate or as a runtime kwarg, not as a template variable

Example fix

# before
PromptTemplate.from_template("List {stop} words")  # ValueError

# after
PromptTemplate.from_template("List {stop_word} words")
# and pass stop sequences at call time: chain.invoke({"stop_word": ...}, config={"stop": [...]})
Defensive patterns

Strategy: validation

Validate before calling

RESERVED = {"stop"}

def check_template(src: str) -> None:
    from langchain_core.prompts.string import get_template_variables
    bad = RESERVED & set(get_template_variables(src, "f-string"))
    if bad:
        msg = f"reserved variable names in template: {bad}"
        raise ValueError(msg)

Try / catch

try:
    prompt = PromptTemplate.from_template(src)
except ValueError as e:
    if "stop" in str(e):
        src = src.replace("{stop}", "{halt}")
        prompt = PromptTemplate.from_template(src)
    else:
        raise

Prevention

When it happens

Trigger: Constructing any prompt template (PromptTemplate, ChatPromptTemplate, etc.) with input_variables=['stop'], a template string containing {stop}, or from_template('... {stop} ...'). Fires immediately on instantiation because it is a Pydantic model_validator.

Common situations: Templates about traffic controls, stop-words filters, music (stop time), or transcription prompts that legitimately use the word 'stop' as a placeholder. Also migrations from older code that passed stop via template variables instead of the stop parameter on generate/invoke.

Related errors


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/fe3863321fbb48d0. Report an issue: GitHub.