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'], 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.1.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.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sse_json_parser-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 3da85406770c8ad05f74906cbbf7da48f3dc1b5a5e3822b3d8da608bd3a0f4ff
MD5 53197446461fa38bb9a1d8221885a090
BLAKE2b-256 bc2b69b159720bcfe81efabc6f5effbbd49bb6d90850e9882bf8693e17de27c2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sse_json_parser-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 98370d5bd256247b59afbe3e604f711b58543d7bdaaded5c5c68f36c7f27e1e7
MD5 e886475bb43b3db633a07f6a82123498
BLAKE2b-256 defac1f0f19e1bfb87ebc26a6eebeffa06c3a0ae712634296a9eefaab314b802

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