Skip to content

fix(py)!: a Message inside your own Pydantic model dumps the same as on its own - #6559

Merged
huangjeff5 merged 9 commits into
mainfrom
fix/py-nested-genkit-types-dump
Oct 31, 2026
Merged

huangjeff5 merged 9 commits into
mainfrom
fix/py-nested-genkit-types-dump

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

A Genkit type nested inside a user BaseModel now dumps as wire JSON (camelCase keys, nulls omitted), matching Message.model_dump() at the top level and unifying Flask, Django, and FastAPI flow responses.

class Turn(BaseModel):
    reply: Message

# --- BEFORE (Flask / Django flow returning Turn) ---
# {"reply": {"role": "model", "content": [{"text": "hi", "media": null, "tool_request": null, ...}], "metadata": null}}

# --- AFTER ---
# {"reply": {"role": "model", "content": [{"text": "hi"}]}}

Breaking Changes

  • Nested Genkit types emit wire JSON: A GenkitModel (or a GenkitConfig held inside one, such as resp.request.config) serialized from a parent user BaseModel now emits camelCase keys and omits None fields instead of snake_case keys with null values. Pass by_alias=False if Python field names are required.
  • exclude_none=False is ignored on Genkit wire types: GenkitModel subclasses always omit None fields (except Part(data=None) when data is the part's sole payload, so null data parts survive replay). Standalone GenkitConfig subclasses still keep None fields and Python field names by default for provider SDKs.

Decisions

  • Wire rules live on GenkitModel serialization, not just model_dump(): Previously, by_alias=True and exclude_none=True were only applied as defaults in GenkitModel.model_dump(), which Pydantic skips when serializing a GenkitModel nested inside a user's BaseModel. Adding serialize_by_alias=True and a @model_serializer(mode='wrap') on GenkitModel enforces wire output at every nesting depth while preserving OpenAPI schemas for FastAPI.
  • GenkitConfig follows wire rules only inside a GenkitModel: Turning serialize_by_alias=True on GenkitConfig globally would break plugins that pass cfg.model_dump() to provider SDKs expecting snake_case kwargs. GenkitModel._serialize_wire sets a ContextVar during serialization so GenkitConfig emits camelCase without nulls inside ModelRequest / ModelResponse while keeping snake_case field names when dumped on its own.
  • Null-only data parts stay {"data": null}: Dropping data: None on Part.from_data(None) would serialize an empty {} part that fails replay validation, so Part._drop_none_fields preserves data: None when no other part kind is set.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@huangjeff5
huangjeff5 marked this pull request as ready for review October 12, 2026 23:21

@hilariie hilariie left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.
@huangjeff5

Copy link
Copy Markdown
Contributor Author

good catch on GenkitConfig after #6697. 120b55ccc sets a ContextVar around GenkitModel._serialize_wire and adds a wrap serializer on GenkitConfig that serializes with by_alias=True (respecting explicit by_alias=False) and exclude_none=True when inside a GenkitModel dump, while standalone cfg.model_dump() stays snake_case with nulls for plugins. also dropped the dead exclude_none=False in _model_garden/_plugin.py and noted exclude_none=False in the breaking-change bullets.

@huangjeff5
huangjeff5 merged commit effdb14 into main Oct 31, 2026
25 checks passed
@huangjeff5
huangjeff5 deleted the fix/py-nested-genkit-types-dump branch October 31, 2026 23:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants