Official SDKs

Python and Node.js SDKs for FineTune Lab API

Installation

Python

Install from PyPI:

# API client only (lightweight)
pip install finetune-lab

# With training dependencies (requires GPU)
pip install finetune-lab[training]

View on PyPI

Node.js / TypeScript

Install from npm:

npm install @finetunelab/sdk
# or
yarn add @finetunelab/sdk
# or
pnpm add @finetunelab/sdk

View on npm

Quick Start

Python

from finetune_lab import FinetuneLabClient

# Initialize with API key
client = FinetuneLabClient(api_key="wak_your_api_key_here")
# Or set FINETUNE_LAB_API_KEY environment variable

# Run inference
response = client.predict(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}]
)
print(response.content)

# Run batch test
test = client.batch_test.run(
    model_id="your-model-id",
    test_suite_id="your-test-suite-id"
)
print(f"Test started: {test.test_id}")

# Check batch test status
status = client.batch_test.status(test.test_id)
print(f"Progress: {status.completed}/{status.total_prompts}")

Node.js / TypeScript

import { FinetuneLabClient } from '@finetunelab/sdk';

// Initialize with API key
const client = new FinetuneLabClient({
  apiKey: 'wak_your_api_key_here',
  // Or set FINETUNE_LAB_API_KEY environment variable
});

// Run inference
const response = await client.predict({
  model: 'gpt-4',
  messages: [{ role: 'user', content: 'Hello!' }],
});
console.log(response.choices[0].message.content);

// Run batch test
const test = await client.batchTest.run({
  modelId: 'your-model-id',
  testSuiteId: 'your-test-suite-id',
});
console.log(`Test started: ${test.testId}`);

// Check batch test status
const status = await client.batchTest.status(test.testId);
console.log(`Progress: ${status.completedPrompts}/${status.totalPrompts}`);

API Key Scopes

Your API key must have the appropriate scope for each operation:

OperationRequired Scope
predict()production or all
batch_test.*testing or all
analytics.*production or all
training.*training or all

Batch Testing

Python

# Run a batch test
test = client.batch_test.run(
    model_id="your-model-id",
    test_suite_id="suite_abc123",  # From your test suites
    prompt_limit=50,               # Max prompts to test
    concurrency=5,                 # Parallel requests
)

# Poll for completion
import time
while True:
    status = client.batch_test.status(test.test_id)
    print(f"Status: {status.status} - {status.completed}/{status.total_prompts}")
    if status.status in ["completed", "failed", "cancelled"]:
        break
    time.sleep(5)

# Cancel if needed
client.batch_test.cancel(test.test_id)

Node.js

// Run a batch test
const test = await client.batchTest.run({
  modelId: 'your-model-id',
  testSuiteId: 'suite_abc123',
  promptLimit: 50,
  concurrency: 5,
});

// Poll for completion
while (true) {
  const status = await client.batchTest.status(test.testId);
  console.log(`Status: ${status.status} - ${status.completedPrompts}/${status.totalPrompts}`);
  if (['completed', 'failed', 'cancelled'].includes(status.status)) break;
  await new Promise(r => setTimeout(r, 5000));
}

// Cancel if needed
await client.batchTest.cancel(test.testId);

Analytics

Python

# Get traces
traces = client.analytics.traces(limit=50, offset=0)
for trace in traces["traces"]:
    print(f"{trace['span_name']}: {trace['duration_ms']}ms")

# Get aggregated data
data = client.analytics.data(
    start_date="2025-01-01",
    end_date="2025-01-31",
    granularity="day"
)
print(f"Total requests: {data['summary']['totalRequests']}")

Node.js

// Get traces
const traces = await client.analytics.traces({ limit: 50 });
for (const trace of traces.traces) {
  console.log(`${trace.spanName}: ${trace.durationMs}ms`);
}

// Get aggregated data
const data = await client.analytics.data({
  startDate: '2025-01-01',
  endDate: '2025-01-31',
  granularity: 'day',
});
console.log(`Total requests: ${data.summary.totalRequests}`);

Error Handling

Python

from finetune_lab import FinetuneLabClient, FinetuneLabError

try:
    response = client.predict(model="invalid", messages=[])
except FinetuneLabError as e:
    print(f"Error {e.status_code}: {e.message}")
    # Access full response if available
    print(e.response)

Node.js

import { FinetuneLabClient, FinetuneLabError } from '@finetunelab/sdk';

try {
  await client.predict({ model: 'invalid', messages: [] });
} catch (error) {
  if (error instanceof FinetuneLabError) {
    console.error(`Error ${error.statusCode}: ${error.message}`);
    console.error('Type:', error.type);
  }
}

Environment Variables

Both SDKs support configuration via environment variables:

VariableDescriptionDefault
FINETUNE_LAB_API_KEYYour API keyRequired
FINETUNE_LAB_API_URLCustom API base URLhttps://app.finetunelab.com

Resources