Skip to main content

transports.vercel_ai

Vercel AI SDK protocol adapter.

Implements the Vercel AI SDK protocol for agent-runtimes using Pydantic AI's built-in Vercel AI support from pydantic_ai.ui.vercel_ai.

Protocol Reference: https://ai.pydantic.dev/ui/vercel-ai/

The Vercel AI SDK protocol provides:

  • Streaming chat responses
  • Tool call support
  • Token usage tracking
  • Standard message format compatible with Vercel AI SDK

VercelAITransport Objects

class VercelAITransport(BaseTransport)

Vercel AI SDK protocol adapter.

Wraps Pydantic AI's built-in Vercel AI support to expose agents through the Vercel AI SDK protocol.

This adapter provides a FastAPI/Starlette compatible handler for the /api/chat endpoint that implements the Vercel AI SDK streaming protocol.

Example:

from pydantic_ai import Agent from agent_runtimes.agents import PydanticAIAgent from agent_runtimes.transports import VercelAITransport from fastapi import FastAPI, Request

Create Pydantic AI agent

pydantic_agent = Agent("openai:gpt-4o")

Wrap with agent adapter

agent = PydanticAIAgent(pydantic_agent)

Create Vercel AI adapter

vercel_adapter = VercelAITransport(agent)

Add to FastAPI app

app = FastAPI()

@app.post("/api/chat") async def chat(request: Request): return await vercel_adapter.handle_vercel_request(request)

__init__

def __init__(agent: BaseAgent,
usage_limits: UsageLimits | None = None,
toolsets: list[Any] | None = None,
builtin_tools: list[str] | None = None,
agent_id: str | None = None,
has_spec_frontend_tools: bool = False,
has_approval_tools: bool = False,
approval_tool_ids: list[str] | None = None,
is_triggered: bool = False)

Initialize the Vercel AI adapter.

Arguments:

  • agent - The agent to adapt.
  • usage_limits - Usage limits for the agent (tokens, tool calls).
  • toolsets - Additional toolsets (e.g., MCP servers).
  • builtin_tools - List of built-in tool names to expose.
  • agent_id - Agent ID for usage tracking.
  • has_spec_frontend_tools - Whether the agent spec declares frontend tools.
  • has_approval_tools - Whether the agent has runtime tools requiring approval.
  • approval_tool_ids - List of tool IDs that require human approval.
  • is_triggered - Whether the agent has a trigger config (output events are only emitted for triggered agents).

protocol_name

@property
def protocol_name() -> str

Get the protocol name.

handle_vercel_request

async def handle_vercel_request(request: Request,
model: str | None = None) -> Response

Handle a Vercel AI SDK request.

This method processes a Starlette/FastAPI request and returns a streaming response compatible with the Vercel AI SDK.

Arguments:

  • request - The Starlette/FastAPI request object.
  • model - Optional model override. If None, extracts from request body or uses the agent's default model.

Returns:

Starlette Response with streaming content.

handle_request

async def handle_request(request: dict[str, Any]) -> dict[str, Any]

Handle a direct request (not recommended for Vercel AI).

Note: Vercel AI is primarily a streaming protocol over HTTP. For proper integration, use handle_vercel_request() with a Starlette Request object.

Arguments:

  • request - Request data.

Returns:

Response data.

handle_stream

async def handle_stream(
request: dict[str, Any]) -> AsyncIterator[dict[str, Any]]

Handle a streaming request (not recommended for Vercel AI).

Note: Vercel AI uses HTTP streaming via Starlette Response. Use handle_vercel_request() instead.

Arguments:

  • request - Request data.

    Yields

    dict[str, Any] Stream events (not implemented - raises NotImplementedError).