Home / Class/ StrictFormatter Class — langchain Architecture

StrictFormatter Class — langchain Architecture

Architecture documentation for the StrictFormatter class in formatting.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  c1656fcc_0114_0b96_5048_58dce824bbbb["StrictFormatter"]
  a6cb80fd_c1ac_c959_2d3d_637ca2511f97["formatting.py"]
  c1656fcc_0114_0b96_5048_58dce824bbbb -->|defined in| a6cb80fd_c1ac_c959_2d3d_637ca2511f97
  db9166af_1e41_82e1_ab4e_da331e45cd14["vformat()"]
  c1656fcc_0114_0b96_5048_58dce824bbbb -->|method| db9166af_1e41_82e1_ab4e_da331e45cd14
  0402a9a1_fc8f_3fb8_a0a0_662564b61b58["validate_input_variables()"]
  c1656fcc_0114_0b96_5048_58dce824bbbb -->|method| 0402a9a1_fc8f_3fb8_a0a0_662564b61b58

Relationship Graph

Source Code

libs/core/langchain_core/utils/formatting.py lines 8–75

class StrictFormatter(Formatter):
    """A string formatter that enforces keyword-only argument substitution.

    This formatter extends Python's built-in `string.Formatter` to provide stricter
    validation for prompt template formatting. It ensures that all variable
    substitutions use keyword arguments rather than positional arguments, which improves
    clarity and reduces errors when formatting prompt templates.

    Example:
        >>> fmt = StrictFormatter()
        >>> fmt.format("Hello, {name}!", name="World")
        'Hello, World!'
        >>> fmt.format("Hello, {}!", "World")  # Raises ValueError
    """

    def vformat(
        self, format_string: str, args: Sequence, kwargs: Mapping[str, Any]
    ) -> str:
        """Format a string using only keyword arguments.

        Overrides the base `vformat` to reject positional arguments, ensuring all
        substitutions are explicit and named.

        Args:
            format_string: A string containing replacement fields (e.g., `'{name}'`).
            args: Positional arguments (must be empty).
            kwargs: Keyword arguments for substitution into the format string.

        Returns:
            The formatted string with all replacement fields substituted.

        Raises:
            ValueError: If any positional arguments are provided.
        """
        if len(args) > 0:
            msg = (
                "No arguments should be provided, "
                "everything should be passed as keyword arguments."
            )
            raise ValueError(msg)
        return super().vformat(format_string, args, kwargs)

    def validate_input_variables(
        self, format_string: str, input_variables: list[str]
    ) -> None:
        """Validate that input variables match the placeholders in a format string.

        Checks that the provided input variables can be used to format the given string
        without missing or extra keys. This is useful for validating prompt templates
        before runtime.

        Args:
            format_string: A string containing replacement fields to validate
                against (e.g., `'Hello, {name}!'`).
            input_variables: List of variable names expected to fill the
                replacement fields.

        Raises:
            KeyError: If the format string contains placeholders not present
                in input_variables.

        Example:
            >>> fmt = StrictFormatter()
            >>> fmt.validate_input_variables("Hello, {name}!", ["name"])  # OK
            >>> fmt.validate_input_variables("Hello, {name}!", ["other"])  # Raises
        """
        dummy_inputs = dict.fromkeys(input_variables, "foo")
        super().format(format_string, **dummy_inputs)

Frequently Asked Questions

What is the StrictFormatter class?
StrictFormatter is a class in the langchain codebase, defined in libs/core/langchain_core/utils/formatting.py.
Where is StrictFormatter defined?
StrictFormatter is defined in libs/core/langchain_core/utils/formatting.py at line 8.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free