Skip to content

Conversation

gverkes
Copy link

@gverkes gverkes commented Oct 13, 2025

Fixes #6669

Tracking issue

Closes flyteorg/flyte#6669

Why are the changes needed?

When deserializing Pydantic models containing FlyteFile or FlyteDirectory fields using model_validate(), the deserialized objects were
missing private attributes (_remote_source, _downloader, etc.). This caused an AttributeError when attempting to re-serialize these
objects with model_dump(), breaking the serialize → deserialize → serialize cycle.

This is a critical bug that prevents users from using FlyteFile/FlyteDirectory within Pydantic BaseModel classes in a normal way.

What changes were proposed in this pull request?

Added Pydantic model validators to both FlyteFile and FlyteDirectory classes that ensure private attributes are properly initialized during
deserialization:

  1. FlyteFile (flytekit/types/file/file.py):

    • Enhanced deserialize_flyte_file validator to check if private attributes exist
    • If missing, reconstructs the FlyteFile using dict_to_flyte_file() transformer
    • If attributes already exist (e.g., when passing already-constructed FlyteFile), returns as-is
  2. FlyteDirectory (flytekit/types/directory/types.py):

    • Applied same fix to deserialize_flyte_dir validator
    • Uses dict_to_flyte_directory() to properly reconstruct the object

How was this patch tested?

Added two new unit tests in test_pydantic_basemodel_transformer.py:

  1. test_flytefile_pydantic_model_dump_validate_cycle - Verifies FlyteFile can be serialized, deserialized, and re-serialized without errors
  2. test_flytedirectory_pydantic_model_dump_validate_cycle - Same for FlyteDirectory

Check all the applicable boxes

  • I updated the documentation accordingly.
  • All new and existing tests passed.
  • All commits are signed-off.

Summary by Bito

This pull request fixes a critical bug in deserializing Pydantic models with FlyteFile and FlyteDirectory fields by adding model validators. These validators ensure proper initialization of private attributes, preventing AttributeErrors during re-serialization. New unit tests have also been included to confirm the functionality of these enhancements.

Copy link

welcome bot commented Oct 13, 2025

Thank you for opening this pull request! 🙌

These tips will help get your PR across the finish line:

  • Most of the repos have a PR template; if not, fill it out to the best of your knowledge.
  • Sign off your commits (Reference: DCO Guide).

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.

[BUG] Deserializing Pydantic models with FlyteFile fields creates FlyteFile instances that can't be serialized again.

1 participant