The Requests library for AI one Unified Python SDK for every LLM provider UniversalAI, a new open-source Python SDK, provides a unified interface for interacting with nine major LLM providers, including OpenAI, Anthropic, Gemini, and Ollama. The SDK supports async-first operations, streaming, tool calling, middleware, routing, vision, audio, image generation, embeddings, RAG, agents, context safety, cost tracking, and a CLI, aiming to simplify AI application development. The Requests library for AI — one unified SDK for every LLM provider. Write once, run anywhere. pip install universal-ai python from universal ai import AI ai = AI provider="openai", model="gpt-4o" response = await ai.chat "What is quantum computing?" print response.content Building AI applications today means juggling multiple provider SDKs, each with different APIs, error handling, and quirks. UniversalAI gives you one clean interface that works across all major providers: | Feature | Description | |---|---| 9 Providers | OpenAI, Anthropic, Gemini, Ollama, Groq, Mistral, OpenRouter, HuggingFace, Azure OpenAI | Async-first | Full async / await with synchronous wrappers for scripts and notebooks | Streaming | Real-time token streaming from any provider | Tool Calling | @tool decorator with automatic execution loop | Middleware | Retry, cache, rate limit, circuit breaker, cost tracking, logging | Routing | Fallback, round-robin, lowest latency, lowest cost strategies | Vision | Image-aware chat with OpenAI, Anthropic, Gemini | Audio | Transcription Whisper and TTS with OpenAI | Image Generation | DALL-E 3 support | Embeddings | OpenAI, Gemini, Mistral, HuggingFace, Azure, OpenRouter | RAG | Built-in retrieval-augmented generation with chunking and vector store | Agents | Multi-agent orchestration with coordinator pattern | Context Safety | Automatic validation and optional truncation | Cost Tracking | Per-request and cumulative cost estimation | CLI | Full-featured uai command-line tool | Core SDK auto-detects available providers pip install universal-ai With specific provider support pip install universal-ai openai pip install universal-ai anthropic pip install universal-ai gemini pip install universal-ai ollama Everything pip install universal-ai all python import asyncio from universal ai import AI async def main : Auto-detect provider from environment ai = AI Chat response = await ai.chat "Explain quantum computing in one sentence" print response.content Streaming async for chunk in ai.stream "Write a haiku about programming" : print chunk.delta, end="", flush=True Embeddings embed response = await ai.embed "Hello, world " print f"Embedding dimensions: {len embed response.vector }" asyncio.run main python from universal ai import AI OpenAI ai = AI provider="openai", model="gpt-4o" response = await ai.chat "Hello " Anthropic ai = AI provider="anthropic", model="claude-sonnet-4-20250514" response = await ai.chat "Hello " Local Ollama ai = AI provider="ollama", model="llama3" response = await ai.chat "Hello " python from universal ai import AI ai = AI provider="openai", model="gpt-4o" Synchronous wrappers for scripts/notebooks response = ai.chat sync "Hello " print response.content Sync streaming returns full text text = ai.stream sync "Tell me a joke" print text Define tools with the @tool decorator and let the AI use them: python from universal ai import AI, tool @tool def get weather city: str, unit: str = "celsius" - str: """Get current weather for a city.""" In a real app, call a weather API return f"Weather in {city}: 22°{unit 0 .upper }, sunny" @tool def calculate expression: str - str: """Evaluate a mathematical expression.""" return str eval expression ai = AI provider="openai", model="gpt-4o" The AI will automatically call your tools response = await ai.chat "What's the weather in Paris? Also calculate 15 23.", tools= get weather, calculate print response.content php from universal ai import AI, tool @tool def search query: str - str: """Search the web.""" return f"Results for: {query}" ai = AI provider="openai", model="gpt-4o" ai.register tool search Tools are auto-executed in the tool loop response = await ai.chat "Search for Python tutorials" Multi-turn conversations with automatic history management: python from universal ai import AI ai = AI provider="openai", model="gpt-4o" Create a conversation conv = ai.conversation system prompt="You are a helpful cooking assistant.", max turns=20 Send messages response = await conv.send message="What should I cook for dinner?" print response.content response = await conv.send message="Can you give me a recipe?" print response.content Access history print f"Turn count: {conv.turn count}" print f"Messages: {len conv.history }" Reset conv.reset Provider selection export UNIVERSALAI PROVIDER=openai export UNIVERSALAI MODEL=gpt-4o API keys provider-specific export OPENAI API KEY=sk-... export ANTHROPIC API KEY=sk-ant-... export GEMINI API KEY=... export GROQ API KEY=gsk ... export MISTRAL API KEY=... export OPENROUTER API KEY=sk-or-... export HF API KEY=hf ... Azure OpenAI export AZURE OPENAI API KEY=... export AZURE OPENAI API BASE=https://your-resource.openai.azure.com export AZURE OPENAI DEPLOYMENT NAME=gpt-4o Ollama local export OLLAMA HOST=http://localhost:11434 ~/.config/universalai/config.yaml provider: openai model: gpt-4o temperature: 0.7 max tokens: 4096 timeout: 30 max retries: 3 auto truncate: true fallback providers: - anthropic - gemini provider api keys: openai: sk-... anthropic: sk-ant-... python from universal ai import AI, Config config = Config provider="openai", model="gpt-4o", temperature=0.7, max tokens=4096, timeout=30, max retries=3, auto truncate=True, provider api keys={ "openai": "sk-...", "anthropic": "sk-ant-...", } ai = AI config=config Add resilience and observability to your requests: python from universal ai import AI from universal ai.middleware import RetryMiddleware, CacheMiddleware, RateLimitMiddleware, CircuitBreakerMiddleware, CostTrackingMiddleware, LoggingMiddleware, ai = AI provider="openai", model="gpt-4o" Add middleware in order executed top to bottom ai.add middleware LoggingMiddleware ai.add middleware CostTrackingMiddleware ai.add middleware RetryMiddleware max retries=3, base delay=1.0 ai.add middleware CacheMiddleware ttl=300 ai.add middleware RateLimitMiddleware requests per minute=60 ai.add middleware CircuitBreakerMiddleware failure threshold=5 All requests now go through the middleware pipeline response = await ai.chat "Hello " | Middleware | Purpose | Key Options | |---|---|---| RetryMiddleware | Retry failed requests | max retries , base delay , max delay , jitter | CacheMiddleware | Cache responses | ttl , backend memory/sqlite/redis | RateLimitMiddleware | Limit request rate | requests per minute , burst | CircuitBreakerMiddleware | Stop cascading failures | failure threshold , recovery timeout | CostTrackingMiddleware | Track API costs | — | LoggingMiddleware | Log requests/responses | log level | Automatically select the best provider: python from universal ai import AI from universal ai.router import Router, FallbackStrategy, RoundRobinStrategy, LowestLatencyStrategy, LowestCostStrategy, Configure fallback in config config = Config provider="openai", fallback providers= "anthropic", "gemini" ai = AI config=config Or use router directly router = Router providers= "openai", "anthropic", "gemini" , strategy=FallbackStrategy | Strategy | Behavior | |---|---| FallbackStrategy | Try first provider, failover to next on error | RoundRobinStrategy | Distribute requests evenly across providers | LowestLatencyStrategy | Always use the fastest responding provider | LowestCostStrategy | Always use the cheapest provider | Build knowledge-base powered chat: python from universal ai import AI from universal ai.rag import RAG, TextLoader, DirectoryLoader Initialize RAG rag = RAG chunk size=500, chunk overlap=50, top k=3 Add content rag.add text "Python is a high-level programming language..." rag.add document Document content="...", source="docs.txt" rag.add folder "./knowledge base" rag.add url "https://example.com/article.txt" rag.add github "owner/repo" Search chunks = await rag.search "What is Python?" for chunk in chunks: print f"Score: {chunk.content :50 }..." Use with AI ai = AI provider="openai", model="gpt-4o" augmented request = await rag.augment request chat request response = await ai.chat augmented request ai = AI provider="openai", model="gpt-4o" Transcribe audio file text = await ai.transcribe "audio.mp3" print text Transcribe from bytes text = await ai.transcribe audio bytes Generate speech audio bytes = await ai.speak "Hello, world ", voice="alloy" with open "output.mp3", "wb" as f: f.write audio bytes Generate image urls = await ai.image "A sunset over mountains", size="1024x1024" print urls 0 URL to generated image UniversalAI includes a full-featured command-line tool: Chat interactively uai chat Chat with specific provider uai chat -p openai -m gpt-4o Send a single message uai chat "What is machine learning?" List available providers uai providers Run diagnostics uai doctor Manage configuration uai config show uai config set provider openai uai config set-api-key openai Benchmark providers uai benchmark --iterations 10 Start local API server uai serve --port 8000 ai = AI provider="openai", model="gpt-4o" Features: Chat, Streaming, Vision, Tools, Embeddings, Audio, Image Gen Requires: OPENAI API KEY ai = AI provider="anthropic", model="claude-sonnet-4-20250514" Features: Chat, Streaming, Vision, Tools Requires: ANTHROPIC API KEY ai = AI provider="gemini", model="gemini-2.0-flash" Features: Chat, Streaming, Vision, Tools, Embeddings Requires: GEMINI API KEY ai = AI provider="ollama", model="llama3" Features: Chat, Streaming, Embeddings Requires: Ollama running locally Install: https://ollama.ai ai = AI provider="groq", model="llama-3.1-70b-versatile" Features: Chat, Streaming, Tools Requires: GROQ API KEY ai = AI provider="mistral", model="mistral-large-latest" Features: Chat, Streaming, Tools, Embeddings Requires: MISTRAL API KEY ai = AI provider="openrouter", model="openai/gpt-4o" Features: Chat, Streaming, Vision, Tools, Embeddings Requires: OPENROUTER API KEY ai = AI provider="huggingface", model="meta-llama/Llama-2-7b-chat-hf" Features: Chat, Streaming, Embeddings Requires: HF API KEY ai = AI provider="azure", model="gpt-4o" Features: Chat, Streaming, Vision, Tools, Embeddings Requires: AZURE OPENAI API KEY, AZURE OPENAI API BASE python from universal ai import AI from universal ai.exceptions import AuthenticationError, RateLimitError, ContextWindowExceededError, ProviderError, TimeoutError, ai = AI provider="openai", model="gpt-4o" try: response = await ai.chat "Hello " except AuthenticationError as e: print f"Invalid API key: {e}" except RateLimitError as e: print f"Rate limited, retry after: {e.retry after}s" except ContextWindowExceededError as e: print f"Context too long: {e.estimated tokens} {e.context window}" except ProviderError as e: print f"Provider error: {e}" except TimeoutError: print "Request timed out" UniversalAI validates that messages fit within the provider's context window: python from universal ai import AI, Config Option 1: Raise error if too long default config = Config auto truncate=False ai = AI config=config Option 2: Auto-truncate to fit config = Config auto truncate=True ai = AI config=config ai = AI provider="openai", model="gpt-4o" Estimate cost before sending estimated cost = ai.estimate cost "Hello, world " print f"Estimated cost: ${estimated cost:.6f}" Track actual costs with middleware from universal ai.middleware import CostTrackingMiddleware cost middleware = CostTrackingMiddleware ai.add middleware cost middleware response = await ai.chat "Hello " print f"Actual cost: ${response.usage.estimated cost:.6f}" print f"Total cost: ${cost middleware.total cost:.6f}" For scripts and notebooks where you can't use async : | Async Method | Sync Wrapper | |---|---| await ai.chat ... | ai.chat sync ... | async for chunk in ai.stream ... | ai.stream sync ... | await ai.embed ... | ai.embed sync ... | await ai.chat with tools ... | ai.chat with tools sync ... | await ai.chat json ... | ai.chat json sync ... | See the examples/ https://dev.toexamples/ directory for complete working examples: basic chat.py - Simple chat usage streaming.py - Real-time streaming tool calling.py - Tool definition and execution middleware demo.py - Middleware configuration rag demo.py - RAG with document loading multi provider.py - Provider switchingWe welcome contributions Please see CONTRIBUTING.md https://CONTRIBUTING.md for guidelines. Clone the repo git clone https://github.com/6t9xstar/universal-ai.git cd universal-ai Install dev dependencies pip install -e ". dev " Run tests pytest Run linting ruff check . Run type checking mypy . MIT License - see LICENSE https://dev.toLICENSE for details.