The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(proxy): stop CacheCodec dropping null fields on cache round-trip (#32207)
ryan-crabbe-berri7148c7c53d5c
diff --git a/litellm/models/managed_files.py b/litellm/models/managed_files.pyindex 24154768860..99ba764dd98 100644--- a/litellm/models/managed_files.py+++ b/litellm/models/managed_files.py@@ -51,12 +51,12 @@ class LiteLLM_ManagedVectorStoreTable(LiteLLMPydanticObjectBase):class LiteLLM_ManagedVectorStoresTable(LiteLLMPydanticObjectBase):vector_store_id: strcustom_llm_provider: str- vector_store_name: Optional[str]- vector_store_description: Optional[str]- vector_store_metadata: Optional[Dict[str, Any]]- created_at: Optional[datetime]- updated_at: Optional[datetime]- litellm_credential_name: Optional[str]- litellm_params: Optional[Dict[str, Any]]- team_id: Optional[str]- user_id: Optional[str]+ vector_store_name: Optional[str] = None+ vector_store_description: Optional[str] = None+ vector_store_metadata: Optional[Dict[str, Any]] = None+ created_at: Optional[datetime] = None+ updated_at: Optional[datetime] = None+ litellm_credential_name: Optional[str] = None+ litellm_params: Optional[Dict[str, Any]] = None+ team_id: Optional[str] = None+ user_id: Optional[str] = Nonediff --git a/litellm/proxy/common_utils/cache_pydantic_utils.py b/litellm/proxy/common_utils/cache_pydantic_utils.pyindex 25d33a0aa52..f57f6a299ae 100644--- a/litellm/proxy/common_utils/cache_pydantic_utils.py+++ b/litellm/proxy/common_utils/cache_pydantic_utils.py@@ -42,7 +42,7 @@ class CacheCodec: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", exclude_none=True)`` — symmetric with ``deserialize``.+ ``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@@ -54,12 +54,12 @@ class CacheCodec:if model_type is not None:if isinstance(value, model_type):# Already the right type: dump directly, skip re-validation copy.- return value.model_dump(mode="json", exclude_none=True)+ return value.model_dump(mode="json")if isinstance(value, (dict, BaseModel)):- return model_type.model_validate(value).model_dump(mode="json", exclude_none=True)+ return model_type.model_validate(value).model_dump(mode="json")return valueif isinstance(value, BaseModel):- return value.model_dump(mode="json", exclude_none=True)+ return value.model_dump(mode="json")return value@staticmethoddiff --git a/tests/test_litellm/proxy/common_utils/test_cache_codec.py b/tests/test_litellm/proxy/common_utils/test_cache_codec.pyindex 044d4c2d1a7..ded35227971 100644--- a/tests/test_litellm/proxy/common_utils/test_cache_codec.py+++ b/tests/test_litellm/proxy/common_utils/test_cache_codec.py@@ -1,10 +1,11 @@import logging-from typing import Optional+from typing import Any, Dict, Optionalfrom unittest.mock import patchimport pytestfrom pydantic import BaseModel, ValidationError+from litellm.models.managed_files import LiteLLM_ManagedVectorStoresTablefrom litellm.proxy.common_utils.cache_pydantic_utils import CacheCodec@@ -17,6 +18,11 @@ class _SampleSubModel(_SampleModel):pass+class _RequiredNullableModel(BaseModel):+ id: str+ budget_table: Optional[Dict[str, Any]]++class TestCacheCodecSerialize:def test_without_model_type_base_model_dumped_json_safe(self):m = _SampleModel(name="a", count=1)@@ -37,12 +43,12 @@ class TestCacheCodecSerialize:def test_with_model_type_base_model_validated_and_dumped(self):m = _SampleModel(name="c", count=None)out = CacheCodec.serialize(m, model_type=_SampleModel)- assert out == {"name": "c"}+ assert out == {"name": "c", "count": None}- def test_with_model_type_exclude_none_on_dump(self):+ def test_with_model_type_none_field_preserved_on_dump(self):out = CacheCodec.serialize({"name": "d"}, model_type=_SampleModel)- assert out == {"name": "d"}- assert "count" not in out+ assert out == {"name": "d", "count": None}+ assert "count" in outdef test_with_model_type_non_dict_non_model_passthrough(self):assert CacheCodec.serialize("raw", model_type=_SampleModel) == "raw"@@ -124,3 +130,39 @@ class TestCacheCodecDeserialize:for r in caplog.recordsif r.levelno >= logging.WARNING), f"Expected deserialize validation warning. Records: {[r.message for r in caplog.records]}"+++class TestCacheCodecRoundTripPreservesNoneFields:+ def test_none_value_kept_as_null_not_dropped(self):+ out = CacheCodec.serialize(+ _RequiredNullableModel(id="x", budget_table=None),+ model_type=_RequiredNullableModel,+ )+ assert out == {"id": "x", "budget_table": None}+ assert "budget_table" in out++ def test_required_nullable_none_field_survives_round_trip(self):+ original = _RequiredNullableModel(id="x", budget_table=None)+ wire = CacheCodec.serialize(original, model_type=_RequiredNullableModel)+ restored = CacheCodec.deserialize(wire, model_type=_RequiredNullableModel)+ assert restored == original++ def test_managed_vector_store_row_round_trips_with_optional_fields_none(self):+ vs = LiteLLM_ManagedVectorStoresTable(+ vector_store_id="vs_1",+ custom_llm_provider="openai",+ vector_store_name=None,+ vector_store_description=None,+ vector_store_metadata=None,+ created_at=None,+ updated_at=None,+ litellm_credential_name=None,+ litellm_params=None,+ team_id=None,+ user_id=None,+ )+ wire = CacheCodec.serialize(vs, model_type=LiteLLM_ManagedVectorStoresTable)+ assert wire.get("vector_store_name", "MISSING") is None+ assert wire.get("team_id", "MISSING") is None+ restored = CacheCodec.deserialize(wire, model_type=LiteLLM_ManagedVectorStoresTable)+ assert restored == vs