Home / File/ strings.py — langchain Source File

strings.py — langchain Source File

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

Entity Profile

Dependency Diagram

graph LR
  7ba4a23d_cad8_05ff_f913_ec9c15a6f08f["strings.py"]
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  7ba4a23d_cad8_05ff_f913_ec9c15a6f08f --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  7ba4a23d_cad8_05ff_f913_ec9c15a6f08f --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  style 7ba4a23d_cad8_05ff_f913_ec9c15a6f08f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""String utilities."""

from collections.abc import Iterable
from typing import Any


def stringify_value(val: Any) -> str:
    """Stringify a value.

    Args:
        val: The value to stringify.

    Returns:
        The stringified value.
    """
    if isinstance(val, str):
        return val
    if isinstance(val, dict):
        return "\n" + stringify_dict(val)
    if isinstance(val, list):
        return "\n".join(stringify_value(v) for v in val)
    return str(val)


def stringify_dict(data: dict) -> str:
    """Stringify a dictionary.

    Args:
        data: The dictionary to stringify.

    Returns:
        The stringified dictionary.
    """
    return "".join(f"{key}: {stringify_value(value)}\n" for key, value in data.items())


def comma_list(items: Iterable[Any]) -> str:
    """Convert an iterable to a comma-separated string.

    Args:
        items: The iterable to convert.

    Returns:
        The comma-separated string.
    """
    return ", ".join(str(item) for item in items)


def sanitize_for_postgres(text: str, replacement: str = "") -> str:
    r"""Sanitize text by removing NUL bytes that are incompatible with PostgreSQL.

    PostgreSQL text fields cannot contain `NUL (0x00)` bytes, which can cause
    `psycopg.DataError` when inserting documents. This function removes or replaces
    such characters to ensure compatibility.

    Args:
        text: The text to sanitize.
        replacement: String to replace `NUL` bytes with.

    Returns:
        The sanitized text with `NUL` bytes removed or replaced.

    Example:
        >>> sanitize_for_postgres("Hello\\x00world")
        'Helloworld'
        >>> sanitize_for_postgres("Hello\\x00world", " ")
        'Hello world'
    """
    return text.replace("\x00", replacement)

Subdomains

Dependencies

  • collections.abc
  • typing

Frequently Asked Questions

What does strings.py do?
strings.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What functions are defined in strings.py?
strings.py defines 4 function(s): comma_list, sanitize_for_postgres, stringify_dict, stringify_value.
What does strings.py depend on?
strings.py imports 2 module(s): collections.abc, typing.
Where is strings.py in the architecture?
strings.py is located at libs/core/langchain_core/utils/strings.py (domain: CoreAbstractions, subdomain: RunnableInterface, 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