Home / File/ json.py — langchain Source File

json.py — langchain Source File

Architecture documentation for json.py, a python file in the langchain codebase. 5 imports, 1 dependents.

File python Observability TelemetryUtilities 5 imports 1 dependents 7 functions

Entity Profile

Dependency Diagram

graph LR
  20a5fe84_8249_f1df_53f0_7f22870f123f["json.py"]
  20a5fe84_8249_f1df_53f0_7f22870f123f["json.py"]
  20a5fe84_8249_f1df_53f0_7f22870f123f --> 20a5fe84_8249_f1df_53f0_7f22870f123f
  b7996424_637b_0b54_6edf_2e18e9c1a8bf["re"]
  20a5fe84_8249_f1df_53f0_7f22870f123f --> b7996424_637b_0b54_6edf_2e18e9c1a8bf
  feec1ec4_6917_867b_d228_b134d0ff8099["typing"]
  20a5fe84_8249_f1df_53f0_7f22870f123f --> feec1ec4_6917_867b_d228_b134d0ff8099
  049d69ec_d53a_d170_b6fa_35c395793702["langchain_core.exceptions"]
  20a5fe84_8249_f1df_53f0_7f22870f123f --> 049d69ec_d53a_d170_b6fa_35c395793702
  2bf6d401_816d_d011_3b05_a6114f55ff58["collections.abc"]
  20a5fe84_8249_f1df_53f0_7f22870f123f --> 2bf6d401_816d_d011_3b05_a6114f55ff58
  20a5fe84_8249_f1df_53f0_7f22870f123f["json.py"]
  20a5fe84_8249_f1df_53f0_7f22870f123f --> 20a5fe84_8249_f1df_53f0_7f22870f123f
  style 20a5fe84_8249_f1df_53f0_7f22870f123f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Utilities for JSON."""

from __future__ import annotations

import json
import re
from typing import TYPE_CHECKING, Any

from langchain_core.exceptions import OutputParserException

if TYPE_CHECKING:
    from collections.abc import Callable


def _replace_new_line(match: re.Match[str]) -> str:
    """Replace newline characters in a regex match with escaped sequences.

    Args:
        match: Regex match object containing the string to process.

    Returns:
        String with newlines, carriage returns, tabs, and quotes properly escaped.
    """
    value = match.group(2)
    value = re.sub(r"\n", r"\\n", value)
    value = re.sub(r"\r", r"\\r", value)
    value = re.sub(r"\t", r"\\t", value)
    value = re.sub(r'(?<!\\)"', r"\"", value)

    return match.group(1) + value + match.group(3)


def _custom_parser(multiline_string: str | bytes | bytearray) -> str:
    r"""Custom parser for multiline strings.

    The LLM response for `action_input` may be a multiline string containing unescaped
    newlines, tabs or quotes. This function replaces those characters with their escaped
    counterparts. (newlines in JSON must be double-escaped: `\\n`).

    Returns:
        The modified string with escaped newlines, tabs and quotes.
    """
    if isinstance(multiline_string, (bytes, bytearray)):
        multiline_string = multiline_string.decode()

    return re.sub(
        r'("action_input"\:\s*")(.*?)(")',
        _replace_new_line,
        multiline_string,
        flags=re.DOTALL,
    )


# Adapted from https://github.com/KillianLucas/open-interpreter/blob/5b6080fae1f8c68938a1e4fa8667e3744084ee21/interpreter/utils/parse_partial_json.py
# MIT License


def parse_partial_json(s: str, *, strict: bool = False) -> Any:
    """Parse a JSON string that may be missing closing braces.

// ... (169 more lines)

Domain

Subdomains

Dependencies

  • collections.abc
  • json.py
  • langchain_core.exceptions
  • re
  • typing

Frequently Asked Questions

What does json.py do?
json.py is a source file in the langchain codebase, written in python. It belongs to the Observability domain, TelemetryUtilities subdomain.
What functions are defined in json.py?
json.py defines 7 function(s): _custom_parser, _parse_json, _replace_new_line, collections, parse_and_check_json_markdown, parse_json_markdown, parse_partial_json.
What does json.py depend on?
json.py imports 5 module(s): collections.abc, json.py, langchain_core.exceptions, re, typing.
What files import json.py?
json.py is imported by 1 file(s): json.py.
Where is json.py in the architecture?
json.py is located at libs/core/langchain_core/utils/json.py (domain: Observability, subdomain: TelemetryUtilities, 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