Skip to main content
W&B Weave supports logging and displaying many content types, with dedicated functions for videos, images, audio clips, PDFs, CSV data, and HTML. This guide is for developers who want to capture media inputs and outputs in their Weave traces, so they can review and share them alongside the rest of the trace data. It provides basic and advanced examples for logging and displaying each supported media type:

Overview

The examples in this guide use annotations. We recommend annotations because they’re the most direct way to start logging your media. For more advanced configurations, see the Content API section.To log media to Weave, add type annotations like Annotated[bytes, Content] or Annotated[str, Content] as input or return types in your ops. If you annotate path arguments with Annotated[str, Content], Weave automatically opens, detects, and displays the media within your trace.
The following sections provide examples of logging each type of media. Each section starts with a minimal example and then expands into more advanced scenarios.

Log images

The following examples demonstrate how to generate and log images to Weave’s UI.
Weave trace view showing a generated image of a cat wearing a pumpkin hat
Log images by annotating functions with Annotated[bytes, Content] types or filepaths with Annotated[str, Content].The following example draws a basic image and then logs it to Weave using the Content annotation:
Weave logs the image and returns a link to the trace where you can view the image.

Advanced example: Generate an image with DALL-E and log it to Weave

The following example generates a picture of a cat and logs it to Weave:

Advanced example: Resize large images before logging

Resize images before logging them to reduce UI rendering cost and storage impact. Use postprocess_output in your @weave.op to resize an image.
Weave logs the resized image and returns a link to the trace where you can view the image.

Log video

The following examples demonstrate how to generate and log videos to Weave’s UI.
Video logging in Weave
Log videos by annotating functions with Annotated[bytes, Content] types. Weave automatically handles mp4 videos. The following is a minimal example:
Weave logs the video and returns a link to the trace where you can view the video.

Advanced example: Log a video within a video analysis project

The following example shows how to log video within a video-understanding project:
Weave logs both the input video and the model’s analysis, so you can review them together in the trace.

Log documents

The following examples generate and log documents to Weave’s UI.
PDF document logging in Weave
Log documents by annotating functions with Annotated[bytes, Content] types, or by specifying the document type with Annotated[str, Content[Literal['text']].Weave automatically handles pdf, csv, md, text, json, and xml file types. You can also log using file paths with Annotated[str, Content].The following example shows how to store copies of the input PDF and CSV files, and then stores the file contents returned by the function:

Advanced example: Log documents within a RAG system

This example demonstrates how to log documents within a Retrieval-Augmented Generation (RAG) system:
Weave logs both the source PDF and the generated answer, so you can audit the document that produced each response.

Log audio

The following examples demonstrate how to log audio to Weave.
Weave trace view showing a logged audio file with an inline audio player
Log audio to Weave by annotating functions with Annotated[bytes, Content] types, or by specifying the audio type with Annotated[str, Content[Literal['mp3']].Weave automatically handles mp3, wav, flac, ogg, and m4a file types. You can also log using file paths with Annotated[str, Content].The following code snippet generates a sine wave, records it, and then logs the audio to Weave:

Advanced example: Generate and log AI-created audio

This example generates and logs AI-created audio using the Content annotation:
This audio is logged to Weave and automatically displayed in the UI, along with an audio player. In the audio player, you can view and download the raw audio waveform.
Audio logging in Weave
The following example shows how to log audio using streaming response from the OpenAI API:
Try our cookbook for Audio Logging. The cookbook also includes an advanced example of a Real-Time Audio API-based assistant integrated with Weave.

Log HTML

The following examples demonstrate how to generate and log HTML to Weave’s UI.
HTML logging in Weave
Log interactive HTML by annotating functions with Annotated[bytes, Content[Literal['html']]].The following example creates a minimal HTML page and logs it to Weave:

Advanced example: Generate self-contained HTML pages using Serverless Inference and log them to Weave

This example generates self-contained HTML pages using Serverless Inference and logs the pages to Weave:
This HTML is logged to Weave and automatically displayed in the UI. Click the file_name.html cell in the table to open it in full screen. You can also download the raw .html file.

Contents API

The Content API handles media objects in Weave. Use it when you need more control than the previous annotation-based examples provide. For example, when you want to import content from base64 data, custom byte buffers, or text, or when you need to attach custom metadata. It lets you import content into Weave as base64 data, file paths, raw bytes, or text.
The Content API is only available in Python.

Usage

You can use the Content API in two primary ways: type annotations and direct initialization. Type annotations automatically detect the proper constructor to use, while direct initialization provides more fine-grained control and lets you take advantage of the Content API’s runtime features in your code.

Type annotations

The Weave Content API is primarily designed for use through type annotations, which signal to Weave that traced inputs and outputs should be processed and stored as content blobs.

Direct initialization

To take advantage of features such as the following:
  • Open a file with a default application (such as a PDF viewer).
  • Dump the model to JSON to upload to your own blob storage (such as S3).
  • Pass custom metadata to associate with the Content blob (such as the model used to generate it).
Initialize content directly from your target type using one of the following methods:
  • Content.from_path - Create from a file path.
  • Content.from_bytes - Create from raw bytes.
  • Content.from_text - Create from text string.
  • Content.from_base64 - Create from base64-encoded data.

Custom mimetypes

Weave can detect most binary mimetypes, but custom mimetypes and text documents such as markdown might not be automatically detected, requiring you to manually specify the mimetype or extension of your file.

Custom mimetypes with type annotations

Custom mimetypes with direct initialization

Content properties

For a comprehensive list of class attributes and methods, see the Content reference docs.

Attributes

Utility methods

  • save(dest: str | Path) -> None: Save content to a file.
  • open() -> bool: Open file using system default application (requires the content to have been saved or loaded from a path).
  • as_string() -> str: Display the data as a string (bytes are decoded using the encoding attribute).

Initialization methods

Create content object from a file path:
Create content object from raw bytes:
Create content object from text:
Create content object from base64-encoded data:

Custom metadata

You can attach custom metadata to any Content object: