a
    ”˜ëj(  ã                   @   s¦   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZ ddlmZ eed	œd
d„Zeed	œdd„Zddœeeeedœdd„Zeed	œdd„ZdS )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                 C   s   t | tƒr|  ¡ S t| ƒS )zÙ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.
    )Ú
isinstancer   Úto_jsonr   ©r	   © r   úW/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain_core/load/dump.pyÚdefault   s    	
r   c                 C   sV   t | tƒrRt | jtƒrR| jj d¡ }rRt |tƒrR| jdd�}| ¡ |jjd< |S | S )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   Zadditional_kwargsÚgetr   Z
model_copyZ
model_dump)r	   r   Zobj_copyr   r   r   Ú_dump_pydantic_models+   s    ÿ
þýür   F)Úpretty)r	   r   Úkwargsr
   c                K   s\   d|v rd}t |ƒ‚t| ƒ} t| ƒ}|rJ| dd¡}tj|fd|i|¤ŽS tj|fi |¤ŽS )a¢  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   ÚmsgZ
serializedr   r   r   r   r   F   s    r   c                 C   s   t | ƒ} t| ƒS )av  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   r   r   r   Údumpdi   s    r   )Ú__doc__r   Útypingr   Zpydanticr   Zlangchain_core.load._validationr   Z langchain_core.load.serializabler   r   Zlangchain_core.messagesr   Zlangchain_core.outputsr   r   r   ÚboolÚstrr   r   r   r   r   r   Ú<module>   s   #