Repository navigation
fix(py)!: a Message inside your own Pydantic model dumps the same as on its own - #6559
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the serialization of Genkit models to ensure they correctly serialize to their wire shape (camelCase, null-free, and base64-encoded bytes) even when nested inside user-defined Pydantic models. This is achieved by upgrading the Pydantic dependency to >=2.11 and utilizing a custom wrap-mode model serializer (@model_serializer(mode='wrap')) on the base GenkitModel. Additionally, corresponding integration tests for Django, FastAPI, and Flask have been added, and the schema-to-typing generation script has been updated. I have no feedback to provide as there are no review comments.
There was a problem hiding this comment.
After rebasing, GenerationCommonConfig is a GenkitConfig (#6697), so _keep_none_fields and the codegen hunk do nothing, and a typed config like GeminiConfig on resp.request.config nested in a user model still dumps snake_case with nulls. Turning on serialize_by_alias for GenkitConfig would break the plugins that rely on its model_dump() returning field names, so how do you want config to fit the "same as standalone" claim?
Also, can the breaking-change notes also say that an explicit exclude_none=False is now ignored on Genkit types? The exclude_none=False at _model_garden/_plugin.py:176 can go too.
…types-dump Conflict resolutions: - py/packages/genkit/src/genkit/_core/_base.py: GenkitModel.model_config keeps main's validate_by_name/validate_by_alias (replacing populate_by_name) plus this PR's serialize_by_alias=True and ser_json_bytes='base64'. Main's new GenkitConfig class kept as is. Dropped the _keep_none_fields hook: its only user, GenerationCommonConfig, is a GenkitConfig on main (#6714), which the GenkitModel wrap serializer never touches, so it already keeps explicit None. - py/scripts/schema_to_typing.py: took main's GenerationUsage.from_messages emission; dropped this PR's GenerationCommonConfig _keep_none_fields emission for the same reason. - py/packages/genkit/src/genkit/_core/_typing.py: regenerated with ./py/bin/generate_schema_typing; now identical to origin/main. ./py/bin/generate_schema_typing --ci passes. Post-merge fixes: - nested_dump_test.py: model_ref(...) -> ModelRef(name=...) (#6766 removed the model_ref builder). - flask_nested_types_test.py: type: ignore on @app.post, the same Flask T_route async-view error main's flask_test.py already carries.
|
good catch on |
A Genkit type nested inside a user
BaseModelnow dumps as wire JSON (camelCase keys, nulls omitted), matchingMessage.model_dump()at the top level and unifying Flask, Django, and FastAPI flow responses.Breaking Changes
GenkitModel(or aGenkitConfigheld inside one, such asresp.request.config) serialized from a parent userBaseModelnow emits camelCase keys and omitsNonefields instead of snake_case keys withnullvalues. Passby_alias=Falseif Python field names are required.exclude_none=Falseis ignored on Genkit wire types:GenkitModelsubclasses always omitNonefields (exceptPart(data=None)whendatais the part's sole payload, so null data parts survive replay). StandaloneGenkitConfigsubclasses still keepNonefields and Python field names by default for provider SDKs.Decisions
GenkitModelserialization, not justmodel_dump(): Previously,by_alias=Trueandexclude_none=Truewere only applied as defaults inGenkitModel.model_dump(), which Pydantic skips when serializing aGenkitModelnested inside a user'sBaseModel. Addingserialize_by_alias=Trueand a@model_serializer(mode='wrap')onGenkitModelenforces wire output at every nesting depth while preserving OpenAPI schemas for FastAPI.GenkitConfigfollows wire rules only inside aGenkitModel: Turningserialize_by_alias=TrueonGenkitConfigglobally would break plugins that passcfg.model_dump()to provider SDKs expecting snake_case kwargs.GenkitModel._serialize_wiresets aContextVarduring serialization soGenkitConfigemits camelCase without nulls insideModelRequest/ModelResponsewhile keeping snake_case field names when dumped on its own.{"data": null}: Droppingdata: NoneonPart.from_data(None)would serialize an empty{}part that fails replay validation, soPart._drop_none_fieldspreservesdata: Nonewhen no other part kind is set.