Registry / http-networking / sseclient-py

sseclient-py

JSON →
library1.9.0pypypi✓ verified 30d ago

sseclient-py is a Python library providing a client for Server-Sent Events (SSE). It simplifies consuming event streams by parsing the SSE protocol over HTTP. The current stable version is 1.9.0, with releases typically focused on bug fixes and feature additions rather than frequent breaking changes.

pip install sseclient-py
INSTALL
IMPORT
SIG · SSECLIENT-PY
S
sseclient-py
http-networkingpythonv1.9.0
Install
1.5s avg
Import
19ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v1.9.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.020s · 17.8MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 1.5s · import 0.018s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SSEClient
✓ from sseclient import SSEClient

Connects to an SSE stream using the `sseclient-py` library with `requests`. It demonstrates how to initialize the client and iterate over incoming events, highlighting the crucial `stream=True` parameter for `requests` to ensure proper streaming behavior.

import sseclient import requests import os # Replace with your SSE stream URL (or set SSE_STREAM_URL environment variable) stream_url = os.environ.get('SSE_STREAM_URL', 'http://localhost:8000/stream') try: # IMPORTANT: stream=True is crucial for long-lived connections. # The sseclient-py library expects an iterable response body, # which requests.get(..., stream=True) provides. response = requests.get(stream_url, stream=True, timeout=30) response.raise_for_status() # Raise an exception for HTTP errors (4xx or 5xx) client = sseclient.SSEClient(response) print(f"Connected to SSE stream: {stream_url}") for event in client.events(): print(f"Event: id={event.id}, event={event.event}, data={event.data}") except requests.exceptions.RequestException as e: print(f"Request failed: {e}") except Exception as e: print(f"An unexpected error occurred: {e}")
Debug
Known issues
gotchaWhen using `requests` with `sseclient-py`, you MUST pass `stream=True` to `requests.get()`. Failing to do so will cause `requests` to download the entire stream before `sseclient-py` can process any events, leading to memory issues and effectively blocking real-time processing.
fix
Ensure your `requests.get()` call includes the `stream=True` parameter, e.g., `requests.get(url, stream=True)`.
affects: All versions
gotchaThe `sseclient-py` library does not automatically handle reconnections or retries after a connection drops (e.g., due to network issues or server restart). Your application code needs to implement this logic for resilient SSE consumption.
fix
Wrap the SSE consumption loop in a retry mechanism (e.g., a `while True` loop with `try...except` and a backoff delay) to re-establish the connection upon failure.
affects: All versions
gotchaThe library assumes the incoming data strictly adheres to the Server-Sent Events specification. Malformed or non-SSE data received from the server may lead to parsing errors, incomplete events, or unexpected behavior without clear warnings.
fix
Ensure the SSE server provides well-formed events. Implement robust error handling around event processing, and consider logging raw data for debugging malformed streams.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'sseclient'
The `sseclient-py` library, which provides the `sseclient` module, has not been installed in the current Python environment.
fix
Install the library using pip:
```bash
pip install sseclient-py
```
TypeError: 'str' object has no attribute 'iter_content'
This error occurs when a URL string is directly passed to `sseclient.SSEClient`, but the client expects a file-like object or a `requests.Response` object (which has `iter_content` or `iter_lines` methods).
fix
Use the `requests` library to make an HTTP GET request to the SSE endpoint and pass the resulting `requests.Response` object to `sseclient.SSEClient`:
```python
import requests
import sseclient

response = requests.get('http://example.com/sse', stream=True)
client = sseclient.SSEClient(response)
```
Application hangs or consumes excessive memory without receiving events, especially for long-running streams (no specific exception raised)
When using `sseclient-py` with the `requests` library, the `stream=True` parameter must be passed to `requests.get()` to ensure the response body is streamed rather than downloaded entirely, preventing memory exhaustion or indefinite hanging for continuous SSE streams.
fix
Always include `stream=True` in your `requests.get()` call when consuming an SSE stream:
```python
import requests
import sseclient

response = requests.get('http://example.com/sse', stream=True)
client = sseclient.SSEClient(response)
for event in client.events():
    print(event.data)
```
AttributeError: 'Event' object has no attribute 'json'
The `sseclient.Event` object, returned when iterating over events, does not have a `json()` method like a `requests.Response` object. The event's data is directly accessible via the `data` attribute.
fix
Access the event's payload using `event.data`. If the data is JSON, parse it explicitly using `json.loads()`:
```python
import json
import requests
import sseclient

response = requests.get('http://example.com/sse', stream=True)
client = sseclient.SSEClient(response)

for event in client.events():
    if event.data:
        try:
            parsed_data = json.loads(event.data)
            print('Parsed JSON data:', parsed_data)
        except json.JSONDecodeError:
            print('Raw data:', event.data)
```
Upgrade
Version history
1.9.0latest on PyPI · released Jan 2, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
18 hits · last 30 days
node
14
OpenAI (training)
1
Resources
sseclient-py — pip install sseclient-py · libregistry