Skip to main content

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):
    if 'bytes' in event:
        # Bedrock sends bytes in the JSON payload
        print(event['bytes'].decode('utf-8'), 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.py for 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

sse_json_parser-0.1.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

sse_json_parser-0.1.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sse_json_parser-0.1.0.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

Hashes for sse_json_parser-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dc438f00fa0b0961ada49aba8c6f4b4e387b3a011e70401dfd9417c3a20dc684
MD5 6fd009c063ad5f154ac897472821cb2d
BLAKE2b-256 97ac273ee6cd341573ad6c34d85b0b685a352a43e84c467cf6b0ae26d1760547

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sse_json_parser-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cef77c63dc84b9f01bb150c4aa308047a9857bce76aede1dfb6812233c5d9f9e
MD5 0a6b8c17832c6b369220667bea3e0733
BLAKE2b-256 5bd5c5e186352600d7c1fe3f8d5d51ad28cd68e63a177c7907f8be801bc4fe10

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