langchain-ai/langchain · error · ValueError

Cannot save prompt with partial variables.

Error message

Cannot save prompt with partial variables.

What it means

BasePromptTemplate.save() refuses to serialize a prompt that has partial_variables, raising ValueError. Serialization writes the template and its input variables; a partially substituted variable has no serializable representation in the saved prompt format, so saving is blocked up front.

Source

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

    def save(self, file_path: Path | str) -> None:
        """Save the prompt.

        Args:
            file_path: Path to directory to save prompt to.

        Raises:
            ValueError: If the prompt has partial variables.
            ValueError: If the file path is not json or yaml.
            NotImplementedError: If the prompt type is not implemented.

        Example:
            ```python
            prompt.save(file_path="path/prompt.yaml")
            ```
        """
        if self.partial_variables:
            msg = "Cannot save prompt with partial variables."
            raise ValueError(msg)

        # Fetch dictionary to save. Preserve deprecated `dict()` overrides until
        # `dict()` is removed.
        prompt_dict = self._dict_for_compat()
        if "_type" not in prompt_dict:
            msg = f"Prompt {self} does not support saving."
            raise NotImplementedError(msg)

        # Convert file to Path object.
        save_path = Path(file_path)

        directory_path = save_path.parent
        directory_path.mkdir(parents=True, exist_ok=True)

        resolved_path = save_path.resolve()
        if resolved_path.suffix == ".json":
            with resolved_path.open("w", encoding="utf-8") as f:
                json.dump(prompt_dict, f, indent=4)

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Remove the partial before saving: serialize the underlying template without .partial(), then re-apply partials at load time
  2. Save a separate config file that records the partial values, and reconstruct with partial after loading
  3. Replace the partial with a static value in the saved copy if the partial is effectively constant

Example fix

# before
prompt = PromptTemplate.from_template("News for {date}: {topic}").partial(date=today)
prompt.save("prompt.yaml")  # ValueError

# after
PromptTemplate.from_template("News for {date}: {topic}").save("prompt.yaml")
loaded = load_prompt("prompt.yaml").partial(date=today)
Defensive patterns

Strategy: fallback

Validate before calling

def can_save(prompt) -> bool:
    return not prompt.partial_variables

Prevention

When it happens

Trigger: Calling prompt.save('prompt.yaml') (or .json) after using .partial(...) or constructing with partial_variables={'k': v} where the dict is non-empty.

Common situations: Teams building prompts with runtime partials (e.g. today's date) and then trying to snapshot them to disk for versioning or deployment; LangHub-style workflows that export prompts to YAML.

Related errors


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