---
title: AI SDK for Python
product: vercel
url: /docs/ai-gateway/sdks-and-apis/ai-sdk-python
canonical_url: "https://vercel.com/docs/ai-gateway/sdks-and-apis/ai-sdk-python"
last_updated: 2026-07-30
type: integration
prerequisites:
  - /docs/ai-gateway/sdks-and-apis
  - /docs/ai-gateway
related:
  - /docs/ai-gateway/authentication-and-byok/oidc
  - /docs/ai-gateway/authentication-and-byok
  - /docs/ai-gateway/models-and-providers/provider-options
  - /docs/ai-gateway/sdks-and-apis/ai-sdk
summary: Build AI-powered Python applications using the AI SDK for Python with AI Gateway for unified access to 200+ models.
install_vercel_plugin: npx plugins add vercel/vercel-plugin
---

# AI SDK for Python

The [AI SDK for Python](https://ai-python.dev) is a toolkit for building LLM-powered applications and agents in Python.
It uses AI Gateway to route requests by default.


<!-- docsgraph:related -->
## Related pages

> **For AI agents:** Follow these links to understand how this page connects to the rest of the Vercel ecosystem. For the full cross-link map (inbound, outbound, prerequisites, and semantic neighbors), see the .graph.md link below.

- [How to build AI Agents with Vercel and the AI SDK](https://vercel.com/kb/guide/how-to-build-ai-agents-with-vercel-and-the-ai-sdk?from=related) — Learn how to build, deploy, and scale AI agents on Vercel using the AI SDK. This guide covers calling LLMs, defining too
- [Building AI apps on Vercel: an overview](https://vercel.com/kb/guide/how-to-build-ai-app?from=related) — Learn the key AI concepts and tools for building and scaling AI apps.
- [Python](https://workflow-sdk.dev/docs/getting-started/python?from=related) — Set up the Workflow Python SDK in your Python application.
- [How to build an AI agent for Slack with Chat SDK and AI SDK](https://vercel.com/kb/guide/how-to-build-an-ai-agent-for-slack-with-chat-sdk-and-ai-sdk?from=related) — Build a Slack AI agent using Chat SDK, AI SDK's ToolLoopAgent, and Vercel AI Gateway. Covers project setup, tool definit
- [Run Python code securely with AI SDK and Vercel Sandbox](https://vercel.com/kb/guide/python-ai-sdk-vercel-sandbox?from=related) — Add an \\`executeCode\\` tool to your AI SDK agent with the \\`ai-sdk-tool-code-execution\\` package to run Python 3.13 insi
- [AI SDK](https://vercel.com/docs/ai-sdk?from=related) — TypeScript toolkit for building AI-powered applications with React, Next.js, Vue, Svelte and Node.js
- [Pydantic AI](https://vercel.com/docs/ai-gateway/ecosystem/framework-integrations/pydantic-ai?from=related) — Learn how to integrate Vercel AI Gateway with Pydantic AI to access multiple AI models through a unified interface
- [OpenAI](https://vercel.com/docs/agent-resources/integrations-for-models/openai?from=related) — Integrate your Vercel project with OpenAI's powerful suite of models.
- [Integrations for Models](https://vercel.com/docs/agent-resources/integrations-for-models?from=related) — Integrate powerful AI services and models seamlessly into your Vercel projects.
- [Ecosystem](https://vercel.com/docs/ai-gateway/ecosystem?from=related) — Explore community framework integrations and ecosystem features for the AI Gateway.

Full cross-link map for this page: [/docs/ai-gateway/sdks-and-apis/ai-sdk-python.graph.md](/docs/ai-gateway/sdks-and-apis/ai-sdk-python.graph.md)
<!-- /docsgraph:related -->

> **💡 Note:** The AI SDK for Python is in public beta.

## Installation

The SDK requires Python 3.12 or later. Install the `ai` package:

#### uv

```bash filename="Terminal"
uv add ai
```

#### pip

```bash filename="Terminal"
pip install ai
```

## Quick start

Create a model with a string model ID and pass it to `ai.stream` to get a streaming response.
You can omit the provider prefix or use `gateway:` to route through AI Gateway.

```python filename="quickstart.py"
import asyncio
import ai

async def main() -> None:
    model = ai.get_model('anthropic/claude-sonnet-4.6')
    messages = [ai.user_message('Explain quantum computing in one paragraph.')]

    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)

asyncio.run(main())
```

Responses always stream. After iteration, the full response is available on `stream.text`, and token counts on `stream.usage`.

## Structured outputs

Pass a [Pydantic](https://pydantic.dev/docs/validation/latest/concepts/models/) model as `output_type` to get validated,
structured data. After the stream finishes, `stream.output` returns an instance of your model:

```python filename="structured.py"
import asyncio
import ai
import pydantic

class Person(pydantic.BaseModel):
    name: str
    age: int
    city: str

async def main() -> None:
    model = ai.get_model('anthropic/claude-sonnet-4.6')
    messages = [
        ai.user_message('Extract: John is 30 years old and lives in NYC.')
    ]

    async with ai.stream(model, messages, output_type=Person) as stream:
        async for event in stream:
            pass

    print(stream.output)  # Person(name='John', age=30, city='NYC')

asyncio.run(main())
```

## Tool calling with agents

Define tools as Python functions with the `@ai.tool` decorator.
An `ai.Agent` runs a loop: it streams model output and executes requested tools until the model returns a final answer.

```python filename="agent.py"
import asyncio
import ai

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

async def main() -> None:
    model = ai.get_model('anthropic/claude-sonnet-4.6')
    agent = ai.Agent(tools=[get_weather])
    messages = [ai.user_message("What's the weather in Tokyo?")]

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

    history = stream.messages  # Full conversation, including tool results

asyncio.run(main())
```

The function signature and docstring become the tool schema. The model sees the tool name, description, and typed parameters.

## Authentication

The SDK can use `AI_GATEWAY_API_KEY` environment variable to authenticate:

```bash filename="Terminal"
export AI_GATEWAY_API_KEY="your_ai_gateway_api_key"
```

When deploying to Vercel or using `vercel dev` for local development, you are encouraged to use [OIDC](/docs/ai-gateway/authentication-and-byok/oidc) instead of an API key.
Install the `vercel` extra and the SDK will handle authentication automatically:

```bash filename="Terminal"
uv add "ai[vercel]"
```

See [Authentication](/docs/ai-gateway/authentication-and-byok) for more details.

## Next steps

- Explore the full [AI SDK for Python documentation](https://ai-python.dev) for agents, subagents, human-in-the-loop hooks, and custom loops
- Browse [examples on GitHub](https://github.com/vercel-labs/ai-python/tree/main/examples), from single-file scripts to end-to-end demos
- Learn about [model routing and fallbacks](/docs/ai-gateway/models-and-providers/provider-options)
- Building with TypeScript? See the [AI SDK](/docs/ai-gateway/sdks-and-apis/ai-sdk) page


---

[View full sitemap](/docs/sitemap)
