AI SDK for Python

August 24, 2025 ยท View on GitHub

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.

๐Ÿ†• New Enhanced Features

  • ๐Ÿ”ง 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

๐ŸŽฏ Project Goals

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

โœ… Current Status

All Major Features Completed โœจ

Core Functionality โœ… COMPLETE

  • โœ… 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

Providers โœ… ALL 29 IMPLEMENTED

  • โœ… 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

Framework Integrations โœ… 4/4 COMPLETE

  • โœ… 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

๐Ÿš€ Enhanced Features Quick Start

FastAPI Integration

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 uvicorn

Multiple Schema Validation

from 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)

Flask Integration

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 run

Usage with FastAPI

Get 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_KEY environment variable
  • Run with ./examples/fastapi_integration_example.py

๐Ÿ“– View Complete Enhanced Features Guide

๐Ÿ› ๏ธ Development

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

Setup

# 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

Testing

# 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 -v

๐Ÿ“š Documentation

Comprehensive documentation is planned and will be available at a later date. For now, refer to:

  • agent/LONG_TERM_PLAN.md - Detailed development roadmap
  • agent/CURRENT_SESSION_TODOS.md - Current session progress
  • Source code with extensive type hints and docstrings

๐Ÿค Contributing

This project is in early development. Contributions will be welcome once the core architecture is established.

๐Ÿ“„ License

Apache 2.0 - same as the original AI SDK.

๐Ÿ™ Acknowledgments

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.