Ë
    tPêj–  ã                  óh   — d Z ddlmZ ddlmZmZ ddlmZmZ ddl	m
Z
  ede¬«      Z G d„ d	«      Zy
)aC  
DualCache presents a single API for reads and writes, but the two backends behave
differently: the in-memory layer can store arbitrary Python objects (including live
``BaseModel`` instances), while Redis persists strings and therefore needs JSON-safe
payloads (``json.dumps`` on the Redis side).

Call sites therefore see cache ``value`` / ``cached`` as effectively ``Any``: the same
key may deserialize to a model on one process (memory hit) or to a ``dict`` after a
Redis round-trip. ``CacheCodec`` centralizes encode/decode at that boundary:
``CacheCodec.serialize`` before ``set``, ``CacheCodec.deserialize`` after ``get``
when you need a typed ``BaseModel``.

``dataclasses`` are not supported: only ``dict`` and Pydantic ``BaseModel`` inputs
are encoded; pass a Pydantic model or convert with e.g. ``dataclasses.asdict`` first.
é    )Úannotations)ÚAnyÚTypeVar)Ú	BaseModelÚValidationError)Úverbose_proxy_loggerÚT)Úboundc                  ó6   — e Zd ZdZeddd„«       Zedd„«       Zy)Ú
CacheCodecaˆ  
    Encode/decode Pydantic models for DualCache (memory vs Redis safe payloads).

    Dataclasses are not supported yet (only ``dict`` and ``BaseModel``).

    Use ``serialize`` with ``model_type`` when writing so the same schema is used
    as on read (``deserialize``). Pass ``model_type`` whenever you know it
    (validates ``dict`` payloads and normalizes ``BaseModel`` instances).
    Nc                óü   — |�Wt        | |«      r| j                  d¬«      S t        | t        t        f«      r!|j	                  | «      j                  d¬«      S | S t        | t        «      r| j                  d¬«      S | S )u=  
        Encode a value for DualCache / Redis (``json.dumps``-safe).

        If ``model_type`` is set, the payload is validated with that model, then
        ``model_dump(mode="json")`` â€” symmetric with ``deserialize``.

        If the value is already an instance of ``model_type`` (or a subclass),
        ``model_validate`` is skipped to avoid an unnecessary Pydantic copy â€” the
        value is dumped directly.

        If ``model_type`` is omitted, any ``BaseModel`` is dumped as above; other
        values (e.g. plain ``dict``) are returned unchanged.
        Újson)Úmode)Ú
isinstanceÚ
model_dumpÚdictr   Úmodel_validate)ÚvalueÚ
model_types     út/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/proxy/common_utils/cache_pydantic_utils.pyÚ	serializezCacheCodec.serialize'   s~   € ð Ð!Ü˜% Ô,à×'Ñ'¨VÐ'Ó4Ð4Ü˜%¤$¬	Ð!2Ô3Ø!×0Ñ0°Ó7×BÑBÈÐBÓOÐOØˆLÜ�eœYÔ'Ø×#Ñ#¨Ð#Ó0Ð0Øˆó    c                óØ   — | €yt        | |«      r| S t        | t        «      r	 |j                  | «      S y# t        $ r+}t	        j
                  d|j                  |«       Y d}~yd}~ww xY w)uÚ  
        Decode a cache entry to ``model_type``.

        - ``None`` â†’ ``None``
        - Already an instance of ``model_type`` (including subclasses) â†’ returned as-is
        - ``dict`` â†’ ``model_type.model_validate(...)``; on ``ValidationError``,
          logs a warning and returns ``None`` (treat as cache miss; avoids serving
          malformed or schema-drifted entries)
        - Any other type â†’ ``None`` (caller should treat as cache miss or log)
        Nz5CacheCodec.deserialize: validation failed for %s (%s))r   r   r   r   r   ÚwarningÚ__name__)Úcachedr   Úes      r   ÚdeserializezCacheCodec.deserializeA   st   € ð ˆ>ØÜ�f˜jÔ)ØˆMÜ�fœdÔ#ðØ!×0Ñ0°Ó8Ð8ð øô #ò Ü$×,Ñ,ØKØ×'Ñ'Øôô
 ûðús   £5 µ	A)¾!A$Á$A))N)r   r   r   ztype[T] | NoneÚreturnr   )r   r   r   ztype[T]r   zT | None)r   Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr   r   © r   r   r   r      s/   „ ñð óó ðð2 òó ñr   r   N)r"   Ú
__future__r   Útypingr   r   Úpydanticr   r   Úlitellm._loggingr   r	   r   r$   r   r   ú<module>r)      s.   ðñõ  #ç ç /å 1áˆC�yÔ!€÷?ò ?r   