posthog
The official PostHog SDK for sending events, feature flags, and session data from any Python application.
Repository Health
Technical Analysis
posthog-python is the official Python client for PostHog, the open-source product analytics platform. It provides a simple API for capturing events, identifying users, evaluating feature flags, and recording session data, batching and sending them to a PostHog instance (cloud or self-hosted).
The SDK includes framework-specific integration guides for Django and Flask, an OpenFeature provider for standardized feature-flag evaluation, and optional LangChain integration for tracking LLM application usage. It supports Python 3.10 through 3.14 and ships as a production-stable package maintained directly by the PostHog team.
What You Get
- A simple capture() API for sending custom events and user properties to PostHog
- Feature flag evaluation, including local evaluation for low-latency flag checks
- An OpenFeature provider for standardized feature-flag access via the OpenFeature Python SDK
- Built-in integration guides and helpers for Django and Flask applications
- Optional LangChain integration for tracking LLM application usage and costs
- Automatic batching and asynchronous delivery of events to reduce application-side latency
Common Use Cases
- Instrumenting a Python backend to send product analytics events to a PostHog project
- Evaluating feature flags server-side to control rollout of new functionality
- Tracking LLM application usage and cost metrics via the LangChain integration
- Using OpenFeature-standard flag evaluation with PostHog as the backing provider
Under The Hood
Architecture - The posthog/ package (147 Python files) organizes functionality into a core client, a feature-flags evaluation module supporting both remote and local flag evaluation, and dedicated integration modules for Django, Flask, LangChain, and OpenFeature; a separate openfeature-provider/ directory implements the OpenFeature spec adapter, and sdk_compliance_adapter/ supports cross-SDK compliance testing shared across PostHog’s client libraries. Tech Stack - Pure Python (3.10-3.14) using requests for HTTP delivery, backoff for retry logic, distro for OS/platform detection, and typing-extensions for backward-compatible type hints; optional extras include langchain for LLM tracking and an opt-in zstd compression extra for request payload compression. Code Quality - The repository includes 78 test files covering unit and integration scenarios (integration_tests/), a mypy-baseline.txt for gradual type-checking adoption, and an e2e_test.sh script for end-to-end validation against a live PostHog instance; commit activity is very high (86 commits/month) reflecting active maintenance by the PostHog team. API Design - The client exposes a minimal posthog.capture()/posthog.identify() surface that mirrors PostHog’s other language SDKs for consistency, while advanced use cases (local flag evaluation, OpenFeature, LangChain) are opt-in via separate imports/extras, keeping the default install lightweight; official docs at posthog.com/docs/libraries/python provide framework-specific quick starts.