Skip to content

Python SDK Overview

ai-lib-python (v0.5.0) is the official Python runtime for AI-Protocol. It provides a developer-friendly, fully async interface with Pydantic v2 type safety and production-grade telemetry.

The Python SDK mirrors the Rust runtime’s layered architecture:

  • AiClient — Main entry point with factory methods
  • AiClientBuilder — Fluent configuration builder
  • ChatRequestBuilder — Request construction
  • ChatResponse / CallStats — Response types
  • CancelToken / CancellableStream — Stream cancellation
  • ProtocolLoader — Loads manifests from local/env/GitHub with caching
  • ProtocolManifest — Pydantic models for provider configurations
  • Validator — JSON Schema validation (fastjsonschema)
  • Decoder — SSE, JSON Lines, Anthropic SSE decoders
  • Selector — JSONPath-based frame selection (jsonpath-ng)
  • Accumulator — Tool call assembly
  • FanOut — Multi-candidate expansion
  • EventMapper — Protocol-driven, Default, and Anthropic mappers
  • HttpTransport — httpx-based async HTTP with streaming
  • Auth — API key resolution from env vars and keyring
  • ConnectionPool — Connection pooling for performance
  • ResilientExecutor — Combines all patterns
  • RetryPolicy — Exponential backoff
  • RateLimiter — Token bucket
  • CircuitBreaker — Failure isolation
  • Backpressure — Concurrency limiting
  • FallbackChain — Multi-target failover
  • PreflightChecker — Unified request gating
  • ModelManager — Model registration and selection
  • ModelArray — Load balancing across endpoints
  • Selection strategies — Round-robin, weighted, cost-based, quality-based
  • MetricsCollector — Prometheus metrics export
  • Tracer — OpenTelemetry distributed tracing
  • Logger — Structured logging
  • HealthChecker — Service health monitoring
  • FeedbackCollector — User feedback
  • embeddings/ — EmbeddingClient with vector operations
  • cache/ — Multi-backend caching (memory, disk)
  • tokens/ — TokenCounter (tiktoken) and cost estimation
  • batch/ — BatchCollector/Executor with concurrency control
  • plugins/ — Plugin base, registry, hooks, middleware
  • structured/ — JSON mode, schema generation, output validation
  • guardrails/ — Content filtering, validators
PackagePurpose
httpxAsync HTTP client
pydanticData validation and types
pydantic-settingsSettings management
fastjsonschemaManifest validation
jsonpath-ngJSONPath expressions
pyyamlYAML parsing
ExtraPackages
[telemetry]OpenTelemetry, Prometheus
[tokenizer]tiktoken
[full]All of the above + watchdog, keyring

Requires Python 3.10+.