Skip to main content

A Python implementation of the Vercel AI Data Stream protocol

Project description

AI Data Stream

This package contains an implementation of the Vercel AI SDK's "Data Stream Protocol".

Note: This is not an official package by Vercel, but rather a community-driven implementation of the protocol for use with python frameworks, it is not affiliated nor endorsed by Vercel. All credit for the protocol design and specification belongs to Vercel. This implementation exists solely to empower developers with additional framework options, with no intention of competing with Vercel's offerings.

Data Stream Protocol

The Data Stream Protocol is a protocol for streaming ai chat response to the client. The protocol is described in the Vercel AI SDK documentation.

The basic idea is that the server sends a stream of messages to the client, in the following format:

{type}:{data}
  • type is a single character that describes the type of the message.
  • data is a json-serialized object that contains the message data.

An example of a full stream can look something like this:

f:{"messageId": "123"}
0:"Hello, i am a help"
0:"full Assistant that can ch"
0:"eck the weather, let me do that"
9:{"toolCallId": "tool-123", "name": "get_weather", "args": {"location": "San Francisco"}}
a:{"toolCallId": "tool-123", "result": "The weather in San Francisco is sunny"}
e:{"finishReason": "tool-calls", "usage": {"promptTokens": 10, "completionTokens": 20}, "isContinued": false}
f:{"messageId": "456"}
0:"The weather in San Francisco is sunny"
e:{"finishReason": "stop", "usage": {"promptTokens": 10, "completionTokens": 20}, "isContinued": false}
d:{"finishReason": "stop", "usage": {"promptTokens": 10, "completionTokens": 20}}

Installation

pip install ai-datastream

Usage

Example usage of usage with FastAPI and LangGraph:

from fastapi import FastAPI
from langgraph.graph import StateGraph
from langgraph.prebuilt import create_react_agent
from langchain_openai import ChatOpenAI

from ai_datastream.api.fastapi import AiChatDataStreamAsyncResponse, FastApiDataStreamRequest
from ai_datastream.agent.langgraph import LanggraphStreamer


app = FastAPI()

@app.post("/ai/chat")
async def chat(request: FastApiDataStreamRequest):
    model = ChatOpenAI(model="gpt-4o")
    tools = [...]  # your tools
    prompt = "You are a helpful assistant."
    agent = create_react_agent(model, tools)
    streamer = LanggraphStreamer(agent)
    return AiChatDataStreamAsyncResponse(streamer, prompt, request.messages)

Then, in your frontend, you can use the useChat hook with the /ai/chat endpoint:

const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat({
  api: "http://your-api-server/ai/chat",
});

Supported Frameworks

Agent Frameworks

API Frameworks

Contributing

Contributions are welcome! Please see the CONTRIBUTING.md file for details.

Development

Setup

  1. Install Poetry (version 2.1.2):
curl -sSL https://install.python-poetry.org | python3 - --version 2.1.2
  1. Install dependencies:
poetry install --with dev
  1. Install pre-commit hooks:
poetry run pre-commit install

Pre-commit Hooks

This project uses pre-commit hooks to ensure code quality. The hooks run automatically on each commit and include:

  • Code formatting and linting (ruff)
  • Type checking (mypy)
  • Basic file checks (trailing whitespace, file endings, etc.)

To run the hooks manually:

poetry run pre-commit run --all-files

Testing

Run the test suite:

poetry run pytest

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

ai_datastream-0.1.0.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

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

ai_datastream-0.1.0-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file ai_datastream-0.1.0.tar.gz.

File metadata

  • Download URL: ai_datastream-0.1.0.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.11.4 Darwin/24.5.0

File hashes

Hashes for ai_datastream-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3187b8cebf6ddaf06d7775a0f4d0eed275d73bbfaab6c89ab61f633ff4a78c9a
MD5 2212caf40d020f9d57a148eeb6df9918
BLAKE2b-256 ed1231f0b46f0fa584eb5e896a3c666060ad4848856675c37278da8a6d4db484

See more details on using hashes here.

File details

Details for the file ai_datastream-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ai_datastream-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.11.4 Darwin/24.5.0

File hashes

Hashes for ai_datastream-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d96738a906416c2f7097703974a9b174cbec8b2b067e0d9b9c94e0570d022036
MD5 b213024d17d8b2556539f77048501b2d
BLAKE2b-256 dc510d7fe0c4a54df0e394738dbf12ea8ec0d84501cd4892dc2dbe3f443cd5c5

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