A Python port of the AI SDK, providing a unified interface for working with various AI providers including OpenAI, Anthropic, Google, and many more.
π Production Ready: This project has achieved 95%+ feature parity with the TypeScript AI SDK (v0.2.0). All 29 providers are implemented with enhanced features for Python developers.
- π§ Multiple Schema Validation: Support for Pydantic, JSONSchema, Marshmallow, and Cerberus
- π FastAPI Integration: High-level decorators and utilities for building AI APIs
- πΆοΈ Flask Integration: Blueprint and middleware support for Flask applications
- π Advanced Streaming: Enhanced streaming capabilities with custom processing
- ποΈ Framework Ready: Production-ready integrations for Python web frameworks
This is a comprehensive Python port of the Vercel AI SDK, aiming to provide:
- Unified Interface: Work with 30+ AI providers through a consistent API
- Modern Python: Built with modern Python features (async/await, type hints, Pydantic)
- Framework Integration: Native FastAPI and Flask integrations with decorators and middleware
- Streaming Support: Real-time streaming for text generation and structured outputs
- Tool Calling: Function/tool calling support across providers
- Type Safety: Full type safety with Pydantic models and mypy support
All Major Features Completed β¨
- β
generate_text()- Generate text with any provider - β
stream_text()- Stream text generation - β
generate_object()- Generate structured objects - β
stream_object()- Stream structured object generation - β
embed()- Generate embeddings - β
embed_many()- Batch embedding generation - β
generate_image()- AI image generation - β
generate_speech()- Text-to-speech synthesis - β
transcribe()- Speech-to-text transcription - β Agent system with multi-step reasoning
- β Advanced tool calling and orchestration
- β Comprehensive middleware system
- β LangChain and LlamaIndex adapters for ecosystem integration
- β OpenAI - GPT, DALL-E, Whisper, embeddings
- β Anthropic - Claude models with tool calling
- β Google - Gemini models with multimodal support
- β Google Vertex - Enterprise Google AI with auth
- β Azure OpenAI - Azure-hosted OpenAI models
- β Amazon Bedrock - AWS-hosted AI models
- β Groq - Ultra-fast LPU inference
- β TogetherAI - 100+ open-source models
- β Mistral - Mixtral and Mistral models
- β Cohere - Enterprise NLP models
- β Perplexity - Search-augmented generation
- β DeepSeek - Advanced reasoning models
- β xAI - Grok models
- β Cerebras - High-performance inference
- β DeepInfra - Cost-effective model hosting
- β Fireworks - Fast model serving
- β Replicate - ML model marketplace
- β ElevenLabs - Advanced text-to-speech
- β Deepgram - Speech-to-text API
- β AssemblyAI - Speech understanding
- β Fal - Image/video generation
- β Hume - Emotion-aware speech
- β LMNT - Real-time speech synthesis
- β Gladia - Audio transcription
- β Luma - AI video generation
- β Vercel - Vercel model endpoints
- β Rev AI - Professional transcription
- β Gateway - AI Gateway for routing/analytics
- β OpenAI-Compatible - Local & custom endpoints
- β LangChain - Seamless integration with LangChain ecosystem
- β LlamaIndex - RAG and document processing integration
- β FastAPI - Native decorators, middleware, streaming, and WebSocket support
- β Flask - Blueprint integration, decorators, and streaming responses
- β Schema Validation - Support for Pydantic, JSONSchema, Marshmallow, Cerberus
import os
from ai_sdk.integrations.fastapi import AIFastAPI
from ai_sdk.providers.openai import create_openai
from ai_sdk.core.generate_text import generate_text, stream_text
from ai_sdk.providers.types import Message
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
class ChatRequest(BaseModel):
message: str
model: str = "gpt-4o-mini"
# Create AI application
provider = create_openai(api_key=os.getenv("OPENAI_API_KEY"))
ai_app = AIFastAPI(default_provider=provider)
@ai_app.app.post("/chat")
async def chat(request: ChatRequest):
messages = [Message(role="user", content=request.message)]
result = await generate_text(
model=provider(request.model),
messages=messages,
max_tokens=1000
)
return {"response": result.text}
@ai_app.app.post("/chat/stream")
async def stream_chat(request: ChatRequest):
messages = [Message(role="user", content=request.message)]
async def generate():
async for chunk in stream_text(
model=provider(request.model),
messages=messages,
max_tokens=1000
):
if chunk.text_delta:
yield f'data: {{"text": "{chunk.text_delta}"}}\n\n'
yield "data: [DONE]\n\n"
return StreamingResponse(generate(), media_type="text/event-stream")
app = ai_app.app # FastAPI app ready for uvicornfrom ai_sdk.schemas import pydantic_schema, jsonschema_schema
from pydantic import BaseModel
# Pydantic (recommended)
class Response(BaseModel):
answer: str
confidence: float
schema = pydantic_schema(Response)
# JSONSchema (universal)
json_schema = jsonschema_schema({
"type": "object",
"properties": {
"answer": {"type": "string"},
"confidence": {"type": "number"}
}
})
# Use with any AI model
result = await generate_object(model=model, prompt=prompt, schema=schema)from ai_sdk.integrations.flask import AIFlask
ai_app = AIFlask(default_provider=provider)
@ai_app.chat_route("/chat")
def chat():
result = asyncio.run(generate_text(model=g.ai_provider, messages=messages))
return {"response": result.text}
app = ai_app.app # Flask app ready to runGet started quickly with the working FastAPI example:
# Run the FastAPI example server
./examples/fastapi_integration_example.py
# Test with curl
curl -X POST "http://localhost:8000/chat" \
-H "Content-Type: application/json" \
-d '{"messages":[{"role":"user","content":"Hello!"}]}'
# Expected response:
# {"response":"Hello! How can I help you today?"}The example includes:
- Direct OpenAI integration with real API responses
- Automatic dependency management via uv script
- Type-safe request/response with Pydantic models
- Production-ready error handling
Requirements:
- Set
OPENAI_API_KEYenvironment variable - Run with
./examples/fastapi_integration_example.py
π View Complete Enhanced Features Guide
This project uses modern Python tooling:
- uv - Fast Python package manager
- ruff - Fast Python linter and formatter
- mypy - Static type checking
- pytest - Testing framework
- pydantic - Data validation and serialization
# Clone the repository
git clone https://github.com/Yonom/ai-sdk-python.git
cd ai-sdk-python
# Install dependencies (requires uv)
uv sync --dev
# Run tests
uv run pytest
# Run linting
uv run ruff check
uv run mypy src# Run all tests
uv run pytest
# Run with coverage
uv run pytest --cov=ai_sdk --cov-report=html
# Run specific test
uv run pytest tests/test_basic.py -vComprehensive documentation is planned and will be available at a later date. For now, refer to:
agent/LONG_TERM_PLAN.md- Detailed development roadmapagent/CURRENT_SESSION_TODOS.md- Current session progress- Source code with extensive type hints and docstrings
This project is in early development. Contributions will be welcome once the core architecture is established.
Apache 2.0 - same as the original AI SDK.
This project is a Python port of the excellent Vercel AI SDK. All credit for the original design and API goes to the Vercel team.