{"id":331,"library":"sniffio","title":"sniffio","description":"A Python library that detects which asynchronous I/O library (e.g., Trio, asyncio) is currently running in your code. Current version: 1.3.1, released on February 25, 2024. Maintained by the Trio project, it has a stable release cadence with updates approximately every 1-2 years.","status":"active","version":"1.3.1","language":"python","source_language":"en","source_url":"https://github.com/python-trio/sniffio","tags":["asyncio","Trio","asynchronous I/O","Python 3.7+"],"install":[{"cmd":"pip install sniffio","lang":"bash","label":"Install sniffio"}],"dependencies":[{"reason":"Required for Trio support","package":"trio"},{"reason":"Required for asyncio support","package":"asyncio"}],"imports":[{"note":"Ensure correct import path to access the function.","symbol":"current_async_library","correct":"from sniffio import current_async_library"}],"quickstart":{"code":"import sniffio\nimport trio\nimport asyncio\n\nasync def print_library():\n    library = sniffio.current_async_library()\n    print(f'This is {library}')\n\n# Prints 'This is trio'\ntrio.run(print_library)\n\n# Prints 'This is asyncio'\nasyncio.run(print_library())","lang":"python","description":"A simple script demonstrating how to use sniffio to detect the current async library and run a coroutine accordingly."},"warnings":[{"fix":"Upgrade to Python 3.7 or later.","message":"Dropped support for Python 3.5 and 3.6 in version 1.3.0","severity":"breaking","affected_versions":"1.3.0"},{"fix":"Remove explicit version specifications in USES=python.","message":"Explicit version specifications in USES=python for '3.x+' are deprecated","severity":"deprecated","affected_versions":"1.3.0"},{"fix":"Install the 'trio' package using pip: `pip install trio`.","message":"ModuleNotFoundError: No module named 'trio'. The 'trio' package is not installed.","severity":"breaking","affected_versions":"N/A"},{"fix":"Install the 'trio' package using pip (e.g., `pip install trio`) or ensure it's included in your project's dependencies.","message":"Module 'trio' not found. Ensure the 'trio' package is installed in the environment.","severity":"breaking","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-05-12T13:07:15.499Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"Ensure that calls to `sniffio.current_async_library()` are made within an `async` function that is itself executed by an asynchronous runner like `asyncio.run()` or `trio.run()`.\n\n```python\nimport sniffio\nimport asyncio\n\nasync def my_async_function():\n    print(f\"Current async library: {sniffio.current_async_library()}\")\n\nasyncio.run(my_async_function())\n```","cause":"You are attempting to call `sniffio.current_async_library()` (or a similar sniffio function) from a synchronous context, outside of an active Trio or asyncio event loop.","error":"RuntimeError: This function is only available from inside a Trio or asyncio task."},{"fix":"Install the `sniffio` package using pip.\n\n```bash\npip install sniffio\n```","cause":"The `sniffio` package has not been installed in your current Python environment.","error":"ModuleNotFoundError: No module named 'sniffio'"},{"fix":"Use the correct function name `sniffio.current_async_library()` to retrieve information about the running async library.\n\n```python\nimport sniffio\nimport asyncio\n\nasync def get_library_info():\n    lib = sniffio.current_async_library()\n    if lib:\n        print(f\"Detected: {lib.sniffio_name}\")\n    else:\n        print(\"No async library detected.\")\n\nasyncio.run(get_library_info())\n```","cause":"You are attempting to access a non-existent attribute or made a typo when trying to call the correct function to detect the current async library.","error":"AttributeError: module 'sniffio' has no attribute 'async_library'"},{"fix":"Always check if the return value of `sniffio.current_async_library()` is not `None` before attempting to access its attributes.\n\n```python\nimport sniffio\nimport asyncio\n\nasync def check_and_use_library():\n    detected_lib = sniffio.current_async_library()\n    if detected_lib:\n        print(f\"Running with: {detected_lib.sniffio_name}\")\n    else:\n        print(\"No async library is currently active.\")\n\nasyncio.run(check_and_use_library())\n```","cause":"This error occurs when `sniffio.current_async_library()` returns `None` (indicating no async event loop is running), and you then attempt to access an attribute like `sniffio_name` directly on that `None` value without checking if a library was actually detected.","error":"AttributeError: 'NoneType' object has no attribute 'sniffio_name'"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":0,"quickstart_tag":"stale","pypi_latest":null,"install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.6,"disk_size":"17.8M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.6,"disk_size":"18M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":0.8,"disk_size":"19.7M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":1,"disk_size":"20M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":0.8,"disk_size":"11.6M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":0.8,"disk_size":"12M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.9,"disk_size":"11.2M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"12M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.6,"disk_size":"17.3M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.6,"disk_size":"18M"}]},"quickstart_checks":{"last_tested":"2026-04-23","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}]}}