Home / File/ uuid.py — langchain Source File

uuid.py — langchain Source File

Architecture documentation for uuid.py, a python file in the langchain codebase. 3 imports, 3 dependents.

File python CoreAbstractions Serialization 3 imports 3 dependents 3 functions

Entity Profile

Dependency Diagram

graph LR
  23a1b3c0_d78f_7f2f_8eca_25b1ca6a84a6["uuid.py"]
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  23a1b3c0_d78f_7f2f_8eca_25b1ca6a84a6 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  23a1b3c0_d78f_7f2f_8eca_25b1ca6a84a6["uuid.py"]
  23a1b3c0_d78f_7f2f_8eca_25b1ca6a84a6 --> 23a1b3c0_d78f_7f2f_8eca_25b1ca6a84a6
  f1d1de3b_825d_cf9b_9a37_9949d0b9c4c5["uuid_utils.compat"]
  23a1b3c0_d78f_7f2f_8eca_25b1ca6a84a6 --> f1d1de3b_825d_cf9b_9a37_9949d0b9c4c5
  344b2838_87a8_d5dc_b550_fdb443ff6c4e["function_calling.py"]
  344b2838_87a8_d5dc_b550_fdb443ff6c4e --> 23a1b3c0_d78f_7f2f_8eca_25b1ca6a84a6
  02897ee0_0cd2_75d9_9323_b7df8540b3b3["utils.py"]
  02897ee0_0cd2_75d9_9323_b7df8540b3b3 --> 23a1b3c0_d78f_7f2f_8eca_25b1ca6a84a6
  23a1b3c0_d78f_7f2f_8eca_25b1ca6a84a6["uuid.py"]
  23a1b3c0_d78f_7f2f_8eca_25b1ca6a84a6 --> 23a1b3c0_d78f_7f2f_8eca_25b1ca6a84a6
  style 23a1b3c0_d78f_7f2f_8eca_25b1ca6a84a6 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""UUID utility functions.

This module exports a uuid7 function to generate monotonic, time-ordered UUIDs
for tracing and similar operations.
"""

from __future__ import annotations

import typing
from uuid import UUID

from uuid_utils.compat import uuid7 as _uuid_utils_uuid7

if typing.TYPE_CHECKING:
    from uuid import UUID

_NANOS_PER_SECOND: typing.Final = 1_000_000_000


def _to_timestamp_and_nanos(nanoseconds: int) -> tuple[int, int]:
    """Split a nanosecond timestamp into seconds and remaining nanoseconds."""
    seconds, nanos = divmod(nanoseconds, _NANOS_PER_SECOND)
    return seconds, nanos


def uuid7(nanoseconds: int | None = None) -> UUID:
    """Generate a UUID from a Unix timestamp in nanoseconds and random bits.

    UUIDv7 objects feature monotonicity within a millisecond.

    Args:
        nanoseconds: Optional ns timestamp. If not provided, uses current time.

    Returns:
        A UUIDv7 object.
    """
    # --- 48 ---   -- 4 --   --- 12 ---   -- 2 --   --- 30 ---   - 32 -
    # unix_ts_ms | version | counter_hi | variant | counter_lo | random
    #
    # 'counter = counter_hi | counter_lo' is a 42-bit counter constructed
    # with Method 1 of RFC 9562, §6.2, and its MSB is set to 0.
    #
    # 'random' is a 32-bit random value regenerated for every new UUID.
    #
    # If multiple UUIDs are generated within the same millisecond, the LSB
    # of 'counter' is incremented by 1. When overflowing, the timestamp is
    # advanced and the counter is reset to a random 42-bit integer with MSB
    # set to 0.

    # For now, just delegate to the uuid_utils implementation
    if nanoseconds is None:
        return _uuid_utils_uuid7()
    seconds, nanos = _to_timestamp_and_nanos(nanoseconds)
    return _uuid_utils_uuid7(timestamp=seconds, nanos=nanos)


__all__ = ["uuid7"]

Subdomains

Dependencies

Frequently Asked Questions

What does uuid.py do?
uuid.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 uuid.py?
uuid.py defines 3 function(s): _to_timestamp_and_nanos, uuid, uuid7.
What does uuid.py depend on?
uuid.py imports 3 module(s): typing, uuid.py, uuid_utils.compat.
What files import uuid.py?
uuid.py is imported by 3 file(s): function_calling.py, utils.py, uuid.py.
Where is uuid.py in the architecture?
uuid.py is located at libs/core/langchain_core/utils/uuid.py (domain: CoreAbstractions, subdomain: Serialization, directory: libs/core/langchain_core/utils).

Analyze Your Own Codebase

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

Try Supermodel Free