A lightweight SSE stream parser for extracting JSON payloads.
Project description
SSE JSON Parser
A lightweight, robust Python library for parsing Server-Sent Events (SSE) streams, specifically optimized for AI Agents and LLM Streaming.
Why use this for Agents?
When building AI Agents (using LangChain, LangGraph, or AWS Bedrock), responses are often streamed to the client to reduce latency (Time to First Byte).
However, these streams are often fragmented when they travel over the network. A single JSON object representing a token or a tool call might be split across multiple chunks:
Chunk 1: data: {"type": "content_
Chunk 2: block_delta", "delta": {"text": "Hello W
Chunk 3: orld"}}
A standard JSON parser will fail on Chunk 1. sse-json-parser buffers these chunks and only yields when a complete, valid JSON event is available.
It handles:
- Token Streaming: reconstructing split text deltas.
- Tool Calls: buffering large tool input JSONs until they are complete.
- Multi-byte Characters: correctly handling emojis or unicode characters split across chunks.
Installation
pip install sse-json-parser
(Or install locally: pip install .)
Usage
1. With LangGraph / LangChain (Agent Streaming)
This is the primary use case. If you are serving a LangGraph agent via a streaming endpoint (like FastAPI), the client needs to reconstruct the events.
import sys
from sse_json_parser import SSEParser
# 'sse_byte_stream' is your network response iterator (e.g. requests.iter_content)
parser = SSEParser()
print("Agent Response:")
for event in parser.parse(sse_byte_stream):
# 'event' is a fully parsed dict
if event.get('type') == 'token':
# Print tokens in real-time as a typewriter effect
sys.stdout.write(event['content'])
sys.stdout.flush()
elif event.get('type') == 'tool_use':
print(f"\n[Agent is using tool: {event['name']}]")
2. With AWS Boto3 (Amazon Bedrock)
Ideal for consuming streaming responses from AWS Bedrock, which often sends split JSON frames.
import boto3
from sse_json_parser import SSEParser, BotoStreamAdapter
client = boto3.client('bedrock-runtime')
response = client.invoke_model_with_response_stream(...)
# Adapter handles the specific Boto3 EventStream structure automatically
adapter = BotoStreamAdapter(response['body'])
parser = SSEParser()
for event in parser.parse(adapter):
# 'event' is the dictionary returned by the API
if 'chunk' in event:
print(event['chunk'].get('bytes', b''), end='')
elif 'completion' in event:
print(event['completion'], end='')
3. Basic Usage
from sse_json_parser import SSEParser
stream = [b'data: {"foo": "bar"}\n\n', b'data: {"baz": "qux"}\n\n']
parser = SSEParser()
for event in parser.parse(stream):
print(event)
Development
- Run Tests:
python3 -m unittest discover - Agent Demo: See
langgraph_agent.pyfor a full OpenAI integration example.
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
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 sse_json_parser-0.1.3.tar.gz.
File metadata
- Download URL: sse_json_parser-0.1.3.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f0d53541c7c5dea9bd59b3c24dd6b0f3eed92b6a627ac139ed6c663d4670b63
|
|
| MD5 |
5573615c3c84533a6ecd0a1d47b0993a
|
|
| BLAKE2b-256 |
ac9593db1caaa1992f30aabda2093755bbf0613e74b4ca1114a815c7947fd733
|
File details
Details for the file sse_json_parser-0.1.3-py3-none-any.whl.
File metadata
- Download URL: sse_json_parser-0.1.3-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7064cd4a1199b4683cecb658be9c099c7c829346c3ab2b371b1241a278c40a91
|
|
| MD5 |
dfc427f387f8eeb5f68bf716352d7e6a
|
|
| BLAKE2b-256 |
4365a35736bc7eff3743e047b9c91949a4ca06c48fc2bc7f30dc6320282cef42
|