Skip to content

Leave computed fields out of serialized pydantic models - #3486

Open
1AbdullahButt wants to merge 2 commits into
flyteorg:masterfrom
1AbdullahButt:pydantic-computed-field-roundtrip
Open

1AbdullahButt wants to merge 2 commits into
flyteorg:masterfrom
1AbdullahButt:pydantic-computed-field-roundtrip

Conversation

@1AbdullahButt

@1AbdullahButt 1AbdullahButt commented Nov 7, 2026 •

Copy link
Copy Markdown

Tracking issue

Closes flyteorg/flyte#8342

Why are the changes needed?

PydanticTransformer serializes a model with model_dump_json(), which includes @computed_field values. to_python_value reads it back with model_validate_json(), and pydantic treats those keys as extra inputs. A model with model_config = ConfigDict(extra="forbid") therefore can't be passed from one task to the next (extra_forbidden before the downstream task runs). With the default extra="ignore" the computed values were silently dropped on the way in anyway.

What changes were proposed in this pull request?

The fix is on the read side, so nothing about what gets written changes:

  • flytekit/extras/pydantic_transformer/transformer.py: a new _strip_computed_fields(tp, data) drops the computed-field keys (name or alias) of the expected model from the deserialized dict before model_validate_json. It follows the model's field annotations into nested models, list/set/Sequence, tuple (fixed and variadic), dict/Mapping, Optional[...], Annotated[...] and RootModel. A union of several types is left as it is, since it isn't known which one the data is for. Keys that are real fields or field aliases are never dropped.
  • It runs in both read paths: the msgpack binary (from_binary_idl) and the old protobuf Struct format (to_python_value).

Compared with excluding computed fields on write (the first version of this PR):

  • Outputs already stored, including cached ones from older versions, load fine, because they are fixed when read.
  • Stored outputs, cache keys and the UI are unchanged, and computed values still appear in them.
  • It needs no exclude_computed_fields, so it works the same on pydantic < 2.12, including nested models.

How was this patch tested?

New tests in tests/flytekit/unit/extras/pydantic_transformer/test_pydantic_basemodel_transformer.py:

  • test_basemodel_with_computed_field_and_extra_forbid (msgpack and FLYTE_USE_OLD_DC_FORMAT=true): an extra="forbid" model with a computed field holds the same kind of model directly, in a List, a Dict and an Optional. It checks that the stored literal still contains the computed values and that it round-trips.
  • test_basemodel_with_computed_field_and_extra_forbid_between_tasks: a workflow passes an extra="forbid" model and a List of them from one task to another.

Run in a python:3.12-slim container:

python -m pytest -q tests/flytekit/unit/extras/pydantic_transformer   # 40 passed, pydantic 2.14.3
python -m pytest -q tests/flytekit/unit/extras/pydantic_transformer   # 40 passed, pydantic 2.10.6

With the transformer from master, the new tests fail with extra_forbidden. ruff check and ruff format --check pass on transformer.py. I did not run the full unit suite.

Setup process

pip install -e . pytest mock pandas pyarrow hypothesis pytest-asyncio

Check all the applicable boxes

  • I updated the documentation accordingly.
  • All new and existing tests passed. (Only the pydantic transformer tests were run, see above.)
  • All commits are signed-off.

Related PRs

flyteorg/flyte-sdk#1807 fixes the same issue in flyte-sdk on the write side; the same read-side approach would carry over there.

This change was written with Claude Code; I reviewed it and ran the tests above.

🤖 Generated with Claude Code

The pydantic transformer serialized models with model_dump_json(),
which includes @computed_field values. Reading the value back with
model_validate_json() then treats them as extra inputs, so a model with
extra="forbid" could not be passed between tasks (extra_forbidden), and
with the default extra="ignore" the computed values were dropped anyway.

Pass exclude_computed_fields=True (pydantic >= 2.12) in both to_literal
and the old generic format path; the receiving side computes the fields
again when it builds the model.

Closes flyteorg/flyte#8342

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: 1AbdullahButt <168034895+1AbdullahButt@users.noreply.github.com>
@AdilFayyaz

Copy link
Copy Markdown
Collaborator

@1AbdullahButt thanks for the clear writeup and tests.

A few concerns with fixing this on the write side:

  • Already-stored outputs still break. A cached task from an older SDK version still returns n_weeks. After upgrading, the user reruns, gets the cached output back, and the next task fails the same way.
  • Cache keys change. Tasks that take these models as inputs get new cache keys, so they re-run once after upgrading. That includes default extra="ignore" models that work fine today.
  • Computed values disappear from outputs. They no longer show up in stored outputs or the UI, even though @computed_field is how users ask for them to be included.
  • Some paths aren't covered. A NamedTuple or TypedDict holding the model still fails (_tuple_dict.py:228), and so do nested models on pydantic < 2.12.

The same discussion is happening on flyteorg/flyte-sdk#1807, so it would be good to land on one approach for both.

Keep writing model_dump_json() as before, so stored outputs, cache keys
and the UI keep the computed values. Before validating, the transformer
now drops the computed-field keys of the expected model, walking nested
models, lists, tuples, dicts, Optional and Annotated fields and
RootModels. That also fixes outputs already stored by older versions, and
works on every supported pydantic version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: 1AbdullahButt <168034895+1AbdullahButt@users.noreply.github.com>
@1AbdullahButt

Copy link
Copy Markdown
Author

@AdilFayyaz thanks, those are all fair. I moved the fix to the read side in 3768cde, so the write path is back to plain model_dump_json():

  • Already-stored outputs: the transformer now drops the expected model's computed-field keys before validating, so outputs written by older versions (cached ones included) load fine.
  • Cache keys: nothing about what's written changes, so cache keys stay the same.
  • Computed values in outputs: they're still stored and shown, and the new test checks the stored literal still has them.
  • Coverage: stripping follows the field annotations into nested models, lists, tuples, dicts, Optional, Annotated and RootModel, and doesn't depend on exclude_computed_fields. So nested models work on pydantic < 2.12 too: the tests pass on 2.10.6 and 2.14.3. I checked flytekit for a _tuple_dict.py and couldn't find one, so I think line 228 is flyte-sdk's (Better error messaging for overrides #1807). Here a List[Model] or Dict[str, Model] input goes through PydanticTransformer per item, and there's a task-to-task test for the list case. A union of several model types is left as it is, since it isn't clear which one the data is for.

If this approach looks right, the same helper could go into flyte-sdk#1807.

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.

Pydantic models with @computed_field and extra="forbid" fail to pass between tasks

2 participants