AI-Lib AI-Lib
v0.5.0 · PyPI

Python Runtime
for AI-Protocol.

Developer-friendly, protocol-driven AI client. Full async support, Pydantic v2 type safety, production-grade telemetry, and smart model routing. Python 3.10+.

Terminal
pip install ai-lib-python

Key Features

Async-First Design

Native async/await throughout. httpx-powered transport with connection pooling. CancellableStream for graceful stream termination.

Pydantic v2 Types

Full type hints with Pydantic v2 validation. ProtocolManifest, Message, ContentBlock, StreamingEvent — all type-safe with runtime validation.

Resilient Executor

ResilientExecutor combines backpressure, rate limiting, circuit breaker, and retry with exponential backoff. PreflightChecker gates requests before execution.

Model Routing

ModelManager with ModelArray for intelligent model selection. Strategies include round-robin, weighted, cost-based, and quality-based routing.

Production Telemetry

MetricsCollector with Prometheus export. Distributed tracing via OpenTelemetry. Structured logging. Health monitoring. User feedback collection.

Structured Output

JSON mode configuration, schema generation from Pydantic models, output validation. Guardrails with content filters and PII detection.

Pythonic & Intuitive

A clean, Pythonic API that feels natural. Create a client from a model identifier, build requests with a fluent builder, and iterate over unified streaming events.

The same code works across all providers. Switch from OpenAI to Anthropic to DeepSeek by changing one string — the protocol manifest handles the rest.

from ai_lib_python import AiClient

# Works with ANY provider
client = await AiClient.create(
    "anthropic/claude-3-5-sonnet"
)

# Streaming chat
async for event in client.chat() \
    .user("Explain AI-Protocol") \
    .temperature(0.7) \
    .max_tokens(1000) \
    .stream():
    if event.is_content_delta:
        print(event.as_content_delta.text,
              end="")

# Non-streaming with stats
response, stats = await client.chat() \
    .user("Summarize this") \
    .execute_with_stats()

print(f"Tokens: {stats.total_tokens}")
print(f"Latency: {stats.latency_ms}ms")

Internal Architecture

Same layered architecture as the Rust runtime — protocol-driven, operator-based, resilient by default.

ai-lib-python Runtime Architecture Client Layer AiClient · AiClientBuilder · ChatRequestBuilder · ChatResponse Policy Engine PreflightChecker · SignalsSnapshot · FallbackChain Protocol Layer ProtocolLoader · Manifest · Validator Resilience Layer CircuitBreaker · RateLimiter · Retry Streaming Pipeline Decoder Selector Accumulator FanOut EventMapper Transport Layer HttpTransport (httpx) · Auth · Connection Pool OpenAI · Anthropic · Gemini · DeepSeek · Qwen · 30+ Providers

Module Overview

client/

AiClient, AiClientBuilder, ChatRequestBuilder, ChatResponse, CallStats, CancelToken, CancellableStream.

protocol/

ProtocolLoader (local/env/GitHub), Pydantic manifest models, JSON Schema validator, hot-reload support.

pipeline/

Decoder (SSE, JSON Lines, Anthropic SSE), Selector (JSONPath), Accumulator, FanOut, EventMapper (protocol-driven, default, Anthropic).

resilience/

ResilientExecutor, RetryPolicy, RateLimiter, CircuitBreaker, Backpressure, FallbackChain, SignalsSnapshot, PreflightChecker.

routing/

ModelManager, ModelArray, selection strategies (round-robin, weighted, cost-based, quality-based). Pre-configured catalogs for major providers.

telemetry/

MetricsCollector (Prometheus), Tracer (OpenTelemetry), structured Logger, HealthChecker, user feedback collection.

embeddings/ + tokens/

EmbeddingClient with vector operations. TokenCounter (tiktoken integration) and cost estimation for usage tracking.

plugins/ + structured/

Plugin base class, PluginRegistry, HookManager, middleware chain. JSON mode config, SchemaGenerator, OutputValidator.

Start Building with Python