Skip to main content

The AI Toolkit for Python

Project description

ai-sdk

A Python version of the AI SDK.

Quick Start

uv add ai-sdk
import ai_sdk as ai

llm = ai.openai.OpenAIModel(model="gpt-4", api_key="...")

async for msg in ai.execute(my_agent, llm, "Hello"):
    print(msg.text)

API Reference

@ai.tool

Decorator that turns an async function into a tool. Parameters are auto-extracted from type hints and docstrings become the tool description.

@ai.tool
async def get_weather(city: str, units: str = "celsius") -> str:
    """Get current weather for a city."""
    return f"72°F in {city}"

ai.stream_text(llm, messages, label=None)

Streams text from the LLM without tool support. Returns a Stream that can be awaited or async-iterated.

result = await ai.stream_text(llm, messages)
# or iterate for real-time updates
async for msg in ai.stream_text(llm, messages):
    print(msg.text_delta, end="")

ai.stream_loop(llm, messages, tools, label=None)

Streams LLM responses and automatically executes tool calls in a loop until complete. This is the main function for agentic workflows.

result = await ai.stream_loop(llm, messages, tools=[get_weather])

ai.execute(root_fn, *args)

Runs an agent function and yields all messages from nested streams. Use this as the top-level entry point for any agent workflow.

async def my_agent(llm, query):
    return await ai.stream_loop(llm, messages, tools)

async for msg in ai.execute(my_agent, llm, "What's the weather?"):
    print(msg)

ai.Message

Universal message type with role ("user", "assistant", "system") and parts. Access text via msg.text. The label field tags messages for multi-agent routing.

ai.TextPart, ai.ToolPart, ai.ReasoningPart

Message parts. TextPart holds text content. ToolPart contains tool invocation details and results. ReasoningPart holds model reasoning/thinking output.

MCP Integration

ai.mcp.get_http_tools(url, headers={}, tool_prefix="")

Connects to an MCP server over HTTP and returns tools. Optional tool_prefix namespaces tool names.

tools = await ai.mcp.get_http_tools(
    "https://mcp.example.com/mcp",
    headers={"API_KEY": "..."},
    tool_prefix="docs"
)

ai.mcp.get_stdio_tools(cmd, *args, tool_prefix="")

Spawns an MCP server process via stdio. Useful for local MCP servers like npx packages.

tools = await ai.mcp.get_stdio_tools(
    "npx", "-y", "@upstash/context7-mcp",
    "--api-key", os.environ["CONTEXT7_API_KEY"],
    tool_prefix="context7"
)

Multi-Agent Example

async def multiagent(llm, query):
    # Run two agents in parallel
    stream1, stream2 = await asyncio.gather(
        ai.stream_loop(llm, msgs1, tools=[add_one], label="agent1"),
        ai.stream_loop(llm, msgs2, tools=[multiply], label="agent2"),
    )

    # Combine results and summarize
    combined = stream1[-1].text + stream2[-1].text
    return await ai.stream_text(llm, make_messages(combined), label="summarizer")

async for msg in ai.execute(multiagent, llm, "10"):
    print(f"[{msg.label}] {msg.text_delta}", end="")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vercel_ai_sdk-0.0.1.dev1.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

vercel_ai_sdk-0.0.1.dev1-py3-none-any.whl (20.7 kB view details)

Uploaded Python 3

File details

Details for the file vercel_ai_sdk-0.0.1.dev1.tar.gz.

File metadata

  • Download URL: vercel_ai_sdk-0.0.1.dev1.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for vercel_ai_sdk-0.0.1.dev1.tar.gz
Algorithm Hash digest
SHA256 38626ff0e40906c6a45f0ae29f78347dade7c2cbd9cc82f410533067bb8fe14a
MD5 2edbe23ca474e8e7f553035dcf9253e1
BLAKE2b-256 e3f474a6a22484ef96c93aaf18f2e5ba9b8e3f4051af0b6367302a866c98dbff

See more details on using hashes here.

File details

Details for the file vercel_ai_sdk-0.0.1.dev1-py3-none-any.whl.

File metadata

  • Download URL: vercel_ai_sdk-0.0.1.dev1-py3-none-any.whl
  • Upload date:
  • Size: 20.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for vercel_ai_sdk-0.0.1.dev1-py3-none-any.whl
Algorithm Hash digest
SHA256 a73dcc08378a87298681ff0080ec218127b0f348809c8f4b583d9e96bc1235fb
MD5 471d5a793de488161a2678e2bcda6118
BLAKE2b-256 85464984d5b2bc087789b1d231c71cf603093cede80da17512dae1e4636e923f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page