sniffio
Sniff out which async library your code is running under
This package has a good security score with no known vulnerabilities.
Community Reviews
Minimal, reliable async runtime detection with no security overhead
In practice, this library introduces virtually zero attack surface. It has no dependencies, performs no network operations, handles no user input, and doesn't touch cryptography. The detection mechanism relies on inspecting stack frames and thread-local state, which is deterministic and side-effect free. Error handling is straightforward: it raises `AsyncLibraryNotFoundError` when not running under a recognized async context, making it easy to handle edge cases without information leakage.
From a supply chain perspective, this is as low-risk as dependencies get. The codebase is under 200 lines, making auditing trivial. It's a foundational utility used by major async libraries like AnyIO and httpx for compatibility layers, so it receives regular maintenance despite its simplicity.
Best for: Libraries building async-agnostic compatibility layers that need runtime detection without security concerns.
Avoid if: You need to detect custom or emerging async frameworks not yet supported by the library.
Simple, focused utility that does one thing reliably well
The library shines in its simplicity and type hint support. IDE autocompletion works perfectly, and the single function signature is immediately clear. Error handling is predictable: it raises `sniffio.AsyncLibraryNotFoundError` when called outside an async context, making it easy to catch and handle appropriately.
The main limitation is that it's really only useful for library authors who need to write async-framework-agnostic code. Application developers rarely need this—you typically know which framework you're using. Documentation is minimal but sufficient given the narrow scope. The package has remained stable for years with infrequent updates, which is actually a good sign for such a focused utility.
Best for: Library authors building async utilities that need to support multiple async frameworks (asyncio, trio, curio) transparently.
Avoid if: You're writing application code where you already know which async framework you're using—you won't need runtime detection.
Minimal, rock-solid async runtime detection with zero overhead
From an operations perspective, this is refreshingly bulletproof. No connection pools to manage, no timeouts to tune, no retry logic needed. It's pure runtime introspection with negligible memory footprint and effectively zero performance impact. The library uses thread-local state detection, so it works correctly even with multiple event loops in different threads. Error handling is straightforward: raises `sniffio.AsyncLibraryNotFoundError` if called outside an async context, which is exactly what you want for fast failure.
The main limitation is narrow scope - if you need more sophisticated async compatibility layers, you'll need additional tooling. But for its intended purpose of runtime detection in cross-compatible async libraries, it's the de facto standard and has remained stable across versions with no breaking changes in years.
Best for: Building async libraries that need to conditionally support multiple event loop implementations (asyncio, trio, curio).
Avoid if: You need actual async compatibility abstractions or protocol adapters rather than just runtime detection.
Sign in to write a review
Sign In