sniffio

A tiny Python library that detects which async framework (trio, asyncio, or curio) your code is currently running under.

Library
PyPI
v1.3.1
154stars
MIT OR Apache-2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
38/100Needs Attention
Development Activity4
Maintenance0
Community68
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
73/100Good
Architecture70
Code Quality75
Innovation55
Learning Curve90

sniffio solves a narrow but recurring problem for library authors who want to support multiple async I/O frameworks without hard-coding a dependency on any one of them. Calling current_async_library() from inside a coroutine returns a string identifying the active runtime, letting a library branch its implementation accordingly instead of forcing callers into a single async stack.

The detection order is deliberate: it first checks a contextvars.ContextVar and a thread-local override (used by frameworks like Trio and Trio-asyncio to explicitly declare themselves), then falls back to sniffing sys.modules for a running asyncio task, and finally checks for a running Curio loop. If none of these match, it raises AsyncLibraryNotFoundError rather than guessing.

Maintained by the Trio project as a shared utility for the async Python ecosystem, sniffio has become a de facto standard dependency pulled in transitively by major HTTP and networking libraries (httpx, anyio, and others) that need to stay framework-agnostic.

What You Get

  • A single current_async_library() function that returns a string ("trio", "asyncio", or "curio") identifying the running async framework
  • An explicit opt-in mechanism via current_async_library_cvar (a ContextVar) and thread_local.name, so frameworks can declare their identity directly rather than relying on sniffing
  • Automatic fallback detection for asyncio (via asyncio.current_task()) and curio (via curio.meta.curio_running()) when no explicit declaration is present
  • A dedicated AsyncLibraryNotFoundError exception raised when called outside any recognized async context, instead of returning None or a wrong guess
  • Zero runtime dependencies and a py.typed marker for full static type-checking support

Common Use Cases

  • HTTP and networking libraries (like httpx and anyio) that expose a single async API surface but need to dispatch to trio- or asyncio-specific primitives internally
  • Async compatibility shims that provide a generic_sleep()-style helper and need to know which underlying sleep implementation to call
  • Testing utilities that must detect the active event loop implementation before setting up fixtures
  • Any library author who wants to support Trio and asyncio users from one codebase without forcing a dependency on either at import time

Under The Hood

Architecture The entire runtime behavior lives in sniffio/_impl.py, a single module with no internal layering: a module-level ContextVar (current_async_library_cvar) and a threading.local subclass (_ThreadLocal) hold explicit declarations, and current_async_library() checks them in a fixed priority order (thread-local, then contextvar, then asyncio sniffing via sys.modules and asyncio.current_task(), then curio sniffing via curio.meta.curio_running()) before raising AsyncLibraryNotFoundError. There is no dependency injection or configuration surface; the public API is exactly one function plus the two state objects frameworks use to opt in explicitly, so the only thing that would break by changing the core abstraction is the detection order itself.

Tech Stack Pure standard-library Python: contextvars.ContextVar, threading.local, and sys.modules introspection, with zero runtime dependencies declared in pyproject.toml. The package is built with setuptools (using setuptools_scm for version derivation from git tags) and requires Python 3.10+. Test and docs tooling is isolated into [dependency-groups] (pytest, pytest-cov, curio for tests; sphinx + sphinx_rtd_theme for docs), and .mypy.ini configures strict-ish static typing with targeted ignore_missing_imports for the untyped curio and pytest packages.

Code Quality Testing uses pytest with pytest-cov for coverage, and _tests/test_sniffio.py covers all four detection paths explicitly (contextvar override, thread-local override, live asyncio task, live curio task) plus the not-found error case, including a second call per branch to exercise caching behavior. Error handling is intentional rather than defensive: AsyncLibraryNotFoundError is a distinct RuntimeError subclass raised deliberately rather than swallowed. The codebase ships a py.typed marker and is checked with mypy, and CI (ci.sh) enforces yapf formatting and mypy --pretty on every push in addition to running the pytest suite with -W error (warnings promoted to failures).

API Design The public surface is deliberately minimal: one function (current_async_library) plus two low-level state hooks (current_async_library_cvar, thread_local) for frameworks that want to declare themselves explicitly rather than be sniffed. There is no configuration object, no classes to instantiate, and no boilerplate — a caller imports one name and calls it. The docstring documents the exact detection table (library, minimum version required, returned string) inline, and the single custom exception type gives callers one clear thing to catch.

Used by 13 apps in this directory

Python
100%
Apache 2.0

Agno

Devops · AI Development · Automation

41,969

Build, run, and manage agent platforms with a full production stack — SDK, runtime, and control plane included.

View details
93
Repo Health
87
Technical
66
Dependency
Built with
Python100%
Updated today
Python
59%
Apache 2.0

argilla

AI Development · Data Engineering

5,088

Collaborate on high-quality AI training data with a self-hosted annotation platform built for LLMs, NLP, and multimodal models.

View details
65
Repo Health
81
Technical
61
Dependency
Built with
Python59%
Jupyter Notebook21%
Updated 6 days ago
C++
69%
Apache 2.0

ClickHouse

Databases · Analytics · Data Engineering

49,535

Open-source column-oriented database that delivers real-time analytical queries on petabyte-scale data with millisecond latency.

View details
95
Repo Health
90
Technical
68
Dependency
Built with
C++69%
Python13%
Updated today
Python
67%
Apache 2.0

GPT Researcher

Productivity · AI Assistants

29,203

The pioneering open-source autonomous AI agent that conducts deep, multi-source research and produces citation-backed reports exceeding 2,000 words — faster and more reliably than any human researcher.

View details
91
Repo Health
91
Technical
64
Dependency
Built with
Python67%
TypeScript20%
Updated 3 days ago
TypeScript
91%
Apache 2.0

Helicone

Monitoring · AI Development · Analytics

6,115

An open-source AI gateway and LLM observability platform that routes requests to 100+ models while logging cost, latency, and full traces for every call.

View details
65
Repo Health
81
Technical
66
Dependency
Built with
TypeScript91%
Updated 4 days ago
Python
84%
Apache 2.0

knowhere

AI Development · Developer Tools

2,752

Transform messy, unstructured documents into persistent, navigable memory that AI agents can actually use.

View details
83
Repo Health
75
Technical
69
Dependency
Built with
Python84%
HTML15%
Updated today
Python
61%
Apache 2.0

marimo

Developer Tools · Data Engineering

22,545

A reactive Python notebook that eliminates hidden state, runs reproducibly, and deploys as a web app or script — stored as pure Python, built for the AI era.

View details
89
Repo Health
91
Technical
65
Dependency
Built with
Python61%
TypeScript37%
Updated yesterday
TypeScript
98%
Apache 2.0

rowboat

AI Assistants · AI Development

17,436

Build, test, and deploy multi-agent AI workflows with a visual editor, RAG data sources, MCP tool integration, and a production-ready REST API.

View details
84
Repo Health
72
Technical
65
Dependency
Built with
TypeScript98%
Updated 2 days ago
TypeScript
62%
MIT

Scalar

Developer Tools

16,012

Beautiful, interactive OpenAPI documentation with a built-in offline-first API client and multi-language code generation — all in one open-source platform.

View details
90
Repo Health
89
Technical
66
Dependency
Built with
TypeScript62%
Vue32%
Updated today

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search