Home / Function/ pre_init() — langchain Function Reference

pre_init() — langchain Function Reference

Architecture documentation for the pre_init() function in pydantic.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  a917fc75_b82e_8b3b_09b2_09240e1a30e6["pre_init()"]
  892b1d9f_bb87_0364_19af_71382204e430["pydantic.py"]
  a917fc75_b82e_8b3b_09b2_09240e1a30e6 -->|defined in| 892b1d9f_bb87_0364_19af_71382204e430
  a917fc75_b82e_8b3b_09b2_09240e1a30e6["pre_init()"]
  a917fc75_b82e_8b3b_09b2_09240e1a30e6 -->|calls| a917fc75_b82e_8b3b_09b2_09240e1a30e6
  a917fc75_b82e_8b3b_09b2_09240e1a30e6["pre_init()"]
  a917fc75_b82e_8b3b_09b2_09240e1a30e6 -->|calls| a917fc75_b82e_8b3b_09b2_09240e1a30e6
  style a917fc75_b82e_8b3b_09b2_09240e1a30e6 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/utils/pydantic.py lines 129–186

def pre_init(func: Callable) -> Any:
    """Decorator to run a function before model initialization.

    Args:
        func: The function to run before model initialization.

    Returns:
        The decorated function.
    """
    with warnings.catch_warnings():
        warnings.filterwarnings(action="ignore", category=PydanticDeprecationWarning)

        # Ideally we would use @model_validator(mode="before") but this would change the
        # order of the validators. See https://github.com/pydantic/pydantic/discussions/7434.
        # So we keep root_validator for backward compatibility.
        @root_validator(pre=True)  # type: ignore[deprecated]
        @wraps(func)
        def wrapper(cls: type[BaseModel], values: dict[str, Any]) -> Any:
            """Decorator to run a function before model initialization.

            Args:
                cls: The model class.
                values: The values to initialize the model with.

            Returns:
                The values to initialize the model with.
            """
            # Insert default values
            fields = cls.model_fields
            for name, field_info in fields.items():
                # Check if allow_population_by_field_name is enabled
                # If yes, then set the field name to the alias
                if (
                    hasattr(cls, "Config")
                    and hasattr(cls.Config, "allow_population_by_field_name")
                    and cls.Config.allow_population_by_field_name
                    and field_info.alias in values
                ):
                    values[name] = values.pop(field_info.alias)
                if (
                    hasattr(cls, "model_config")
                    and cls.model_config.get("populate_by_name")
                    and field_info.alias in values
                ):
                    values[name] = values.pop(field_info.alias)

                if (
                    name not in values or values[name] is None
                ) and not field_info.is_required():
                    if field_info.default_factory is not None:
                        values[name] = field_info.default_factory()  # type: ignore[call-arg]
                    else:
                        values[name] = field_info.default

            # Call the decorated function
            return func(cls, values)

    return wrapper

Domain

Subdomains

Calls

Called By

Frequently Asked Questions

What does pre_init() do?
pre_init() is a function in the langchain codebase, defined in libs/core/langchain_core/utils/pydantic.py.
Where is pre_init() defined?
pre_init() is defined in libs/core/langchain_core/utils/pydantic.py at line 129.
What does pre_init() call?
pre_init() calls 1 function(s): pre_init.
What calls pre_init()?
pre_init() is called by 1 function(s): pre_init.

Analyze Your Own Codebase

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

Try Supermodel Free