Home / Function/ parse_partial_json() — langchain Function Reference

parse_partial_json() — langchain Function Reference

Architecture documentation for the parse_partial_json() function in json.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  f5cfd8ec_27a7_20ca_e555_68843627e13c["parse_partial_json()"]
  20a5fe84_8249_f1df_53f0_7f22870f123f["json.py"]
  f5cfd8ec_27a7_20ca_e555_68843627e13c -->|defined in| 20a5fe84_8249_f1df_53f0_7f22870f123f
  f5cfd8ec_27a7_20ca_e555_68843627e13c["parse_partial_json()"]
  f5cfd8ec_27a7_20ca_e555_68843627e13c -->|calls| f5cfd8ec_27a7_20ca_e555_68843627e13c
  f5cfd8ec_27a7_20ca_e555_68843627e13c["parse_partial_json()"]
  f5cfd8ec_27a7_20ca_e555_68843627e13c -->|calls| f5cfd8ec_27a7_20ca_e555_68843627e13c
  style f5cfd8ec_27a7_20ca_e555_68843627e13c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/utils/json.py lines 58–136

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

    Args:
        s: The JSON string to parse.
        strict: Whether to use strict parsing.

    Returns:
        The parsed JSON object as a Python dictionary.
    """
    # Attempt to parse the string as-is.
    try:
        return json.loads(s, strict=strict)
    except json.JSONDecodeError:
        pass

    # Initialize variables.
    new_chars = []
    stack = []
    is_inside_string = False
    escaped = False

    # Process each character in the string one at a time.
    for char in s:
        new_char = char
        if is_inside_string:
            if char == '"' and not escaped:
                is_inside_string = False
            elif char == "\n" and not escaped:
                new_char = (
                    "\\n"  # Replace the newline character with the escape sequence.
                )
            elif char == "\\":
                escaped = not escaped
            else:
                escaped = False
        elif char == '"':
            is_inside_string = True
            escaped = False
        elif char == "{":
            stack.append("}")
        elif char == "[":
            stack.append("]")
        elif char in {"}", "]"}:
            if stack and stack[-1] == char:
                stack.pop()
            else:
                # Mismatched closing character; the input is malformed.
                return None

        # Append the processed character to the new string.
        new_chars.append(new_char)

    # If we're still inside a string at the end of processing,
    # we need to close the string.
    if is_inside_string:
        if escaped:  # Remove unterminated escape character
            new_chars.pop()
        new_chars.append('"')

    # Reverse the stack to get the closing characters.
    stack.reverse()

    # Try to parse mods of string until we succeed or run out of characters.
    while new_chars:
        # Close any remaining open structures in the reverse
        # order that they were opened.
        # Attempt to parse the modified string as JSON.
        try:
            return json.loads("".join(new_chars + stack), strict=strict)
        except json.JSONDecodeError:
            # If we still can't parse the string as JSON,
            # try removing the last character
            new_chars.pop()

    # If we got here, we ran out of characters to remove
    # and still couldn't parse the string as JSON, so return the parse error
    # for the original string.
    return json.loads(s, strict=strict)

Domain

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free