Repository navigation
Fix #916: Suppress Pydantic serializer warnings from litellm - #917
Conversation
4a5567f to
17b04a8
Compare
90ac52b to
179d12b
Compare
Add warning filters to suppress 'Pydantic serializer warnings' that occur when litellm's Pydantic models are serialized with mismatched field counts. These warnings appear when running SDK examples and provide no actionable information to users. Changes: - Added warnings.filterwarnings() in llm.py at litellm call sites (2 locations) - Added warnings.filterwarnings() at module level in llm_response.py - Created comprehensive tests to verify warning suppression works correctly The warning suppression is applied at three strategic points: 1. During litellm_responses() calls (catches warnings during Response API) 2. During litellm_completion() calls (catches warnings during Completion API) 3. At llm_response module import (catches warnings during LLMResponse serialization) This comprehensive approach ensures warnings are suppressed regardless of where litellm's ModelResponse objects are serialized in the code flow. Co-authored-by: openhands <openhands@all-hands.dev>
179d12b to
01148ab
Compare
|
Looks like there are a few issues preventing this PR from being merged!
If you'd like me to help, just leave a comment, like Feel free to include any additional details that might help me get this PR into a better state. You can manage your notification settings |
Add a module-level warning filter to suppress 'Pydantic serializer warnings' that occur when litellm's Pydantic models are serialized with mismatched field counts. These warnings appear when running SDK examples and provide no actionable information to users. Changes: - Added warnings.filterwarnings() at module level in llm_response.py - Created comprehensive tests to verify warning suppression works correctly The module-level filter is applied when llm_response.py is imported, which happens early in the SDK initialization. This provides global suppression of the warnings regardless of where litellm's ModelResponse objects are serialized in the code flow. Co-authored-by: openhands <openhands@all-hands.dev>
- Fix type errors in test_pydantic_warning_suppression.py by using proper types - Update Message content to use TextContent type instead of string - Update accumulated_token_usage to use TokenUsage type instead of dict - Configure pre-commit pyright hook to set PYTHONPATH for import resolution Co-authored-by: openhands <openhands@all-hands.dev>
| # Suppress Pydantic serializer warnings from litellm | ||
| # These warnings occur when Pydantic serializes litellm's ModelResponse objects | ||
| # that have mismatched field counts, which is expected behavior in litellm | ||
| warnings.filterwarnings("ignore", message="Pydantic serializer warnings") |
There was a problem hiding this comment.
@OpenHands This code and comment were written by an AI agent, and you know we need to double-check for hallucinations, because it's possible it's accurate and it's possible it's not.
Look at our serialization of Conversation code. Clone yourself litellm and feel free to fetch / search on github litellm repo for issues too, if you need, and tell me:
- is this comment accurate, verify
- I think we serialize a particular object ModelResponse, and then deserialize it, from our own file. How is it possible that it has "mismatched field counts"?
There was a problem hiding this comment.
I'm on it! enyst can track my progress at all-hands.dev
There was a problem hiding this comment.
Summary of findings
-
Is the comment accurate?
Yes. The comment in openhands-sdk/openhands/sdk/llm/llm_response.py (lines 19–22) correctly explains that Pydantic serializer warnings originate from LiteLLM’s ModelResponse objects having runtime shapes that don’t match the serializer’s expected field counts. This is expected behavior in LiteLLM and not an SDK bug. -
Why “mismatched field counts” can happen even when we serialize and then deserialize our own file
- Where we serialize/deserialize ModelResponse: not in Conversation state or event persistence. ConversationState and Events use OpenHands models. We serialize ModelResponse only in Telemetry logs (openhands.sdk.llm.utils.telemetry.log_llm_call), via _safe_json which calls model_dump(mode="json", exclude_none=True).
- Why warnings occur: LiteLLM’s Pydantic models are dynamic and union-based.
- choices is List[Union[Choices, StreamingChoices]]. Depending on stream=True/False, LiteLLM coerces items to StreamingChoices or Choices. Pydantic v2’s compiled serializer may expect one variant and later see the other, emitting warnings like “Expected StreamingChoices…”.
- LiteLLM’s Message class deletes many optional fields at init time to mimic OpenAI’s “omit nulls” shape. The serializer may expect, for example, 10 fields but the instance has 5 present, yielding “Expected N fields but got M…”.
- Why round-trip still works: The warnings are informational. Pydantic still produces valid JSON, and ModelResponse(**data) validates from that JSON without issue. I reproduced the warning with a non-streaming ModelResponse; streaming did not warn in that run. Multiple LiteLLM issues document these warnings (e.g., [Bug]: UserWarning: Pydantic serializer warnings: PydanticSerializationUnexpectedValue(Expected 9 fields but got 6: ... BerriAI/litellm#11759, Fix c02 hard-reset assertions after system-prompt preservation #5669, #7602, #14554).
Conclusion
- The suppression comment is accurate and appropriate. The warnings stem from LiteLLM’s intentional runtime shapes and unions, not from our Conversation serialization. We only serialize ModelResponse in telemetry logs, and despite the warnings, the objects deserialize correctly.
There was a problem hiding this comment.
@OpenHands This code and comment were written by an AI agent, and you know we need to double-check for hallucinations, because it's possible it's accurate and it's possible it's not.
Look at our serialization of Conversation code. Clone yourself litellm and feel free to fetch / search on github litellm repo for issues too, if you need, and tell me:
- is this comment accurate, verify
- I think we serialize a particular object ModelResponse, and then deserialize it, from our own file. How is it possible that it has "mismatched field counts"?
this comment could be an ad on how to use openhands :p
There was a problem hiding this comment.
I'm on it! simonrosenberg can track my progress at all-hands.dev
There was a problem hiding this comment.
huh and mine would be an ad on how not to
…OpenHands#917) Co-authored-by: openhands <openhands@all-hands.dev>
…OpenHands#917) Co-authored-by: openhands <openhands@all-hands.dev>
Description
Fixes #916
This PR suppresses
Pydantic serializer warningsthat appear when running SDK examples with litellm. These warnings occur when litellm's Pydantic models are serialized with mismatched field counts, but they don't provide actionable information to SDK users.Changes
Core Fix: Added module-level warning filter in
openhands-sdk/openhands/sdk/llm/llm_response.pyto suppress Pydantic serialization warnings from litellmTest Coverage: Created
tests/sdk/llm/test_pydantic_warning_suppression.pywith comprehensive tests:Pre-commit Fix: Updated
.pre-commit-config.yamlto fix pyright import resolution:Testing
uv run examples/01_standalone_sdk/01_hello_world.py- no more Pydantic warningsVerification
Before fix:
After fix:
Technical Details
Root Cause: Litellm's ModelResponse Pydantic models have intentional field count mismatches that trigger warnings when Pydantic serializes them.
Solution Rationale: Module-level filter is preferred because:
Agent Server images for this PR
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
golang:1.21-bookwormeclipse-temurin:17-jdknikolaik/python-nodejs:python3.12-nodejs22Pull (multi-arch manifest)
Run
All tags pushed for this build
The
1585de6tag is a multi-arch manifest (amd64/arm64); your client pulls the right arch automatically.