Datachain

Data Types

Data types supported by DataChain must be of type DataType. DataType includes most Python types supported in Pydantic fields, as well as any class that inherits from Pydantic BaseModel.

Pydantic models can be used to group and nest multiple fields together into a single type object. Any Pydantic model must be registered so that the chain knows the expected schema of the model. Alternatively, models may inherit from DataModel, which is a lightweight wrapper around Pydantic's BaseModel that automatically handles registering the model.

DataModel

Bases: BaseModel

Pydantic model wrapper that registers model with DataChain.

To leave fields out of show() by default, list them in _hidden_fields. show(include_hidden=True) shows them, and they stay in the data and in to_pandas().

Example
from typing import ClassVar

import datachain as dc

class Detection(dc.DataModel):
    _hidden_fields: ClassVar[list[str]] = ["raw"]
    label: str
    raw: str

dc.read_values(det=[Detection(label="cat", raw="xyz")]).show()  # no `raw`

register staticmethod

register(models: DataType | Sequence[DataType])

For registering classes manually. It accepts a single class or a sequence of classes.

Source code in datachain/lib/data_model.py
93
 94
 95
 96
 97
 98
 99
100
@staticmethod
def register(models: DataType | Sequence[DataType]):
    """For registering classes manually. It accepts a single class or a sequence of
    classes."""
    if not isinstance(models, Sequence):
        models = [models]
    for val in models:
        ModelStore.register(val)

DataType module-attribute

DataType = type[BaseModel] | StandardType

is_chain_type

is_chain_type(t: type) -> bool

Return true if type is supported by DataChain.

Source code in datachain/lib/data_model.py
430
431
432
433
434
435
436
437
438
439
def is_chain_type(t: type) -> bool:
    """Return true if type is supported by `DataChain`."""
    if ModelStore.is_pydantic(t):
        return True
    if any(t is ft or t is get_args(ft)[0] for ft in get_args(StandardType)):
        return True
    inner, is_optional = unwrap_optional(t)
    if is_optional:
        return is_chain_type(inner)
    return _is_chain_container_type(t)