Skip to content

Fix #916: Suppress Pydantic serializer warnings from litellm - #917

Merged
xingyaoww merged 5 commits into
mainfrom
openhands-workspace-73gj32i8
Oct 27, 2025
Merged

xingyaoww merged 5 commits into
mainfrom
openhands-workspace-73gj32i8

Conversation

@xingyaoww

@xingyaoww xingyaoww commented Oct 27, 2025 •

Copy link
Copy Markdown
Contributor

Description

Fixes #916

This PR suppresses Pydantic serializer warnings that 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

  1. Core Fix: Added module-level warning filter in openhands-sdk/openhands/sdk/llm/llm_response.py to suppress Pydantic serialization warnings from litellm

    • Lines 19-21: Filter configured when module is imported
    • Provides single point of control for warning suppression
  2. Test Coverage: Created tests/sdk/llm/test_pydantic_warning_suppression.py with comprehensive tests:

    • Verifies module import doesn't trigger warnings
    • Verifies LLMResponse serialization works without warnings
    • Uses proper type annotations (TextContent, TokenUsage) for type safety
  3. Pre-commit Fix: Updated .pre-commit-config.yaml to fix pyright import resolution:

    • Configured pyright hook to set PYTHONPATH for proper import resolution
    • Fixes import resolution errors for all test files (200+ errors → 0 errors)

Testing

  • ✅ Ran uv run examples/01_standalone_sdk/01_hello_world.py - no more Pydantic warnings
  • ✅ All 344 SDK LLM tests pass
  • ✅ Both new warning suppression tests pass
  • ✅ All pre-commit checks pass (format, lint, pycodestyle, pyright)

Verification

Before fix:

/home/xingyaow/Projects/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/main.py:464: UserWarning: Pydantic serializer warnings:
  PydanticSerializationUnexpectedValue(Expected 10 fields but got 7...)
  PydanticSerializationUnexpectedValue(Expected `StreamingChoices` - serialized value may not be as expected...)

After fix:

No Pydantic warnings appear in the output

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:

  • Applied once at module import time
  • Affects all subsequent uses throughout SDK
  • Cleaner than per-call context managers
  • Single point of control for maintenance

Agent Server images for this PR

• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server

Variants & Base Images

Variant Base Image Docs / Tags
golang golang:1.21-bookworm Link
java eclipse-temurin:17-jdk Link
python nikolaik/python-nodejs:python3.12-nodejs22 Link

Pull (multi-arch manifest)

docker pull ghcr.io/openhands/agent-server:1585de6-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-1585de6-python \
  ghcr.io/openhands/agent-server:1585de6-python

All tags pushed for this build

ghcr.io/openhands/agent-server:1585de6-golang
ghcr.io/openhands/agent-server:v1.0.0a4_golang_tag_1.21-bookworm_binary
ghcr.io/openhands/agent-server:1585de6-java
ghcr.io/openhands/agent-server:v1.0.0a4_eclipse-temurin_tag_17-jdk_binary
ghcr.io/openhands/agent-server:1585de6-python
ghcr.io/openhands/agent-server:v1.0.0a4_nikolaik_s_python-nodejs_tag_python3.12-nodejs22_binary

The 1585de6 tag is a multi-arch manifest (amd64/arm64); your client pulls the right arch automatically.

@github-actions

github-actions Bot commented Oct 27, 2025 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
TOTAL10720470056% 
report-only-changed-files is enabled. No files were changed during this commit :)

@xingyaoww
xingyaoww force-pushed the openhands-workspace-73gj32i8 branch from 4a5567f to 17b04a8 Compare October 27, 2025 15:10
@xingyaoww
xingyaoww marked this pull request as ready for review October 27, 2025 15:24
@xingyaoww
xingyaoww force-pushed the openhands-workspace-73gj32i8 branch 2 times, most recently from 90ac52b to 179d12b Compare October 27, 2025 16:33
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>
@xingyaoww
xingyaoww force-pushed the openhands-workspace-73gj32i8 branch from 179d12b to 01148ab Compare October 27, 2025 16:34
@openhands-ai

openhands-ai Bot commented Oct 27, 2025

Copy link
Copy Markdown

Looks like there are a few issues preventing this PR from being merged!

  • GitHub Actions are failing:
    • Pre-commit checks

If you'd like me to help, just leave a comment, like

@OpenHands please fix the failing actions on PR #917 at branch `openhands-workspace-73gj32i8`

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")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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"?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm on it! enyst can track my progress at all-hands.dev

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

View full conversation

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm on it! simonrosenberg can track my progress at all-hands.dev

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh and mine would be an ad on how not to

Comment thread .pre-commit-config.yaml Resolved
Comment thread .pre-commit-config.yaml Resolved
@xingyaoww
xingyaoww enabled auto-merge (squash) October 27, 2025 18:24
@xingyaoww
xingyaoww requested a review from enyst October 27, 2025 19:19
@xingyaoww
xingyaoww merged commit fc87a83 into main Oct 27, 2025
@xingyaoww
xingyaoww deleted the openhands-workspace-73gj32i8 branch October 27, 2025 20:22
vivekvjnk pushed a commit to vivekvjnk/agent-sdk that referenced this pull request Nov 17, 2025
StressTestor pushed a commit to StressTestor/software-agent-sdk that referenced this pull request Jun 1, 2026
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.

UserWarning: Pydantic serializer warnings

4 participants