
    D_i+              	           S r SSKrSSKJr  SSKJr  SSKJr  SSKJ	r	J
r
  SSKJr  SSKJr  S	\S
\4S jrS	\S
\4S jrSS.S	\S\S\S
\4S jjrS	\S
\4S jrg)a  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.
    N)Any)	BaseModel)_serialize_value)Serializableto_json_not_implemented)	AIMessage)ChatGenerationobjreturnc                 b    [        U [        5      (       a  U R                  5       $ [        U 5      $ )zReturn 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.
)
isinstancer   to_jsonr   r
   s    R/home/james-whalen/.local/lib/python3.13/site-packages/langchain_core/load/dump.pydefaultr      s(     #|$${{}"3''    c                 `   [        U [        5      (       a  [        U R                  [        5      (       ay  U R                  R                  R                  S5      =n(       aM  [        U[        5      (       a8  U R                  SS9nUR                  5       UR                  R                  S'   U$ U $ )a  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.
parsedT)deep)	r   r	   messager   additional_kwargsgetr   
model_copy
model_dump)r
   r   obj_copys      r   _dump_pydantic_modelsr   +   s      	3''s{{I..{{4488BBVBvy))>>t>,7=7H7H7J**84Jr   F)prettyr   kwargsc                    SU;   a  Sn[        U5      e[        U 5      n [        U 5      nU(       a+  UR                  SS5      n[        R
                  " U4SU0UD6$ [        R
                  " U40 UD6$ )af  Return a JSON string representation of an object.

Note:
    Plain dicts containing an `'lc'` key are automatically escaped to prevent
    confusion with LC serialization format. The escape marker is removed during
    deserialization.

Args:
    obj: The object to dump.
    pretty: Whether to pretty print the json.

        If `True`, the json will be indented by either 2 spaces or the amount
        provided in the `indent` kwarg.
    **kwargs: Additional arguments to pass to `json.dumps`

Returns:
    A JSON string representation of the object.

Raises:
    ValueError: If `default` is passed as a kwarg.
r   z'`default` should not be passed to dumpsindent   )
ValueErrorr   r   popjsondumps)r
   r   r   msg
serializedr    s         r   r%   r%   F   so    , F7o

$C!#&JHa(zz*>V>v>>::j+F++r   c                 .    [        U 5      n [        U 5      $ )aU  Return a dict representation of an object.

Note:
    Plain dicts containing an `'lc'` key are automatically escaped to prevent
    confusion with LC serialization format. The escape marker is removed during
    deserialization.

Args:
    obj: The object to dump.

Returns:
    Dictionary that can be serialized to json using `json.dumps`.
)r   r   r   s    r   dumpdr)   i   s      
$CC  r   )__doc__r$   typingr   pydanticr   langchain_core.load._validationr    langchain_core.load.serializabler   r   langchain_core.messagesr   langchain_core.outputsr	   r   r   boolstrr%   r)    r   r   <module>r4      s   "    < R - 1( ( (s s 6 ',  ,s  ,t  ,s  ,s  ,F!s !s !r   