{"id":1727,"library":"sseclient-py","title":"SSE Client for Python","description":"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.","status":"active","version":"1.9.0","language":"en","source_language":"en","source_url":"https://github.com/btubbs/sseclient","tags":["sse","server-sent events","client","streaming","http"],"install":[{"cmd":"pip install sseclient-py","lang":"bash","label":"Install stable"}],"dependencies":[],"imports":[{"symbol":"SSEClient","correct":"from sseclient import SSEClient"}],"quickstart":{"code":"import sseclient\nimport requests\nimport os\n\n# Replace with your SSE stream URL (or set SSE_STREAM_URL environment variable)\nstream_url = os.environ.get('SSE_STREAM_URL', 'http://localhost:8000/stream')\n\ntry:\n    # IMPORTANT: stream=True is crucial for long-lived connections.\n    # The sseclient-py library expects an iterable response body,\n    # which requests.get(..., stream=True) provides.\n    response = requests.get(stream_url, stream=True, timeout=30)\n    response.raise_for_status() # Raise an exception for HTTP errors (4xx or 5xx)\n\n    client = sseclient.SSEClient(response)\n\n    print(f\"Connected to SSE stream: {stream_url}\")\n    for event in client.events():\n        print(f\"Event: id={event.id}, event={event.event}, data={event.data}\")\nexcept requests.exceptions.RequestException as e:\n    print(f\"Request failed: {e}\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")","lang":"python","description":"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."},"warnings":[{"fix":"Ensure your `requests.get()` call includes the `stream=True` parameter, e.g., `requests.get(url, stream=True)`.","message":"When 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"The `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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}