fix: handle FastAPI serialization for Message objects#800
Closed
OiPunk wants to merge 1 commit intoQwenLM:mainfrom
Closed
fix: handle FastAPI serialization for Message objects#800OiPunk wants to merge 1 commit intoQwenLM:mainfrom
OiPunk wants to merge 1 commit intoQwenLM:mainfrom
Conversation
…objects The overridden `model_dump` method in `BaseModelCompatibleDict` only works when called directly. FastAPI uses Pydantic's `TypeAdapter` and `jsonable_encoder` for response serialization, both of which bypass the custom `model_dump` override and pass `exclude_none=False` explicitly. This causes None-valued fields (e.g. `name`, `function_call`, `extra`) to appear as `null` in API responses, conflicting with the intended behaviour of always excluding None values. Add a `@model_serializer(mode='wrap')` decorator that filters out None values at the Pydantic serialization level, ensuring consistent behaviour regardless of the call path (direct `model_dump()`, `TypeAdapter`, or FastAPI's `jsonable_encoder`). Closes QwenLM#347
Author
|
Closing — shifting focus to AI Agent core projects. Thank you for your time! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
BaseModelCompatibleDict.model_dump()override was being bypassed by FastAPI's serialization pipeline@model_serializer(mode='wrap')toBaseModelCompatibleDictthat filters outNonevalues at the Pydantic serialization levelexclude_none=Truebehavior whether serialized via directmodel_dump(), Pydantic'sTypeAdapter, or FastAPI'sjsonable_encoderProblem
The overridden
model_dumpmethod only setsexclude_none=Truewhen the caller doesn't pass the parameter. However:jsonable_encodercallsmodel_dump(exclude_none=False)explicitly, overriding the intended defaultTypeAdapter(used by FastAPI forresponse_modelvalidation) bypassesmodel_dump()entirelyThis causes
None-valued fields (name,function_call,extra, etc.) to appear asnullin API responses.Solution
Add a
@model_serializer(mode='wrap')that operates at the Pydantic core serialization level, ensuringNonevalues are filtered regardless of the call path. The existingmodel_dump/model_dump_jsonoverrides are preserved for backward compatibility.Test plan
Message.model_dump()excludesNonefieldsMessage.model_dump_json()excludesNonefieldsContentItem.get_type_and_value()still works (depends on single-item dict)TypeAdapter(Message).dump_python()excludesNonefieldsTypeAdapter(Message).dump_json()contains nonullvaluesnullfields__getitem__,__setitem__,get) still workstests/llm/test_schema.py