Home / File/ pydantic_utils.py — langchain Source File

pydantic_utils.py — langchain Source File

Architecture documentation for pydantic_utils.py, a python file in the langchain codebase. 4 imports, 0 dependents.

File python CoreAbstractions Serialization 4 imports 6 functions

Entity Profile

Dependency Diagram

graph LR
  b22df862_b52d_14dc_cb85_6573258dd27c["pydantic_utils.py"]
  614e7b9f_ed51_0780_749c_ff40b74963fc["inspect"]
  b22df862_b52d_14dc_cb85_6573258dd27c --> 614e7b9f_ed51_0780_749c_ff40b74963fc
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  b22df862_b52d_14dc_cb85_6573258dd27c --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  b22df862_b52d_14dc_cb85_6573258dd27c --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  63ae09a2_4a67_3eaf_7888_b0fd11776a0e["pydantic.v1"]
  b22df862_b52d_14dc_cb85_6573258dd27c --> 63ae09a2_4a67_3eaf_7888_b0fd11776a0e
  style b22df862_b52d_14dc_cb85_6573258dd27c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from inspect import isclass
from typing import Any

from pydantic import BaseModel
from pydantic.v1 import BaseModel as BaseModelV1


# Function to replace allOf with $ref
def replace_all_of_with_ref(schema: Any) -> None:
    if isinstance(schema, dict):
        # If the schema has an allOf key with a single item that contains a $ref
        if (
            "allOf" in schema
            and len(schema["allOf"]) == 1
            and "$ref" in schema["allOf"][0]
        ):
            schema["$ref"] = schema["allOf"][0]["$ref"]
            del schema["allOf"]
            if "default" in schema and schema["default"] is None:
                del schema["default"]
        else:
            # Recursively process nested schemas
            for value in schema.values():
                if isinstance(value, (dict, list)):
                    replace_all_of_with_ref(value)
    elif isinstance(schema, list):
        for item in schema:
            replace_all_of_with_ref(item)


def remove_all_none_default(schema: Any) -> None:
    """Removing all none defaults.

    Pydantic v1 did not generate these, but Pydantic v2 does.

    The None defaults usually represent **NotRequired** fields, and the None value
    is actually **incorrect** as a value since the fields do not allow a None value.

    See difference between Optional and NotRequired types in python.
    """
    if isinstance(schema, dict):
        for value in schema.values():
            if isinstance(value, dict):
                if "default" in value and value["default"] is None:
                    any_of = value.get("anyOf", [])
                    for type_ in any_of:
                        if "type" in type_ and type_["type"] == "null":
                            break  # Null type explicitly defined
                    else:
                        del value["default"]
                remove_all_none_default(value)
            elif isinstance(value, list):
                for item in value:
                    remove_all_none_default(item)
    elif isinstance(schema, list):
        for item in schema:
            remove_all_none_default(item)


def _remove_enum(obj: Any) -> None:
// ... (91 more lines)

Subdomains

Dependencies

  • inspect
  • pydantic
  • pydantic.v1
  • typing

Frequently Asked Questions

What does pydantic_utils.py do?
pydantic_utils.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, Serialization subdomain.
What functions are defined in pydantic_utils.py?
pydantic_utils.py defines 6 function(s): _normalize_schema, _remove_additionalproperties, _remove_enum, _schema, remove_all_none_default, replace_all_of_with_ref.
What does pydantic_utils.py depend on?
pydantic_utils.py imports 4 module(s): inspect, pydantic, pydantic.v1, typing.
Where is pydantic_utils.py in the architecture?
pydantic_utils.py is located at libs/core/tests/unit_tests/pydantic_utils.py (domain: CoreAbstractions, subdomain: Serialization, directory: libs/core/tests/unit_tests).

Analyze Your Own Codebase

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

Try Supermodel Free