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
Metadata
Release files for ai-datastream 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ai_datastream-0.1.1.tar.gz | 12.6 kB | Without provenance | Details for ai_datastream-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ai_datastream-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for ai_datastream-0.1.1-py3-none-any.whl |
Total release size: 29.0 kB
Release files / ai_datastream-0.1.1.tar.gz
| Download URL | ai_datastream-0.1.1.tar.gz |
|---|---|
| Size | 12.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d12c2a7a7bafde435f802ce0bbe1d20ed4908296c9c427c30bf1e6df5cfbcbb2
|
|
BLAKE2b-256 checksum How to use checksums |
9c4107a6edbf3ab55baf976d05834a05f4809e3969782d40f21b9b88e16cc7f8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.2 CPython/3.11.4 Darwin/24.5.0
|
Release files / ai_datastream-0.1.1-py3-none-any.whl
| Download URL | ai_datastream-0.1.1-py3-none-any.whl |
|---|---|
| Size | 16.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e1930d4b9244224d7e523e14e884e0de964d9c23ebad707e08fc3fccac3b46f5
|
|
BLAKE2b-256 checksum How to use checksums |
3650724e12dd8eb5713d36c5862bb953b14d16d71e8c69ca1da706216c967e48
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.2 CPython/3.11.4 Darwin/24.5.0
|
Security for ai-datastream 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.