Langfuse JS/TS SDKs
    Preparing search index...

    Class LangfuseMedia

    A class for wrapping media objects for upload to Langfuse.

    This class handles the preparation and formatting of media content for Langfuse, supporting both base64 data URIs and raw content bytes. It automatically:

    • Parses base64 data URIs to extract content type and bytes
    • Generates SHA-256 hashes for content integrity
    • Creates unique media IDs based on content hash
    • Formats media references for embedding in traces
    // From base64 data URI
    const media1 = new LangfuseMedia({
    source: "base64_data_uri",
    base64DataUri: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=="
    });

    // From raw bytes
    const media2 = new LangfuseMedia({
    source: "bytes",
    contentBytes: new Uint8Array([72, 101, 108, 108, 111])
    contentType: "text/plain"
    });

    console.log(media1.id); // Unique media ID
    console.log(media1.tag); // Media reference tag
    Index

    Constructors

    • Creates a new LangfuseMedia instance.

      Parameters

      Returns LangfuseMedia

      // Create from base64 data URI
      const media = new LangfuseMedia({
      source: "base64_data_uri",
      base64DataUri: "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ..."
      });

    Properties

    _contentBytes?: Uint8Array<ArrayBufferLike>
    _contentType?: MediaContentType
    _source?: string

    Accessors

    • get base64DataUri(): null | string

      Gets the media content as a base64 data URI.

      Returns null | string

      The complete data URI string, or null if no content is available

      const media = new LangfuseMedia({...});
      console.log(media.base64DataUri);
      // "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB..."
    • get contentLength(): undefined | number

      Gets the length of the media content in bytes.

      Returns undefined | number

      The content length in bytes, or undefined if no content is available

    Methods

    • Gets a unique identifier for this media based on its content hash.

      The ID is derived from the first 22 characters of the URL-safe base64-encoded SHA-256 hash of the content.

      Returns Promise<null | string>

      The unique media ID, or null if hash generation failed

      const media = new LangfuseMedia({...});
      console.log(media.id); // "A1B2C3D4E5F6G7H8I9J0K1"
    • Gets the SHA-256 hash of the media content.

      The hash is used for content integrity verification and generating unique media IDs. Returns undefined if crypto is not available or hash generation fails.

      Returns Promise<undefined | string>

      The base64-encoded SHA-256 hash, or undefined if unavailable

    • Gets the media reference tag for embedding in trace data.

      The tag format is: @@@langfuseMedia:type=<contentType>|id=<mediaId>|source=<source>@@@ This tag can be embedded in trace attributes and will be replaced with actual media content when the trace is viewed in Langfuse.

      Returns Promise<null | string>

      The media reference tag, or null if required data is missing

      const media = new LangfuseMedia({...});
      console.log(media.tag);
      // "@@@langfuseMedia:type=image/png|id=A1B2C3D4E5F6G7H8I9J0K1|source=base64_data_uri@@@"
    • Serializes the media to JSON (returns the base64 data URI).

      Returns null | string

      The base64 data URI, or null if no content is available