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):
    # 'event' is the dictionary returned by the API
    if 'chunk' in event:
         print(event['chunk'].get('bytes', b'').decode('utf-8'), 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.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.2.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.2-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sse_json_parser-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 7af3cc7f0fe07d580ba361bd3a4bc0b44fc4674d23a3c63f0f157fa72a00125e
MD5 c62c89e806ecd1c8d3bced6736441d37
BLAKE2b-256 6e56af2626eb48850c40608719aa7989ba6f0d6283d2e955f3f275c9900bd5a0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sse_json_parser-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f220069fd6eccde9e2dff4f4477651c32042ce684eaf3c33b4862ee091dc9c21
MD5 0fe328c32955119f2f634c6ba4b3d610
BLAKE2b-256 8b0d5f56d8001ad2d4d679a5fb6226eab25a997d68f89e8284295b2db5723eae

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