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}
typeis a single character that describes the type of the message.datais 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
- Install Poetry (version 2.1.2):
curl -sSL https://install.python-poetry.org | python3 - --version 2.1.2
- Install dependencies:
poetry install --with dev
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ai_datastream-0.1.1.tar.gz.
File metadata
- Download URL: ai_datastream-0.1.1.tar.gz
- Upload date:
- Size: 12.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.2 CPython/3.11.4 Darwin/24.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d12c2a7a7bafde435f802ce0bbe1d20ed4908296c9c427c30bf1e6df5cfbcbb2
|
|
| MD5 |
135393c1df90cb612095e07a85e88a5f
|
|
| BLAKE2b-256 |
9c4107a6edbf3ab55baf976d05834a05f4809e3969782d40f21b9b88e16cc7f8
|
File details
Details for the file ai_datastream-0.1.1-py3-none-any.whl.
File metadata
- Download URL: ai_datastream-0.1.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e1930d4b9244224d7e523e14e884e0de964d9c23ebad707e08fc3fccac3b46f5
|
|
| MD5 |
de7959ed90aef59ed8499a96c25b3339
|
|
| BLAKE2b-256 |
3650724e12dd8eb5713d36c5862bb953b14d16d71e8c69ca1da706216c967e48
|