Home / File/ dump.py — langchain Source File

dump.py — langchain Source File

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

File python CoreAbstractions Serialization 7 imports 4 functions

Entity Profile

Dependency Diagram

graph LR
  a51258b9_512f_dc1c_b25c_dc1d97b3a5d8["dump.py"]
  7025b240_fdc3_cf68_b72f_f41dac94566b["json"]
  a51258b9_512f_dc1c_b25c_dc1d97b3a5d8 --> 7025b240_fdc3_cf68_b72f_f41dac94566b
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  a51258b9_512f_dc1c_b25c_dc1d97b3a5d8 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  a51258b9_512f_dc1c_b25c_dc1d97b3a5d8 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  70b61df1_dd26_5ca0_df15_9d94ba8f1334["langchain_core.load._validation"]
  a51258b9_512f_dc1c_b25c_dc1d97b3a5d8 --> 70b61df1_dd26_5ca0_df15_9d94ba8f1334
  30d1300e_92bb_90d4_ac5e_1afe56db09d2["langchain_core.load.serializable"]
  a51258b9_512f_dc1c_b25c_dc1d97b3a5d8 --> 30d1300e_92bb_90d4_ac5e_1afe56db09d2
  d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"]
  a51258b9_512f_dc1c_b25c_dc1d97b3a5d8 --> d758344f_537f_649e_f467_b9d7442e86df
  ac2a9b92_4484_491e_1b48_ec85e71e1d58["langchain_core.outputs"]
  a51258b9_512f_dc1c_b25c_dc1d97b3a5d8 --> ac2a9b92_4484_491e_1b48_ec85e71e1d58
  style a51258b9_512f_dc1c_b25c_dc1d97b3a5d8 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Serialize LangChain objects to JSON.

Provides `dumps` (to JSON string) and `dumpd` (to dict) for serializing
`Serializable` objects.

## Escaping

During serialization, plain dicts (user data) that contain an `'lc'` key are escaped
by wrapping them: `{"__lc_escaped__": {...original...}}`. This prevents injection
attacks where malicious data could trick the deserializer into instantiating
arbitrary classes. The escape marker is removed during deserialization.

This is an allowlist approach: only dicts explicitly produced by
`Serializable.to_json()` are treated as LC objects; everything else is escaped if it
could be confused with the LC format.
"""

import json
from typing import Any

from pydantic import BaseModel

from langchain_core.load._validation import _serialize_value
from langchain_core.load.serializable import Serializable, to_json_not_implemented
from langchain_core.messages import AIMessage
from langchain_core.outputs import ChatGeneration


def default(obj: Any) -> Any:
    """Return a default value for an object.

    Args:
        obj: The object to serialize to json if it is a Serializable object.

    Returns:
        A JSON serializable object or a SerializedNotImplemented object.
    """
    if isinstance(obj, Serializable):
        return obj.to_json()
    return to_json_not_implemented(obj)


def _dump_pydantic_models(obj: Any) -> Any:
    """Convert nested Pydantic models to dicts for JSON serialization.

    Handles the special case where a `ChatGeneration` contains an `AIMessage`
    with a parsed Pydantic model in `additional_kwargs["parsed"]`. Since
    Pydantic models aren't directly JSON serializable, this converts them to
    dicts.

    Args:
        obj: The object to process.

    Returns:
        A copy of the object with nested Pydantic models converted to dicts, or
            the original object unchanged if no conversion was needed.
    """
    if (
        isinstance(obj, ChatGeneration)
        and isinstance(obj.message, AIMessage)
// ... (61 more lines)

Subdomains

Dependencies

  • json
  • langchain_core.load._validation
  • langchain_core.load.serializable
  • langchain_core.messages
  • langchain_core.outputs
  • pydantic
  • typing

Frequently Asked Questions

What does dump.py do?
dump.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 dump.py?
dump.py defines 4 function(s): _dump_pydantic_models, default, dumpd, dumps.
What does dump.py depend on?
dump.py imports 7 module(s): json, langchain_core.load._validation, langchain_core.load.serializable, langchain_core.messages, langchain_core.outputs, pydantic, typing.
Where is dump.py in the architecture?
dump.py is located at libs/core/langchain_core/load/dump.py (domain: CoreAbstractions, subdomain: Serialization, directory: libs/core/langchain_core/load).

Analyze Your Own Codebase

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

Try Supermodel Free