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.

Constructors

  • Parameters

    • params: {
          base64DataUri?: string;
          contentBytes?: Buffer;
          contentType?:
              | "application/xml"
              | "image/svg+xml"
              | "text/html"
              | "image/png"
              | "image/jpeg"
              | "image/jpg"
              | "image/webp"
              | "image/gif"
              | "image/tiff"
              | "image/bmp"
              | "audio/mpeg"
              | "audio/mp3"
              | "audio/wav"
              | "audio/ogg"
              | "audio/oga"
              | "audio/aac"
              | "audio/mp4"
              | "audio/flac"
              | "video/mp4"
              | "video/webm"
              | "text/plain"
              | "text/css"
              | "text/csv"
              | "application/pdf"
              | "application/msword"
              | "application/vnd.ms-excel"
              | "application/zip"
              | "application/json"
              | "application/octet-stream";
          filePath?: string;
          obj?: object;
      }
      • Optionalbase64DataUri?: string
      • OptionalcontentBytes?: Buffer
      • OptionalcontentType?:
            | "application/xml"
            | "image/svg+xml"
            | "text/html"
            | "image/png"
            | "image/jpeg"
            | "image/jpg"
            | "image/webp"
            | "image/gif"
            | "image/tiff"
            | "image/bmp"
            | "audio/mpeg"
            | "audio/mp3"
            | "audio/wav"
            | "audio/ogg"
            | "audio/oga"
            | "audio/aac"
            | "audio/mp4"
            | "audio/flac"
            | "video/mp4"
            | "video/webm"
            | "text/plain"
            | "text/css"
            | "text/csv"
            | "application/pdf"
            | "application/msword"
            | "application/vnd.ms-excel"
            | "application/zip"
            | "application/json"
            | "application/octet-stream"
      • OptionalfilePath?: string
      • Optionalobj?: object

    Returns LangfuseMedia

Properties

_contentBytes?: Buffer
_contentType?:
    | "application/xml"
    | "image/svg+xml"
    | "text/html"
    | "image/png"
    | "image/jpeg"
    | "image/jpg"
    | "image/webp"
    | "image/gif"
    | "image/tiff"
    | "image/bmp"
    | "audio/mpeg"
    | "audio/mp3"
    | "audio/wav"
    | "audio/ogg"
    | "audio/oga"
    | "audio/aac"
    | "audio/mp4"
    | "audio/flac"
    | "video/mp4"
    | "video/webm"
    | "text/plain"
    | "text/css"
    | "text/csv"
    | "application/pdf"
    | "application/msword"
    | "application/vnd.ms-excel"
    | "application/zip"
    | "application/json"
    | "application/octet-stream"
_mediaId?: string
_source?: string
obj?: object

Accessors

  • get contentLength(): undefined | number
  • Returns undefined | number

  • get contentSha256Hash(): undefined | string
  • Returns undefined | string

Methods

  • Returns undefined | string

  • Parses a media reference string into a ParsedMediaReference.

    Example reference string: "@@@langfuseMedia:type=image/jpeg|id=some-uuid|source=base64DataUri@@@"

    Parameters

    • referenceString: string

      The reference string to parse.

    Returns ParsedMediaReference

    An object with the mediaId, source, and contentType.

    Error if the reference string is invalid or missing required fields.

  • Replaces the media reference strings in an object with base64 data URIs for the media content.

    This method recursively traverses an object (up to a maximum depth of 10) looking for media reference strings in the format "@@@langfuseMedia:...@@@". When found, it fetches the actual media content using the provided Langfuse client and replaces the reference string with a base64 data URI.

    If fetching media content fails for a reference string, a warning is logged and the reference string is left unchanged.

    Type Parameters

    • T

    Parameters

    • params: LangfuseMediaResolveMediaReferencesParams<T>

      Configuration object

    Returns Promise<T>

    A deep copy of the input object with all media references replaced with base64 data URIs where possible

    const obj = {
    image: "@@@langfuseMedia:type=image/jpeg|id=123|source=bytes@@@",
    nested: {
    pdf: "@@@langfuseMedia:type=application/pdf|id=456|source=bytes@@@"
    }
    };

    const result = await LangfuseMedia.resolveMediaReferences({
    obj,
    langfuseClient
    });

    // Result:
    // {
    // image: "data:image/jpeg;base64,/9j/4AAQSkZJRg...",
    // nested: {
    // pdf: "data:application/pdf;base64,JVBERi0xLjcK..."
    // }
    // }