ai

bởi vercel

Python `ai` SDK — models, providers, streams, events, tools, agents, hooks, MCP, AI SDK UI, structured output, and media generation

npx skills add https://github.com/vercel-labs/ai-python --skill ai

AI SDK for Python

Package: ai. Requires Python 3.12+. Install with uv add ai.

Unprefixed model IDs use AI Gateway and AI_GATEWAY_API_KEY. Direct providers use provider:model, their API key, and an extra:

uv add "ai[openai]"     # OPENAI_API_KEY,    ai.get_model("openai:gpt-5")
uv add "ai[anthropic]"  # ANTHROPIC_API_KEY, ai.get_model("anthropic:claude-sonnet-4")

Basic use

Use ai.stream for one model call without Python tool execution:

import ai

model = ai.get_model("anthropic/claude-sonnet-4")
messages = [
    ai.system_message("Be concise."),
    ai.user_message("Write a haiku about rain."),
]

async with ai.stream(model, messages) as stream:
    async for event in stream:
        if isinstance(event, ai.events.TextDelta):
            print(event.chunk, end="", flush=True)

answer = stream.output
message = stream.message

Use ai.Agent for a loop that executes Python tools and manages history:

@ai.tool
async def get_weather(city: str) -> str:
    """Get the weather for a city."""
    return "Sunny"


agent = ai.Agent(tools=[get_weather])
async with agent.run(model, messages) as run:
    async for event in run:
        if isinstance(event, ai.events.TextDelta):
            print(event.chunk, end="", flush=True)

answer = run.output
history = run.messages

These examples are sufficient for basic model calls, messages, tools, and agents.

Advanced work

For an advanced task, fetch its page under https://ai-python.dev/docs/ and read the listed local notes before writing code.

TaskPageLocal notes
Provider clients, options, discoverybasics/providers.md
Structured output, complex streamsbasics/streaming.md
Buffered language-model callsbasics/streaming.md
Images, video, speech, embeddings, transcription, rerankingbasics/model-operations.md
Events and serializationbasics/messages-and-events.md
Advanced tools, streaming, aggregationbasics/tools.mdstreaming-tools.md
Advanced agent behaviorbasics/agents.md
Deterministic model and agent testsbasics/testing.md
Subagents and multi-agentbasics/subagents-and-multi-agent.mdstreaming-tools.md
Custom agent loopsbasics/custom-loops.mdcustom-loops.md
Approvals and hooksbasics/human-in-the-loop.md
Serverless resumebasics/human-in-the-loop.mdserverless.md
Durable executionbasics/durable-execution.mddurable.md
Telemetry and tracingbasics/telemetry.md
AI SDK UI backendsbasics/ai-sdk-ui.mdui.md
Custom providersbasics/providers.mdcustom-provider.md

For exact APIs, use reference.md and the relevant reference/*.md page.

Thêm skills từ vercel

vercel
vercel
API REST Vercel được mô phỏng để phát triển và kiểm thử cục bộ. Sử dụng khi người dùng cần tương tác với các điểm cuối API Vercel tại chỗ, kiểm thử tích hợp Vercel,…
cron-jobs
vercel
Cấu hình và các phương pháp tốt nhất cho Vercel Cron Jobs. Sử dụng khi thêm, chỉnh sửa hoặc gỡ lỗi các tác vụ đã lên lịch trong vercel.json.
codegen
vercel
Các tiện ích tạo mã cho json-render. Sử dụng khi tạo mã từ đặc tả giao diện người dùng, xây dựng trình xuất mã tùy chỉnh, duyệt qua các đặc tả hoặc tuần tự hóa các thuộc tính cho…
next-best-practice
vercel
Các phương pháp hay nhất của Next.js - quy ước tệp, ranh giới RSC, mẫu dữ liệu, API bất đồng bộ, siêu dữ liệu, xử lý lỗi, trình xử lý tuyến đường, tối ưu hóa hình ảnh/phông chữ,…
benchmark-sandbox
vercel
Chạy các kịch bản đánh giá vercel-plugin trong Vercel Sandboxes thay vì các bảng WezTerm cục bộ. Cung cấp các microVM tạm thời với Claude Code và plugin được cài đặt sẵn,…
write-guide
vercel
Tạo một hướng dẫn kỹ thuật dạy một trường hợp sử dụng thực tế thông qua các ví dụ tiến dần. Các khái niệm chỉ được giới thiệu khi người đọc cần đến chúng.
benchmark-testing
vercel
Tạo và khởi chạy các dự án kiểm thử benchmark để thực thi việc tiêm kỹ năng vercel-plugin qua các kịch bản thực tế. Thiết lập các thư mục độc lập, cài đặt…
ai-gateway
vercel
Hướng