MixtrainDocs
from mixtrain.types import File, Image, Video, Audio, Model3D, Text, Markdown, JSON, Embedding

Media Types

Media types can be used as both inputs and outputs. All inherit from File and support downloading from https://, gs://, and s3:// URLs.

File

Base class for file references.

from mixtrain.types import File

file = File(
    url="gs://bucket/data.bin",
    content_type="application/octet-stream",
    filename="data.bin"
)

# Download to local file
local_path = file.to_file()

# Or specify path
file.to_file("/tmp/myfile.bin")
PropertyTypeDescription
urlstrURL to the file (https://, gs://, s3://)
content_typestr | NoneMIME type
filenamestr | NoneOriginal filename

Methods:

MethodReturnsDescription
to_file(path=None)strDownload to local file. Returns path.

Image

Extends File with image-specific helpers.

from mixtrain.types import Image

image = Image(
    url="https://...",
    width=1024,
    height=768
)

# As input in model
def run(self, image: Image):
    pil = image.to_pil()     # PIL.Image
    arr = image.to_numpy()   # numpy (H, W, C)
PropertyTypeDescription
urlstrURL to the image
widthint | NoneImage width in pixels
heightint | NoneImage height in pixels
formatstr | NoneFormat (png, jpg, webp)

Methods:

MethodReturnsDescription
to_file(path=None)strDownload to local file
to_pil()PIL.Image.ImageOpen as PIL Image
to_numpy()np.ndarrayConvert to numpy array (H, W, C)

Video

Extends File with video-specific helpers.

from mixtrain.types import Video

video = Video(
    url="https://...",
    width=1920,
    height=1080,
    duration_seconds=10.5
)

# As input in model
def run(self, video: Video):
    cap = video.to_cv2()
    for frame in video.to_frames():
        process(frame)
PropertyTypeDescription
urlstrURL to the video
widthint | NoneVideo width in pixels
heightint | NoneVideo height in pixels
duration_secondsfloat | NoneDuration in seconds
formatstr | NoneFormat (mp4, webm)

Methods:

MethodReturnsDescription
to_file(path=None)strDownload to local file
to_cv2()cv2.VideoCaptureOpen as OpenCV VideoCapture
to_frames()Iterator[np.ndarray]Yield frames as numpy arrays (H, W, C)

Audio

Extends File with audio-specific helpers.

from mixtrain.types import Audio

audio = Audio(
    url="https://...",
    duration_seconds=30.0
)

# As input in model
def run(self, audio: Audio):
    samples, sr = audio.to_numpy()
PropertyTypeDescription
urlstrURL to the audio file
duration_secondsfloat | NoneDuration in seconds
formatstr | NoneFormat (mp3, wav)

Methods:

MethodReturnsDescription
to_file(path=None)strDownload to local file
to_numpy()tuple[np.ndarray, int]Returns (samples, sample_rate)

Model3D

from mixtrain.types import Model3D

model3d = Model3D(
    url="https://...",
    format="glb"
)
PropertyTypeDescription
urlstrURL to the 3D model file
formatstrFile format (e.g., "glb", "obj")

Embedding

from mixtrain.types import Embedding

embedding = Embedding(
    vector=[0.1, 0.2, 0.3, ...],
    dimensions=1536
)
PropertyTypeDescription
vectorlist[float]Embedding vector
dimensionsintVector dimensions

Text Types

Text

Plain text output.

from mixtrain.types import Text

text = Text(content="Hello, world!")
PropertyTypeDescription
contentstrText content

Markdown

Markdown-formatted text.

from mixtrain.types import Markdown

md = Markdown(content="# Hello\n\nThis is **bold**.")
PropertyTypeDescription
contentstrMarkdown content

JSON

Structured JSON data.

from mixtrain.types import JSON

data = JSON(data={"key": "value", "count": 42})
PropertyTypeDescription
datadictJSON data

Using Types in Your Models

Return typed outputs from your MixModel:

from mixtrain import MixModel
from mixtrain.types import Image, Video

class ImageGenerator(MixModel):
    def run(self, inputs=None):
        # Generate image
        image_url = self._generate(inputs["prompt"])

        return {
            "image": Image(
                url=image_url,
                width=1024,
                height=1024
            )
        }

class VideoGenerator(MixModel):
    def run(self, inputs=None):
        video_url = self._generate(inputs["prompt"])

        return {
            "video": Video(
                url=video_url,
                width=1920,
                height=1080,
                duration_seconds=5.0
            )
        }

Accessing Types from ModelResult

When you run a model, typed accessors are available on the result:

from mixtrain import Model

model = Model("flux-pro")
result = model.run({"prompt": "A sunset"})

# Typed accessor returns Image or None
if result.image:
    print(result.image.url)
    print(f"{result.image.width}x{result.image.height}")

# For video models
model = Model("hunyuan-video")
result = model.run({"prompt": "A cat playing"})

if result.video:
    print(result.video.url)
    print(f"Duration: {result.video.duration_seconds}s")

On this page