{"id":641,"library":"httpx-sse","title":"httpx-sse","description":"httpx-sse is a Python library designed to consume Server-Sent Event (SSE) messages, integrating seamlessly with the HTTPX client. It provides both synchronous and asynchronous helpers to connect to SSE endpoints and iterate over the received events. Currently at version 0.4.3, the library is actively maintained with regular updates and fixes.","status":"active","version":"0.4.3","language":"python","source_language":"en","source_url":"https://github.com/florimondmanca/httpx-sse","tags":["http","sse","event-stream","httpx","client","async"],"install":[{"cmd":"pip install httpx-sse","lang":"bash","label":"Install latest stable version"}],"dependencies":[{"reason":"Core dependency for making HTTP requests. The library is built as an extension for HTTPX.","package":"httpx","optional":false}],"imports":[{"symbol":"connect_sse","correct":"from httpx_sse import connect_sse"},{"symbol":"aconnect_sse","correct":"from httpx_sse import aconnect_sse"},{"symbol":"ServerSentEvent","correct":"from httpx_sse import ServerSentEvent"}],"quickstart":{"code":"import asyncio\nimport httpx\nfrom httpx_sse import aconnect_sse, ServerSentEvent\nimport os\n\nasync def consume_sse_stream(url: str):\n    # Ensure a long enough timeout for SSE streams, or set to None\n    # if the stream is expected to be indefinite.\n    # httpx.AsyncClient(timeout=None) or httpx.Timeout(60.0, connect=5.0)\n    async with httpx.AsyncClient(timeout=None) as client:\n        try:\n            async with aconnect_sse(client, \"GET\", url) as event_source:\n                async for sse in event_source.aiter_sse():\n                    print(f\"Event: {sse.event}, Data: {sse.data}, ID: {sse.id}, Retry: {sse.retry}\")\n                    if sse.event == \"end\":\n                        break\n        except httpx.RequestError as exc:\n            print(f\"An error occurred while requesting {exc.request.url!r}: {exc}\")\n        except Exception as e:\n            print(f\"An unexpected error occurred: {e}\")\n\n# Example usage (replace with your actual SSE endpoint)\n# For local testing, you might run a simple SSE server.\nSSE_ENDPOINT_URL = os.environ.get(\"SSE_TEST_URL\", \"http://localhost:8000/sse\")\n\nif __name__ == \"__main__\":\n    print(f\"Connecting to SSE endpoint: {SSE_ENDPOINT_URL}\")\n    asyncio.run(consume_sse_stream(SSE_ENDPOINT_URL))\n","lang":"python","description":"Demonstrates how to establish an asynchronous connection to an SSE endpoint using `aconnect_sse` and iterate through incoming `ServerSentEvent` objects. It includes error handling and a common pattern for managing HTTPX client timeouts, which is crucial for long-lived SSE connections."},"warnings":[{"fix":"Upgrade to Python 3.8 or newer. The library requires Python >=3.9 since 0.4.0.","message":"Python 3.7 support was officially dropped as it reached End-of-Life.","severity":"breaking","affected_versions":"0.4.0 and later"},{"fix":"If your code relied on `SSEError` being raised immediately during connection, you might need to adjust error handling to occur during iteration, or explicitly check `event_source.response.headers['Content-Type']` before iterating.","message":"The timing of `SSEError` (for non `text/event-stream` Content-Type) was moved from `connect_sse()` to `iter_sse()`/`aiter_sse()`. This allows inspecting the raw response before content type validation.","severity":"gotcha","affected_versions":"0.3.0 and later"},{"fix":"Ensure your SSE server emits events strictly according to the SSE specification to avoid unexpected parsing behavior or silent data loss. Update to `0.4.2` or later for correct parsing.","message":"Incorrect newline parsing that was not compliant with the SSE specification was fixed. If you were working around previous parsing quirks, your code might need adjustment.","severity":"gotcha","affected_versions":"0.4.2"},{"fix":"If you experienced performance degradation after upgrading to `0.4.2`, update to `0.4.3` or newer to resolve the issue.","message":"A performance issue was introduced in `0.4.2` due to improved line parsing, which was subsequently fixed.","severity":"gotcha","affected_versions":"0.4.2"},{"fix":"Install `httpx` using `pip install httpx`.","message":"The `httpx` library is a required dependency. Failure to install it will result in a `ModuleNotFoundError`.","severity":"breaking","affected_versions":"all versions"},{"fix":"Ensure `httpx` is installed in your environment by running `pip install httpx`. If you are installing the main library, ensure its dependencies are correctly resolved (e.g., install it within a virtual environment or avoid `pip install --no-deps`).","message":"A required dependency, `httpx`, was not found. This error occurs when the `httpx` library is not installed in the execution environment, preventing the application from running.","severity":"breaking","affected_versions":"all versions"}],"env_vars":null,"last_verified":"2026-05-12T17:06:53.770Z","next_check":"2026-06-28T00:00:00.000Z","problems":[{"fix":"Upgrade `httpx-sse` to version 0.4.3 or newer, or pin your `httpx` dependency to a version older than 0.28.0. The `httpx-sse` library version 0.4.3 and above addresses this compatibility issue.\n\n`pip install --upgrade httpx-sse`\n\nor\n\n`pip install httpx==0.27.*`","cause":"This error occurs when `httpx-sse` is used with a version of `httpx` (>=0.28.0) that has removed or renamed `httpx.TransportError`. Older versions of `httpx-sse` (prior to 0.4.3) subclass `httpx.TransportError`, leading to this `AttributeError` when `httpx` is updated.","error":"AttributeError: module 'httpx' has no attribute 'TransportError'"},{"fix":"Ensure that the SSE endpoint on your server correctly sets the `Content-Type` header to `text/event-stream` for SSE responses. If you control the server, modify the response headers. If you don't, you might be connecting to a non-SSE endpoint or one that's misconfigured.","cause":"This error indicates that the server is not sending events with the correct `Content-Type` header, which should be `text/event-stream` as per the Server-Sent Events specification. `httpx-sse` explicitly checks for this header and raises an `SSEError` if it's incorrect.","error":"httpx_sse.SSEError: Expected Content-Type header to be 'text/event-stream', got 'application/json'"},{"fix":"Implement custom reconnection logic in your client code to gracefully handle `httpx.ReadError`. You can use the `sse.id` and `sse.retry` values provided by `httpx-sse` to resume the event stream. Libraries like `stamina` can assist with exponential backoff and retries.","cause":"This error occurs when the connection to the SSE server is unexpectedly closed or breaks while `httpx-sse` is attempting to read data. This is a common network-related issue and typically indicates the server closed the connection, or there was an intermediary network problem. `httpx-sse` does not have built-in reconnection logic and will raise this `httpx.ReadError` when the connection is interrupted.","error":"httpx.ReadError: [Errno 104] Connection reset by peer"},{"fix":"Install the `sse-starlette` library using pip to resolve the missing module.\n\n`pip install sse-starlette`","cause":"This error typically occurs when running examples or tests provided with `httpx-sse` that depend on `sse-starlette`, but `sse-starlette` has not been installed. While `httpx-sse` itself doesn't strictly require `sse-starlette` for basic operation, many examples demonstrating server-side SSE generation or testing setups use it.","error":"ModuleNotFoundError: No module named 'sse_starlette'"}],"ecosystem":"pypi","meta_description":null,"install_score":0,"install_tag":"stale","quickstart_score":0,"quickstart_tag":"stale","pypi_latest":"0.4.3","install_checks":{"last_tested":"2026-05-12","tag":"stale","tag_description":"widespread failures or data too old to trust","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"17.8M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.5,"import_time_s":null,"mem_mb":null,"disk_size":"18M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"19.7M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.6,"import_time_s":null,"mem_mb":null,"disk_size":"20M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"11.6M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.4,"import_time_s":null,"mem_mb":null,"disk_size":"12M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"11.3M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.4,"import_time_s":null,"mem_mb":null,"disk_size":"12M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"17.3M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.7,"import_time_s":null,"mem_mb":null,"disk_size":"18M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null}]},"quickstart_checks":{"last_tested":"2026-04-24","tag":"stale","tag_description":"widespread failures or data too old to trust","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}}