Document Predictions

The document component provides methods for processing and analyzing documents using VLM Run’s models.

Process a Document

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

// Process a document using file ID
const response = await client.document.generate({
  fileId: file.id,
  model: "vlm-1",
  domain: "document.invoice",
});
console.log(response);

// Process a document using URL
const documentUrl =
  "https://storage.googleapis.com/vlm-data-public-prod/hub/examples/document.invoice/google_invoice.pdf";
const urlResponse = await client.document.generate({
  url: documentUrl,
  model: "vlm-1",
  domain: "document.invoice",
});
console.log(urlResponse);

Process a document passing zod schema

import { z } from "zod";

const schema = z.object({
  invoice_id: z.string(),
  total: z.number(),
  sub_total: z.number(),
  tax: z.number(),
  items: z.array(
    z.object({
      name: z.string(),
      quantity: z.number(),
      price: z.number(),
      total: z.number(),
    })
  ),
});

const apiResponse = await client.document.generate({
  url: documentUrl,
  domain: "document.invoice",
  config: { responseModel: schema },
});

const response = apiResponse.response as z.infer<typeof schema>;
console.log(response);

Get usage

const usage = response.usage;
console.log(usage);

TypeScript Interfaces

interface FilePredictionParams extends PredictionGenerateParams {
  batch?: boolean;
  fileId?: string;
  url?: string;
}

interface CreditUsage {
  elements_processed?: number;
  element_type?: "image" | "page" | "video" | "audio";
  credits_used?: number;
}

Error Handling

try {
  const response = await client.document.generate({
    url: "invalid-url",
    model: "vlm-1",
  });
} catch (error) {
  if (error instanceof ApiError) {
    console.error("API Error:", error.message);
    // Handle API-specific errors
  } else {
    console.error("File system error:", error);
    // Handle local file system errors
  }
}

Best Practices

  1. Document Formats

    • Supported formats: PDF
    • Ensure proper document quality
    • Consider file size limits
  2. Performance

    • Use URLs for remote documents when possible
    • Handle timeouts appropriately
    • Consider document size and complexity
  3. Error Handling

    • Validate documents before processing
    • Handle both API and file system errors
    • Implement proper error recovery