Skip to content

SDK wrapper, serverless framework, and integration tests#328

Draft
begna112 wants to merge 2 commits intovast-ai:masterfrom
begna112:pr/sdk-merge
Draft

SDK wrapper, serverless framework, and integration tests#328
begna112 wants to merge 2 commits intovast-ai:masterfrom
begna112:pr/sdk-merge

Conversation

@begna112
Copy link

Summary

Adds the vastai Python SDK package that wraps the CLI for programmatic use, plus a serverless framework for deploying GPU workers.

Depends on: #327 (CLI fixes and test infrastructure) — please merge #327 first, then this PR will rebase cleanly.

SDK Package (vastai/)

  • VastAI class wrapping all CLI commands with Python-native interface
  • Stdout capture/restore for clean programmatic output
  • Raw mode support returning parsed JSON dicts
  • Module-level lazy imports for optional serverless dependencies

Serverless Framework (vastai/serverless/)

  • Client: VastClient for dispatching work to GPU workers with SSL pinning, retry logic, and async I/O
  • Server: VastWorker base class for implementing GPU worker endpoints with Prometheus metrics
  • Data types for worker status, requests, and responses

Additional Files

  • vastai_sdk/ backward-compatibility shim
  • examples/ - 17 client/server usage examples
  • benchmarks/ - Prometheus metrics and docker-compose setup
  • 13 SDK/serverless test files covering integration, regression, and unit tests

Changes to Existing Files

  • pyproject.toml - Added pyparsing dependency, vastai/vastai_sdk packages, serverless/all optional dependency groups, mypy overrides
  • .github/workflows/ci.yml - Added type-check job (mypy)

Stats

  • 55 files changed, +10,012 lines, -23 lines
  • 546 tests passing (44.46% coverage)

Test plan

  • from vastai import VastAI succeeds
  • python -m vastai --help works
  • All PR 1 CLI tests still pass
  • All SDK/serverless tests pass (pytest tests/)
  • mypy vastai/ type checking passes

Bug fixes:
- Fix mutable default arguments in http_put/post/del
- Add missing exception handling in http_request
- Fix timezone handling with UTC-aware timestamps
- Fix show_instances row rebinding bug
- Fix show_machine dict response handling
- Fix parse_query field alias data loss
- Remove hardcoded if True anti-patterns
- Fix raw mode to return parsed JSON consistently
- Fix namespace attribute typo
- Replace bare except clauses with specific exceptions
- Remove shell=True from subprocess calls
- Fix self-test instance creation type handling and error messages

Infrastructure improvements:
- Add request timeout support with configurable defaults
- Expand retry logic to cover 502/503/504 and connection errors
- Add JSONDecodeError protection to high-risk .json() sites
- Consolidate direct requests calls through http_* helpers
- Add structured JSON error output for --raw mode
- Add raw mode return handling to all command functions
- Remove unreachable code after raise_for_status()
- Add api_call/output_result/error_output DRY helpers
- Add 2FA session expiry handling with automatic key fallback
- Filter sensitive fields from show endpoints output

CLI improvements:
- Improve help text and descriptions for 130+ commands
- Add named flags for transfer credit (--sender, --recipient)
- Fix argument mismatches in usage strings
- Standardize show maints to use --ids instead of -ids
- Remove Python 2 compatibility code

Test infrastructure:
- Add pytest with regression, smoke, and unit test suites
- Add CI workflow with lint, test, and smoke-test jobs
- Add ruff configuration for code quality
- Add dev dependencies to pyproject.toml
Introduce the vastai Python package providing programmatic SDK access
to the Vast.ai API, plus a serverless client/server framework for
building GPU workers.

New packages:
- vastai/ - SDK wrapper (sdk.py), base class (vastai_base.py),
  serverless client and server framework
- vastai_sdk/ - Backward-compatibility shim for old import path
- examples/ - 17 client/server example scripts
- benchmarks/ - Prometheus metrics and docker-compose

Test additions:
- 7 unit tests for serverless components and VastAIBase
- 6 regression tests for SDK integration, lazy imports,
  exception handling, naming consistency

Config updates:
- pyproject.toml: add pyparsing dep, serverless optional deps,
  vastai/vastai_sdk packages, mypy config
- ci.yml: add type-check job (mypy vastai/)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant