File Operations

The files component of the VLM Run client provides methods for uploading and managing files.

Upload a File

import { VlmRun } from "vlmrun";

const client = new VlmRun({
  apiKey: "your-api-key",
});

// Upload a file using local file path
const file = await client.files.upload({
  filePath: "path/to/document.pdf",
});

// The response includes the file ID and metadata
console.log(file.id); // "file_abc123"
console.log(file.filename); // "document.pdf"
console.log(file.bytes); // 1234567
console.log(file.purpose); // "document"
console.log(file.created_at); // "2024-01-01T00:00:00Z"

Get a File

// Get a file by ID
const file = await client.files.get("file_abc123");

File Types

The SDK supports various file types including:

  • Images (jpg, jpeg, png)
  • Documents (pdf)
  • Audio files (mp3, wav)
  • Video files (mp4)

TypeScript Interfaces

type FilePurpose = string;

interface FileResponse {
  id: string;
  filename: string;
  bytes: number;
  purpose: FilePurpose;
  created_at: string;
  object: "file";
}

Error Handling

try {
  const file = await client.files.upload({
    filePath: "nonexistent.pdf",
  });
} catch (error) {
  if (error instanceof ApiError) {
    console.error("API Error:", error.message);
    // Handle API-specific errors (rate limits, permissions, etc.)
  } else {
    console.error("File system error:", error);
    // Handle local file system errors
  }
}

Best Practices

  1. File Size Limits

    • Check file size before uploading
    • Handle large files appropriately
  2. File Types

    • Verify file types before upload
    • Use appropriate MIME types
  3. Error Handling

    • Implement proper error handling
    • Handle both API and file system errors