Files and Types
DataChain's type system is built on Pydantic. Every chain carries a schema, every column has a type, and custom models integrate automatically.
File Abstraction
File is the bridge between object storage and the data layer. Every chain begins with files: read_storage() produces a chain of File objects. File carries storage coordinates and provides methods to read content.
Storage Coordinates
File tracks everything needed to locate and identify a blob: source, path, version, etag, size, is_latest, and last_modified. This metadata is indexed by the Dataset DB and available for chain operations without touching the actual bytes.
Content Access
File inherits from DataModel (a Pydantic BaseModel subclass), so it participates in schemas, gets stored as columns, and flows through all chain operations.
Modality Types
File has specialized subclasses for each data modality. Each adds domain-specific methods while inheriting all of File's capabilities.
ImageFile: read() returns a PIL.Image.Image. get_info() returns Image metadata (width, height, format). save() supports format conversion.
VideoFile: get_frame(frame, video_stream_index=0) returns one frame reference with a timestamp estimated from FPS metadata. Pixel access may seek to a nearby keyframe and decode forward. get_frames(start, end, step, video_stream_index=0) yields VideoFrame objects with frame indexes, video stream indexes, and decoded frame timestamps when available, and is preferred for sequential frame reads. get_fragments(duration, start, end) yields VideoFragment time slices. get_info(video_stream_index=0) returns Video metadata (fps, duration, codec, resolution).
AudioFile: get_fragments(duration, start, end) yields AudioFragment chunks. get_info() returns Audio metadata (sample_rate, channels, duration, codec). save() supports format conversion.
Sub-File Units
Videos and audio tracks can be sliced into smaller units that are themselves DataModels:
Annotation Types
Built-in DataModels for annotation primitives: BBox, OBBox, Pose, Pose3D, Segment.
Annotation types compose naturally into Pydantic models:
DataModel and Custom Types
Use pydantic.BaseModel directly for custom types; DataChain accepts it natively:
For external Pydantic models (like Mistral's ChatCompletionResponse), register them explicitly: dc.DataModel.register(MistralResponse).
Union and Optional Fields
A field or a signal can hold one of several types. Each type gets its own columns,
plus a hidden discriminator naming the type a row carries, so values keep their type
through saving and reading datasets. Model | None (the same as
Optional[Model]) is the single-model case of the same mechanism. Optional scalar
fields, such as str | None, use a nullable column.
A union is also a signal type on its own, and a UDF can return one:
To reference one of the types, name it in the path; the name can be dropped when a single type has that field. Functions read the discriminator rather than a column:
select(), distinct() and group_by() carry the whole union, so a value is never
separated from the type it belongs to. A union cannot be a key for diff();
subtract() supports union keys when both sides have matching column layouts.
Inside a list or a dict there are no per-element columns — the collection is one
JSON value — so types that JSON stores alike are rejected when the chain is created:
models whose field names are the same or a subset of one another, datetime | str,
and a union as a dict key. Give such models a kind: Literal["..."] field to tell
them apart. A collection as one of the types (str | list[str]) is not supported.
Core Classes
- DataChain: the core class for composing queries with 60+ methods
- DataModel: Pydantic base for structured types
- Column (aliased as
C): references a column by name for vectorized expressions - File, ImageFile, VideoFile, AudioFile, TextFile: storage-native file types
- BBox, OBBox, Pose, Pose3D, Segment: annotation types (in
datachain.model)