Skip to main content

Official Python Runtime for AI-Protocol - The canonical Pythonic implementation for unified AI model interaction

Project description

ai-lib-python

Official Python Runtime for AI-Protocol โ€” The canonical Pythonic implementation for unified AI model interaction

PyPI Version Python 3.10+ License

ai-lib-python is the Python runtime implementation for the AI-Protocol specification, embodying the core design principle:

All logic is operators, all configuration is protocol

๐ŸŽฏ Design Philosophy

Unlike traditional adapter libraries that hardcode provider-specific logic, ai-lib-python is a protocol-driven runtime:

  • Zero Hardcoding โ€” All behavior is driven by protocol manifests (YAML/JSON)
  • Operator Pipeline โ€” Decoder โ†’ Selector โ†’ Accumulator โ†’ FanOut โ†’ EventMapper
  • Hot Reload โ€” Protocol configurations can be updated at runtime without restart
  • Unified Interface โ€” Single API for all providers, no provider-specific code needed

๐Ÿ—๏ธ v0.8 Architecture: Logical Separation

Starting from v0.8.0, ai-lib-python adopts an execution/policy logical separation architecture. Unlike Rust, Python maintains a single-package structure (following Python ecosystem conventions), with clear module-level separation:

ai_lib_python/
โ”œโ”€โ”€ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โ”‚   E Layer (Execution) โ€” Deterministic execution, minimal deps
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โ”‚   client/          Unified client interface
โ”‚   protocol/        Protocol loading and validation
โ”‚   pipeline/        Operator pipeline
โ”‚   transport/       HTTP transport layer
โ”‚   drivers/         Provider drivers
โ”‚   types/           Type system (Message, Event, Tool)
โ”‚   structured/      Structured output
โ”‚   embeddings/      Embedding generation
โ”‚   mcp/             MCP tool bridging
โ”‚   computer_use/    Computer Use abstraction
โ”‚   multimodal/      Multimodal support
โ”‚   stt/ / tts/      Speech recognition/synthesis
โ”‚   rerank/          Re-ranking
โ”‚
โ”œโ”€โ”€ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โ”‚   P Layer (Policy) โ€” Policy decisions, may be stateful
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โ”‚   routing/         Model routing
โ”‚   cache/           Response caching
โ”‚   batch/           Batch processing
โ”‚   plugins/         Plugin system
โ”‚   resilience/      Resilience (retry/circuit-breaker/rate-limit)
โ”‚   telemetry/       Telemetry and observability
โ”‚   guardrails/      Input/output guardrails
โ”‚   tokens/          Token counting and cost estimation
โ”‚   registry/        Capability registry

Benefits of E/P Separation

Aspect E Layer Modules P Layer Modules
Responsibility Deterministic execution, protocol loading, type conversion Policy decisions, caching, routing, telemetry
Dependencies Minimal, stateless May be stateful, depends on E layer
Use Case Edge devices, serverless, microservices Server-side, full applications

Installation Options

# Basic installation (E-layer core capabilities)
pip install ai-lib-python

# Full installation (all capabilities)
pip install ai-lib-python[full]

BYOK Credential Chain

ai-lib-python resolves provider credentials through the unified PT-074 chain:

  1. explicit application override;
  2. manifest-declared env from endpoint.auth or V1 top-level auth;
  3. conventional <PROVIDER_ID>_API_KEY;
  4. optional platform keyring support when installed and enabled.

Auth attachment follows the active manifest auth shape (bearer, custom header, or query parameter). Diagnostics expose only source metadata and env var names, never raw key values.

Capability Extras

Execution Layer Capabilities:

  • [vision] โ€” Image processing (Pillow)
  • [audio] โ€” Audio processing (soundfile)
  • [embeddings] โ€” Embedding generation
  • [structured] โ€” Structured output / JSON mode
  • [stt] โ€” Speech-to-text
  • [tts] โ€” Text-to-speech
  • [reranking] โ€” Document re-ranking

Policy Layer Capabilities:

  • [batch] โ€” Batch processing
  • [telemetry] โ€” OpenTelemetry integration
  • [tokenizer] โ€” Token counting (tiktoken)

Meta-extras:

  • [full] โ€” Enable all capabilities
  • [dev] โ€” Development dependencies (pytest, mypy, ruff)
  • [docs] โ€” Documentation build (mkdocs)

๐Ÿš€ Quick Start

Basic Usage

import asyncio
from ai_lib_python import AiClient, Message

async def main():
    # Protocol-driven: supports any provider defined in ai-protocol manifests
    client = await AiClient.create("anthropic/claude-3-5-sonnet")

    # Simple chat
    response = await (
        client.chat()
        .system("You are a helpful assistant.")
        .user("Hello!")
        .execute()
    )

    print(response.content)
    await client.close()

asyncio.run(main())

Streaming Response

import asyncio
from ai_lib_python import AiClient, Message
from ai_lib_python.types.events import StreamingEvent

async def main():
    client = await AiClient.create("openai/gpt-4o")

    # Streaming chat
    async for event in client.chat().user("Tell me a joke").stream():
        if isinstance(event, StreamingEvent.PartialContentDelta):
            print(event.content, end="", flush=True)
        elif isinstance(event, StreamingEvent.StreamEnd):
            print()  # newline

    await client.close()

asyncio.run(main())

Production Configuration

from ai_lib_python import AiClient

# Enable full production capabilities: retry, circuit breaker, rate limiting
client = await (
    AiClient.builder()
    .model("deepseek/deepseek-chat")
    .production_ready()  # One-click enable all resilience patterns
    .build()
)

Multimodal

from ai_lib_python import Message, MessageContent, ContentBlock

# Image + text
message = Message(
    role="user",
    content=MessageContent.blocks([
        ContentBlock.text("Describe this image"),
        ContentBlock.image_from_file("./photo.jpg"),
    ])
)

response = await client.chat().messages([message]).execute()

๐Ÿ”ง Configuration

Protocol Manifest Search Path

The runtime searches for protocol configurations in the following order:

  1. AI_PROTOCOL_DIR / AI_PROTOCOL_PATH environment variable
  2. Common development paths: ai-protocol/, ../ai-protocol/, ../../ai-protocol/
  3. Final fallback: GitHub raw ailib-official/ai-protocol

API Keys

Recommended (production):

export ANTHROPIC_API_KEY="sk-ant-..."
export OPENAI_API_KEY="sk-..."
export DEEPSEEK_API_KEY="sk-..."

Optional (local development): OS keyring (requires keyring package)

Environment Variables

# Proxy
export AI_PROXY_URL="http://user:pass@host:port"

# Timeout
export AI_HTTP_TIMEOUT_SECS=30

# Concurrency limit
export AI_LIB_MAX_INFLIGHT=10

# Rate limiting
export AI_LIB_RPS=5  # or AI_LIB_RPM=300

HTTP proxies (cross-runtime parity with ai-lib-rust)

Variable Purpose
AI_PROXY_URL Explicit proxy URL for outbound requests when AI_HTTP_TRUST_ENV=1 (Python also honors httpx trust_env only in that mode).
HTTP_PROXY / HTTPS_PROXY Standard vars; in Rust they are merged with AI_PROXY_URL as candidate routes. In Python, enable via AI_HTTP_TRUST_ENV=1 so local/mock traffic is not accidentally proxied.
NO_PROXY / AI_PROXY_NO_PROXY Comma-separated hosts that must bypass the proxy (include mock hostnames, API hostnames that must be direct, and 127.0.0.1). Rust documents AI_PROXY_NO_PROXY; set the same where your stack reads it.

With a proxy: set NO_PROXY to include the mock server host (for example NO_PROXY=192.168.2.13,localhost,127.0.0.1).

Or in code: AiClient.create("openai/gpt-4o", base_url="http://localhost:4010").

For shared semantics across runtimes, see CROSS_RUNTIME.md.

๐Ÿงช Testing

Unit Tests

# Run all tests
pytest tests/ -v

# Run unit tests only
pytest tests/unit/ -v

Compliance Tests (Cross-Runtime Consistency)

Install test dependencies first (pytest is not a default runtime dependency):

python -m pip install -e ".[dev]"
# Run compliance tests
python -m pytest tests/compliance/ -v

# Specify compliance directory (POSIX)
COMPLIANCE_DIR=../ai-protocol/tests/compliance python -m pytest tests/compliance/ -v

# Windows PowerShell
$env:COMPLIANCE_DIR = "D:\ai-protocol\tests\compliance"
python -m pytest tests/compliance/ -v

# Wave-5 execution-layer-only subset (skips resilience-heavy cases)
$env:COMPLIANCE_SUBSET = "e_only"   # PowerShell
# COMPLIANCE_SUBSET=e_only python -m pytest tests/compliance/ -v   # POSIX
python -m pytest tests/compliance/ -v

Testing with Mock Server

# Start ai-protocol-mock
docker-compose up -d

# Run tests with mock
MOCK_HTTP_URL=http://localhost:4010 pytest tests/integration/ -v

๐Ÿ“Š Observability

Call Statistics

response = await client.chat().user("Hello").execute()
print(f"request_id: {response.stats.request_id}")
print(f"latency_ms: {response.stats.latency_ms}")
print(f"tokens: {response.usage}")

Telemetry Feedback (opt-in)

from ai_lib_python.telemetry import FeedbackEvent, ChoiceSelectionFeedback, report_feedback

await report_feedback(
    FeedbackEvent.ChoiceSelection(
        request_id=response.stats.request_id,
        chosen_index=0,
    )
)

๐Ÿ”„ Error Codes (V2 Specification)

All provider errors are normalized to 13 standard error codes:

Code Name Retryable Fallbackable
E1001 invalid_request No No
E1002 authentication No Yes
E1003 permission_denied No No
E1004 not_found No No
E1005 request_too_large No No
E2001 rate_limited Yes Yes
E2002 quota_exhausted No Yes
E3001 server_error Yes Yes
E3002 overloaded Yes Yes
E3003 timeout Yes Yes
E4001 conflict Yes No
E4002 cancelled No No
E9999 unknown No No

๐Ÿค Community & Contributing

Use Cases

  • Server Applications โ€” Use pip install ai-lib-python[full] for complete capabilities
  • Edge/Serverless โ€” Basic installation, add extras as needed
  • Microservices โ€” Combine with telemetry for distributed tracing

Contributing Guidelines

  1. Code must pass ruff checks and mypy type checking
  2. New features must include tests; compliance tests must pass
  3. Follow Python type hints best practices

Code of Conduct

  • Respect all contributors
  • Welcome participants from all backgrounds
  • Focus on technical discussions, avoid personal attacks
  • Report issues via GitHub Issues

๐Ÿ”— Related Projects

Project Description
AI-Protocol Protocol specification (v1.5 / V2)
ai-lib-rust Rust runtime
ai-lib-ts TypeScript runtime
ai-lib-go Go runtime
ai-protocol-mock Mock server

๐Ÿ“„ License

This project is dual-licensed:

You may choose either.


ai-lib-python โ€” Where protocol meets Pythonic ๐Ÿš€

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ai_lib_python-1.0.0.tar.gz (162.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ai_lib_python-1.0.0-py3-none-any.whl (215.9 kB view details)

Uploaded Python 3

File details

Details for the file ai_lib_python-1.0.0.tar.gz.

File metadata

  • Download URL: ai_lib_python-1.0.0.tar.gz
  • Upload date:
  • Size: 162.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ai_lib_python-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5c90e14f19805533741248eb495add8b6101eca44be13aa817036f8759a9d134
MD5 987f8b67173924d3aaffdbb1fa5e929b
BLAKE2b-256 ac5abf305c730d6a7e1330ad6b38c43318c5a3b4576497adfdda45e67d17af10

See more details on using hashes here.

File details

Details for the file ai_lib_python-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: ai_lib_python-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 215.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ai_lib_python-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a24e646a1f76cea5cb97a5247b2e004543015b80b303724eb0b426a3dd8c4519
MD5 7608d4ed5c2faaa1ea5911376d9b2b9b
BLAKE2b-256 29c2e167c51b71b0dcb177e70355b8dab12c2fb7fae40fdb7a9bdf7b6a8996f9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page