Skip to content

Conversation

@pmorch
Copy link

@pmorch pmorch commented Nov 6, 2025

Title

Fix pydantic serialization warnings

Relevant issues

Fixes #11759

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • I have added a screenshot of my new test passing locally
  • My PR passes all unit tests on make test-unit
    • See below
  • My PR's scope is as isolated as possible, it only solves 1 specific problem
image

Pre-existing test failures:

4 Pre-existing test failures (unrelated to this PR):

  1. test_vertex_gemma_transformation.py::TestVertexGemmaCompletion::test_acompletion_basic_request
    • Reason: Missing optional dependency vertexai (needs google-cloud-aiplatform>=1.38)
  2. test_azure_assistant_cost_tracking.py::TestAzureAssistantCostTracking::test_constants_loaded_correctly
    • Reason: Missing model cost configuration for azure/container (expects code_interpreter_cost_per_session=0.03)
  3. test_gemini_common_utils.py::TestGoogleAIStudioTokenCounter::test_clean_contents_for_gemini_api_preserves_other_fields
    • Reason: Missing optional dependency google.genai
  4. test_tool_call_cost_tracking.py::test_azure_assistant_features_integrated_cost_tracking
    • Reason: Same root cause as item 2 - missing code interpreter cost calculation (gets $10.00 instead of expected $10.06)

All 4 failures are due to missing optional dependencies or missing model cost data, not related to this Pydantic serialization fix.

Type

🐛 Bug Fix
✅ Test

Changes

This PR fixes Pydantic serialization warnings (issue #11759) by using Field(exclude=True) instead of del self.field in LiteLLM's Message and Choices classes, and removes the global warning filter that was masking the issue.

The Problem: LiteLLM deletes Pydantic model fields after initialization, which breaks Pydantic's field tracking and generates warnings. A global warning filter was added to suppress these warnings, but it affects all code in the process—including user code.

The Fix:

  • Added Field(exclude=True) to fields in litellm/types/utils.py and removed del statements
  • Removed global filter from litellm/llms/meta_llama/chat/transformation.py
  • Added 3 test files demonstrating the issues and verifying the fix

See #11759 (comment) for full context

Three tests that demonstrate the problems caused by the 'del self.field'
anti-pattern in Message and Choices classes:

1. test_pydantic_serialization_warnings.py - Shows that serialization
   generates Pydantic warnings due to deleted fields

2. test_global_filter_pollution.py - Shows that the global warning filter
   added in 08b2b4f suppresses warnings from user code (action-at-a-distance)

3. test_warnings_leak_in_strict_mode.py - Shows that warnings still leak
   into pytest output despite the global filter

All three tests currently FAIL, demonstrating the bugs exist.
They will PASS after applying the fix (Field(exclude=True) instead of del).
Replace the 'del self.field' anti-pattern with Field(exclude=True) in
Message and Choices classes. This is the canonical Pydantic approach for
excluding fields from serialization.

Changes:
- Message class: Use Field(exclude=True) for audio, images, annotations,
  reasoning_content, and thinking_blocks
- Choices class: Use Field(exclude=True) for logprobs and provider_specific_fields
- Remove all 'del self.field' statements from __init__ methods
- Remove global warning filter from transformation.py

This eliminates Pydantic serialization warnings at their source without
global side effects.
@vercel
Copy link

vercel bot commented Nov 6, 2025

@pmorch is attempting to deploy a commit to the CLERKIEAI Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant
Copy link

CLAassistant commented Nov 6, 2025

CLA assistant check
All committers have signed the CLA.

@pmorch
Copy link
Author

pmorch commented Nov 6, 2025

About the github action linting errors:

This PR only modified:

  • litellm/types/utils.py
  • litellm/llms/meta_llama/chat/transformation.py
  • Three test files in tests/test_litellm/

The mypy errors are in completely different files:

  • caching/redis_cache.py
  • caching/redis_cluster_cache.py
  • responses/utils.py
  • litellm_core_utils/litellm_logging.py
  • integrations/s3.py
  • completion_extras/litellm_responses_transformation/transformation.py

These are all pre-existing mypy errors in the codebase, unrelated to these changes.

@pmorch
Copy link
Author

pmorch commented Nov 6, 2025

Note: This PR does not address the exception handler broadness discussed in "Note on Exception Handling" in #11759 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: UserWarning: Pydantic serializer warnings: PydanticSerializationUnexpectedValue(Expected 9 fields but got 6: ...

2 participants