Skip to content

basetenlabs/truss-go

Repository files navigation

truss-go

Beta: This SDK is in beta. APIs may change between versions.

A Go SDK for deploying models to Baseten using Truss.

Installation

go get github.com/basetenlabs/truss-go

Quick Start

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/basetenlabs/truss-go"
)

func main() {
    // Create client (reads BASETEN_API_KEY from environment)
    client := truss.NewClient()

    // Push a truss
    result, err := client.Push(context.Background(), "./my-truss", "my-model")
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Deployed! URL: %s\n", result.PredictURL())
}

Authentication

The SDK supports three authentication methods:

// 1. Environment variable (recommended)
// Set BASETEN_API_KEY in your environment
client := truss.NewClient()

// 2. Explicit API key
client := truss.NewClient(truss.WithAPIKey("your-api-key"))

// 3. From ~/.trussrc config file
client, err := truss.NewClientFromConfig("baseten")

Config File Format

The ~/.trussrc file uses INI format:

[baseten]
remote_provider = baseten
api_key = your-api-key
remote_url = https://app.baseten.co

Push Options

result, err := client.Push(ctx, "./my-truss", "my-model",
    // Create a production deployment (default is development)
    truss.Publish(),

    // Promote to production immediately
    truss.Promote(),

    // Deploy to a specific environment
    truss.WithEnvironment("production"),

    // Set a custom deployment name
    truss.WithDeploymentName("v2.0-release"),

    // Assign to a team
    truss.WithTeam("ml-team"),

    // Include git commit info in deployment metadata
    truss.WithGitInfo(),

    // Keep previous production deployment's autoscaling
    truss.PreservePreviousProduction(),

    // Use existing environment's instance type
    truss.PreserveEnvInstanceType(),

    // Set backend deployment timeout
    truss.WithDeployTimeout(30 * time.Minute),

    // Allow downloading truss from Baseten UI
    truss.AllowTrussDownload(),
)

Working with Results

result, err := client.Push(ctx, trussDir, modelName)
if err != nil {
    log.Fatal(err)
}

// Get deployment info
fmt.Println("Model ID:", result.ModelID)
fmt.Println("Version ID:", result.VersionID)
fmt.Println("Predict URL:", result.PredictURL())
fmt.Println("Logs URL:", result.LogsURL(client.BaseURL()))
fmt.Println("Is Draft:", result.IsDraft)

// Wait for deployment to be ready
err = result.WaitReady(ctx, 10*time.Minute)
if err != nil {
    log.Fatal(err)
}

// Or check status manually
status, err := result.Status(ctx)
// status is one of: StatusBuilding, StatusDeploying, StatusActive, StatusFailed

Other Operations

// Get current user info
user, err := client.WhoAmI(ctx)
fmt.Println(user.Email, user.WorkspaceName)

// List all models
models, err := client.Models(ctx)

// List models for a specific team
models, err := client.Models(ctx, truss.WithTeamFilter("ml-team"))

// List teams
teams, err := client.Teams(ctx)

// Validate a truss config without deploying
result, err := client.Validate(ctx, "./my-truss")
if !result.Success {
    fmt.Println("Validation errors:", result.Messages)
}

Error Handling

The SDK provides typed errors for different failure modes:

result, err := client.Push(ctx, trussDir, modelName)
if err != nil {
    switch e := err.(type) {
    case *truss.AuthError:
        // Invalid or missing API key
        log.Fatal("Authentication failed:", e)

    case *truss.ValidationError:
        // Truss config validation failed
        fmt.Println("Errors:", e.Errors)
        fmt.Println("Warnings:", e.Warnings)

    case *truss.APIError:
        // Error response from Baseten API
        fmt.Printf("API error (code: %s): %s\n", e.Code, e.Message)

    case *truss.UploadError:
        // Failed to upload truss to S3
        log.Fatal("Upload failed:", e)

    case *truss.ArchiveError:
        // Failed to create truss archive
        log.Fatal("Archive creation failed:", e)

    case *truss.ConfigError:
        // Problem with truss or SDK configuration
        log.Fatal("Config error:", e)

    default:
        log.Fatal(err)
    }
}

.trussignore

The SDK respects .trussignore files in your truss directory. This file uses gitignore-style patterns to exclude files from the upload.

Default exclusions (applied even without a .trussignore file):

  • .git
  • __pycache__
  • *.pyc, *.pyo
  • .DS_Store
  • .env, .venv, venv
  • node_modules

Example .trussignore:

# Exclude test files
tests/
*_test.py

# Exclude large data files
*.csv
*.parquet
data/

# Exclude secrets
secrets/
*.key

Development

Running Tests

# Run all unit tests
go test -v ./...

# Run tests with coverage
go test -cover ./...

Running Integration Tests

Integration tests push actual models to Baseten and require valid credentials in ~/.trussrc.

# Run integration tests (requires ~/.trussrc with valid API key)
go test -tags=integration -v ./...

The integration tests will:

  • Authenticate using the "baseten" remote from ~/.trussrc
  • Push a test truss to your workspace
  • Poll until the deployment reaches MODEL_READY status

Requirements

  • Go 1.21 or later
  • A Baseten account and API key

License

MIT

About

Golang SDK for Truss

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages