Skip to main content

Welcome to the Python AI SDK

A pure Python re-implementation of Vercel’s popular AI SDK for TypeScript. Get started with AI in minutes, not hours.

Quick Start

Get up and running in under 5 minutes with our quick start guide.

Why the Python AI SDK?

Python is the defacto language for AI. However, to actually get started with AI, you’ll need to 1. use a bloated external framework and install a bunch of dependencies, or 2. use an incredibly confusing API client (to simply call an LLM, you need client.chat.completions.create(**kwargs).result.choices[0].message.content).

Features

  • Zero-configuration functions that work consistently across providers
  • First-class streaming & tool-calling support
  • Strong Pydantic types throughout - you know exactly what you’re getting
  • Strict structured-output generation and streaming via Pydantic models
  • Provider-agnostic embeddings with built-in batching & retry logic
  • Tiny dependency footprint - no bloated external frameworks

Quick Examples

Text Generation

from ai_sdk import generate_text, openai

model = openai("gpt-4.1-mini")
res = generate_text(model=model, prompt="Tell me a haiku about Python")
print(res.text)

Streaming

import asyncio
from ai_sdk import stream_text, openai

async def main():
    model = openai("gpt-4.1-mini")
    stream_res = stream_text(model=model, prompt="Write a story")

    async for chunk in stream_res.text_stream:
        print(chunk, end="", flush=True)

asyncio.run(main())

Structured Output

from ai_sdk import generate_object, openai
from pydantic import BaseModel

class Person(BaseModel):
    name: str
    age: int

model = openai("gpt-4.1-mini")
res = generate_object(
    model=model,
    schema=Person,
    prompt="Create a person named Alice, age 30"
)
print(res.object)  # Person(name='Alice', age=30)

Core Functions

Text Generation

Generate text synchronously with generate_text or stream in real-time with stream_text.

Object Generation

Parse model output directly into your own Pydantic models with automatic validation.

Embeddings

Create vector embeddings with embed and embed_many, plus semantic similarity with cosine_similarity.

Tools

Turn any Python function into an LLM-callable tool with a single decorator.

Supported Providers

https://upload.wikimedia.org/wikipedia/commons/6/66/OpenAI_logo_2025_%28symbol%29.svg

OpenAI

GPT models, embeddings, and function calling with full streaming support.
https://assets.streamlinehq.com/image/private/w_300,h_300,ar_1/f_auto/v1/icons/1/anthropic-icon-wii9u8ifrjrd99btrqfgi.png/anthropic-icon-tdvkiqisswbrmtkiygb0ia.png?_a=DATAg1AAZAA0

Anthropic

Claude models with OpenAI-compatible API interface.

Installation

Install via UV (Python package manager):
uv add ai-sdk-python
Or with pip:
pip install ai-sdk-python
That’s it - no extra build steps or config files.

Key Benefits

1. Zero Configuration

No complex setup - just import and use:
from ai_sdk import generate_text, openai
res = generate_text(model=openai("gpt-4.1-mini"), prompt="Hello!")

2. Provider Agnostic

Swap providers without changing code:
# Works with any provider
model = openai("gpt-4.1-mini")  # or anthropic("claude-3-haiku-20240307")
res = generate_text(model=model, prompt="Hello!")

3. Strong Typing

Full Pydantic integration for type safety:
from pydantic import BaseModel
from ai_sdk import generate_object

class User(BaseModel):
    name: str
    age: int

res = generate_object(model=model, schema=User, prompt="Create a user")
user = res.object  # Fully typed User instance

4. Built-in Streaming

Real-time text generation:
async for chunk in stream_text(model=model, prompt="Tell a story").text_stream:
    print(chunk, end="", flush=True)

5. Automatic Tool Calling

Define tools once, use everywhere:
add = tool(name="add", description="Add numbers",
           parameters={...}, execute=lambda x, y: x + y)

res = generate_text(model=model, prompt="What's 2+2?", tools=[add])

Documentation Sections

Getting Started

Introduction

Learn about the SDK’s philosophy and core concepts.

Concepts

Understand the fundamental concepts and patterns.

Examples & Guides

Basic Text

Simple text generation examples.

Streaming

Real-time streaming examples.

Structured Output

Working with Pydantic models.

Tool Agent

Building agents with tool calling.

Embeddings Search

Semantic search with embeddings.

Reference

Text Generation

generate_text and stream_text functions.

Object Generation

generate_object and stream_object functions.

Embeddings

embed, embed_many, and cosine_similarity functions.

Tools

tool function for LLM-callable functions.

Providers

Supported AI providers and their features.

Types

Type definitions and schemas.

Community & Support

GitHub

View source code, report issues, and contribute.

PyPI

Install the package from PyPI.

Support

Get help with implementation questions.

The Python AI SDK is designed to be simple yet powerful. Start with the basics and gradually explore advanced features like tool calling and structured output.