Compare commits

...
Author SHA1 Message Date
z 53159f70b6 docs(brand): add hero banner 2026-06-28 20:18:49 -07:00
z 359e47de80 chore(brand): dynamic hero banner 2026-06-28 20:18:47 -07:00
c8443d3a00 ci: run on self-hosted ARC pool (hanzo-build-linux-amd64/deploy), not GitHub-hosted (#1)
Co-authored-by: zeekay <z@hanzo.ai>
2026-06-19 20:35:56 -07:00
Antje WorringandClaude Opus 4.8 8cdd93b4aa docs: tidy LLM.md indexes; CLAUDE.md -> LLM.md symlink convention
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-17 09:42:05 -07:00
Antje WorringandClaude Opus 4.8 a638777561 Add Claude Code project docs (CLAUDE.md, LLM.md)
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-05-31 21:44:57 -07:00
Hanzo Dev 34b739cd6b rebrand: remove all compat aliases, zero posthog refs
- Remove Posthog = Insights alias from __init__.py
- Remove PosthogContextMiddleware alias from django.py
- Remove PostHogTracingProcessor alias from processor.py
- Change $lib from "posthog-python" to "insights-python"
- Change ingestion URLs from posthog.com to insights.hanzo.ai
- Rename all posthog_* kwargs to insights_* across AI wrappers
- Rename __posthog_exception_captured to __insights_exception_captured
- Rename posthog_context_stack contextvar to insights_context_stack
- Rename posthog🎏 Redis prefix to insights🎏
- Rename $$_posthog_redacted_* sentinels to $$_insights_redacted_*
- Remove POSTHOG_MW_* Django settings fallback, X-POSTHOG-* headers
- Rename Prompts(posthog=) param to Prompts(client=)
- Update APP_ENDPOINT to us.insights.hanzo.ai
- Update all tests, examples, docs, mypy config
2026-03-13 20:29:13 -07:00
Hanzo Dev 9fb72596af rebrand: Posthog->Insights, package hanzo-insights
- Rename main class Posthog -> Insights (Posthog kept as alias)
- Rename PosthogContextMiddleware -> InsightsContextMiddleware (alias kept)
- Rename PostHogTracingProcessor -> InsightsTracingProcessor (alias kept)
- Add `insights/` re-export package so `from insights import Insights` works
- Update all imports from `posthog` to `hanzo_insights` across source and tests
- Update docstrings, comments, error messages, user agent string
- Update README, example.py, .env.example, Makefile, LLM.md
- Django middleware now supports INSIGHTS_MW_* settings (POSTHOG_MW_* still works)
- Django middleware accepts X-INSIGHTS-* headers (X-POSTHOG-* still works)
- Keep protocol-level values ($lib, ingestion URLs, sentinel strings) for server compat
- Keep posthog_* parameter names in AI wrappers for API compat
- All 681 tests pass
2026-03-13 19:58:09 -07:00
Hanzo Dev 7105552a05 docs: add LLM.md project guide 2026-03-11 10:32:50 -07:00
Hanzo Dev 98a2ca443c chore: rename package from hanzoanalytics to hanzo-insights
Package name: hanzo-insights (import as hanzo_insights)
2026-03-06 22:44:19 -08:00
Hanzo Dev f4cbdf28c4 Rename package from posthog/posthoganalytics to hanzoanalytics
Full rebrand: module directory, pyproject.toml, setup.py, all imports.
2026-03-06 22:42:05 -08:00
Radu RaiceaandGitHub 11466c625e feat(llma): support prompt versions in prompts sdk (#454)
* feat(llma): support prompt versions in prompts sdk

* fix(llma): enforce clear_cache version requires name
2026-03-06 10:29:07 +01:00
github-actions[bot] ef5e1356ef chore: Release v7.9.7 2026-03-05 22:09:29 +00:00
a99c7d73b1 Add warning log for local flag evaluation cold start (#452)
* Add warning log when local flag evaluation called before flags loaded

When feature_enabled() is called with only_evaluate_locally=True before
flag definitions are fetched, the SDK silently returns None. This adds a
warning log so users can diagnose the issue immediately.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Move cold start warning to only fire for only_evaluate_locally=True

The warning was in _locally_evaluate_flag which runs for all flag
evaluations, including those that fall back to server-side evaluation.
Move it to the caller where only_evaluate_locally is known, so it only
fires when the caller explicitly opted out of the server fallback.

* Narrow cold start warning to only fire when flags were never fetched

Use `is None` instead of `not` to avoid firing when flags are loaded
but empty (401, 402, no personal_api_key), which already have their
own specific error logs.

* add changeset

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-05 13:36:05 -08:00
b206669bf6 fix(llma): use distinct_id from outer context if not provided (#449)
* fix(llma): use distinct_id from outer context if not provided

* fix(llma): distinct_id from context is now explicitly passed to capture method

* fix(llma): fix $process_person_profile with outer context distinct_id, add tests

- Fix personless check to consider outer context distinct_id (not just the
  explicit param), so events from users who set distinct_id via outer context
  are not incorrectly marked as personless.
- Fix typo: "district_id" -> "distinct_id" in comments.
- Add test coverage for distinct_id resolution: no id (personless), explicit
  param, outer context, and explicit overriding outer context.

* chore: add sampo changeset for distinct_id context fix

* style: ruff format

---------

Co-authored-by: Andrew Maguire <andrewm4894@gmail.com>
2026-03-05 15:11:40 +00:00
github-actions[bot] 16e180231f chore: Release v7.9.6 2026-03-02 21:28:45 +00:00
8d83315b67 refactor: add PROPERTY_OPERATORS constant for match_property (#448)
* feat: add semver targeting support to local flag evaluation

Implement 9 semver comparison operators (semver_eq, semver_neq, semver_gt, semver_gte, semver_lt, semver_lte, semver_tilde, semver_caret, semver_wildcard) for feature flag local evaluation. Uses regex-based parsing that matches the server-side sortableSemver behavior to handle v-prefix, whitespace, pre-release suffixes, and non-standard version formats.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>

* fix: guard against ReDoS in semver regex parsing

Add input length limit before regex search to prevent polynomial
backtracking on adversarial input (CodeQL py/polynomial-redos).

* fix: replace regex with string parsing to resolve ReDoS warning

Replace SEMVER_EXTRACT_RE regex with simple string splitting to
eliminate nested quantifiers that CodeQL flagged as polynomial-redos.

* refactor: inline semver operator tuple to match existing patterns

* refactor: add PROPERTY_OPERATORS constant for match_property

Extract all operator strings into a single source-of-truth tuple and
validate against it early in match_property, replacing the fallthrough
at the end of the function.

* refactor: split PROPERTY_OPERATORS into composable sub-groups

Break the flat tuple into category-specific tuples (EQUALITY_OPERATORS,
STRING_OPERATORS, etc.) that compose into PROPERTY_OPERATORS via
concatenation. The semver dispatch code now references
SEMVER_OPERATORS and SEMVER_COMPARISON_OPERATORS instead of
repeating the full operator lists inline.

* fix: add unreachable fallthrough to satisfy mypy return check

* add release

---------

Co-authored-by: Claude Haiku 4.5 <noreply@anthropic.com>
2026-03-02 21:12:09 +00:00
github-actions[bot] 1e1e566fa5 chore: Release v7.9.5 2026-03-02 20:53:53 +00:00
830244bd40 feat: add semver targeting support to local flag evaluation (#447)
* feat: add semver targeting support to local flag evaluation

Implement 9 semver comparison operators (semver_eq, semver_neq, semver_gt, semver_gte, semver_lt, semver_lte, semver_tilde, semver_caret, semver_wildcard) for feature flag local evaluation. Uses regex-based parsing that matches the server-side sortableSemver behavior to handle v-prefix, whitespace, pre-release suffixes, and non-standard version formats.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>

* fix: guard against ReDoS in semver regex parsing

Add input length limit before regex search to prevent polynomial
backtracking on adversarial input (CodeQL py/polynomial-redos).

* fix: replace regex with string parsing to resolve ReDoS warning

Replace SEMVER_EXTRACT_RE regex with simple string splitting to
eliminate nested quantifiers that CodeQL flagged as polynomial-redos.

* refactor: inline semver operator tuple to match existing patterns

* add changeset

---------

Co-authored-by: Claude Haiku 4.5 <noreply@anthropic.com>
2026-03-02 20:52:37 +00:00
github-actions[bot] 60001f1829 chore: Release v7.9.4 2026-02-25 15:28:29 +00:00
Carlos MarchalandGitHub a68a6a6d04 fix: revert manual release and add sampo changeset for ai_tokens_source (#445) 2026-02-25 16:25:53 +01:00
Andrew MaguireandGitHub 150e24ba6a feat(llma): add $ai_tokens_source property to detect token value overrides (#444)
* feat: add $ai_tokens_source property to detect token value overrides

When users pass token properties (e.g. $ai_input_tokens) via
posthog_properties, these override the SDK-computed values. This new
$ai_tokens_source property ("sdk" or "passthrough") lets us distinguish
whether token values came from the SDK or were externally injected,
which is critical for diagnosing cost calculation discrepancies.

* chore: bump version to 7.9.4

* chore: add changelog entry for 7.9.4

* chore: fix ruff formatting

* chore: remove unused pytest import
2026-02-25 13:38:53 +00:00
Michael BiancoandGitHub a8b5529baf fix: use $ip not $ip_addess (#356) 2026-02-20 07:46:13 +01:00
github-actions[bot] d45c04646e chore: Release v7.9.3 2026-02-18 22:20:09 +00:00
Rafael AudibertandGitHub 9f9553a420 Small fixes for python publishing (#441)
* fix: Avoid setting dynamic version

Version is now fixed because of sampo, so we can get rid of this

* feat: add changeset

* docs: Add new RELEASING section to README
2026-02-18 22:17:26 +00:00
github-actions[bot] 16bc87b646 chore: Release v7.9.2 2026-02-18 22:05:00 +00:00
Rafael AudibertandGitHub f1dc4d7391 chore: Migrate releases to sampo (#398)
* chore: Migrate releases to `sampo`

This is much closer to what we have in `posthog-js`, let's see if it's a good thing!

There's still a lot to do before deploying this:
- updating CI
- updating README with instructions

* Add sampo changeset

* chore: Update  to relase Python via Slack + sampo

* Update release.yml

* fix: Use pyproject.toml version as source of truth
2026-02-18 19:02:19 -03:00
Radu RaiceaandGitHub 23dae56d68 fix(ai): bind prompt reads to project token (#433)
* fix(ai): bind prompt reads to project token

* chore(release): bump version to 7.8.7
2026-02-17 16:58:59 +00:00
73bec043cf chore: release v7.9.0 (#434)
chore: bump version to 7.9.0

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 10:32:05 +01:00
AndersandGitHub 603ed376dd feat: Support device_id as bucketing identifier for local evaluation (#424)
* feat: Support device_id as bucketing identifier for local evaluation

Add support for `bucketing_identifier` field on feature flags to allow
using `device_id` instead of `distinct_id` for hashing/bucketing in
local evaluation.
2026-02-16 11:05:09 +01:00
AndersandGitHub bb0c7b4fa8 test(flags): make wrong-key load_feature_flags deterministic (#432) 2026-02-13 15:53:22 +01:00
Aleksander BłaszkiewiczandGitHub 499194e0c4 feat: limit max number of items in collection to scan (#430)
* feat: limit max number of items in collection to scan

* feat: changelog

* fix: format

* feat: test

* feat: replace entire collection instead of truncating
2026-02-11 14:59:11 +01:00
Aleksander BłaszkiewiczandGitHub ffb8e9b591 feat: further optimize code variables regex search (#429)
* feat: initial

* fix: ruff
2026-02-09 23:59:03 +01:00
Aleksander BłaszkiewiczandGitHub 7780ca8390 fix: long variables pattern matching (#428) 2026-02-09 17:45:23 +01:00
AndersandGitHub bca175214d fix: Retry on 408 and respect Retry-After header (#426)
* fix: Retry on 408 and respect Retry-After header

408 (Request Timeout) was incorrectly treated as a non-retryable client
error. Retry-After response headers were ignored during backoff. Replace
backoff library usage with a manual retry loop that honours Retry-After
when present and falls back to exponential backoff otherwise.

* fix: Parse HTTP-date Retry-After values

Retry-After can be seconds or an HTTP-date per RFC 7231. Fall back to
email.utils.parsedate_to_datetime when the numeric parse fails.

* fix: Don't retry on unclassifiable APIError status

When APIError.status is "N/A" (no HTTP status), treat it as
non-retryable to avoid unexpected retry loops on errors the SDK
cannot classify.

* test: Add retry delay tests for Retry-After and exponential backoff

Verify time.sleep is called with the Retry-After value when present,
uses exponential backoff (2^attempt) when absent, and that 408 is
retried.
2026-02-09 12:15:49 +00:00
Aleksander BłaszkiewiczandGitHub fe3a9bbf75 fix: openai image sanitization (#425) 2026-02-06 14:15:57 +01:00
b6e66330e5 fix: openAI input image sanitization (#384)
Co-authored-by: Aleksander Błaszkiewicz <kqmdjc8@gmail.com>
2026-02-06 13:53:02 +01:00
Gabriel GrinbergandGitHub 4f32fa4100 Fix feature flag 401 errors causing HTTP request storm (#422)
* Fix feature flag 401 errors causing HTTP request storm

Set feature_flags = [] on 401 error to prevent repeated requests.

* Clear flag_cache, group_type_mapping, cohorts on 401
2026-02-04 10:31:10 -05:00
104 changed files with 4663 additions and 2400 deletions
+6 -6
View File
@@ -1,11 +1,11 @@
# PostHog API Configuration
# Hanzo Insights API Configuration
# Copy this file to .env and update with your actual values
# Your project API key (found on the /setup page in PostHog)
POSTHOG_PROJECT_API_KEY=phc_your_project_api_key_here
# Your project API key (found on the setup page in Insights)
INSIGHTS_PROJECT_API_KEY=hi_your_project_api_key_here
# Your personal API key (for local evaluation and other advanced features)
POSTHOG_PERSONAL_API_KEY=phx_your_personal_api_key_here
INSIGHTS_PERSONAL_API_KEY=phx_your_personal_api_key_here
# PostHog host URL (remove this line if using posthog.com)
POSTHOG_HOST=http://localhost:8000
# Insights host URL (remove this line if using insights.hanzo.ai)
INSIGHTS_HOST=http://localhost:8000
+9
View File
@@ -0,0 +1,9 @@
<svg xmlns="http://www.w3.org/2000/svg" width="1280" height="640" viewBox="0 0 1280 640" role="img" aria-label="insights-python">
<rect width="1280" height="640" fill="#0A0A0A"/>
<svg x="96" y="215" width="210" height="210" viewBox="0 0 67 67"><path d="M22.21 67V44.6369H0V67H22.21Z" fill="#fff"/><path d="M66.7038 22.3184H22.2534L0.0878906 44.6367H44.4634L66.7038 22.3184Z" fill="#fff"/><path d="M22.21 0H0V22.3184H22.21V0Z" fill="#fff"/><path d="M66.7198 0H44.5098V22.3184H66.7198V0Z" fill="#fff"/><path d="M66.7198 67V44.6369H44.5098V67H66.7198Z" fill="#fff"/></svg>
<text x="378" y="276" font-family="Inter,system-ui,-apple-system,sans-serif" font-size="78" font-weight="800" letter-spacing="-2" fill="#ffffff">insights-python</text>
<text x="378" y="322" font-family="Inter,system-ui,sans-serif" font-size="30" fill="#ffffff" opacity=".66">Send usage data from your Python code to PostHog.</text>
<rect x="378" y="338" width="806" height="3" rx="1.5" fill="#ffffff" opacity=".9"/>
<text x="378" y="390" font-family="Inter,system-ui,sans-serif" font-size="24" font-weight="600" fill="#ffffff" opacity=".5">github.com/hanzoai</text>
<text x="1184" y="390" text-anchor="end" font-family="Inter,system-ui,sans-serif" font-size="24" font-weight="600" fill="#ffffff" opacity=".5">hanzo.ai</text>
</svg>

After

Width:  |  Height:  |  Size: 1.3 KiB

+4 -4
View File
@@ -9,7 +9,7 @@ permissions:
jobs:
code-quality:
name: Code quality checks
runs-on: ubuntu-latest
runs-on: hanzo-build-linux-amd64
steps:
- uses: actions/checkout@85e6279cec87321a52edac9c87bce653a07cf6c2
@@ -46,7 +46,7 @@ jobs:
tests:
name: Python ${{ matrix.python-version }} tests
runs-on: ubuntu-latest
runs-on: hanzo-build-linux-amd64
strategy:
matrix:
python-version: ['3.10', '3.11', '3.12', '3.13', '3.14']
@@ -78,7 +78,7 @@ jobs:
import-check:
name: Python ${{ matrix.python-version }} import check
runs-on: ubuntu-latest
runs-on: hanzo-build-linux-amd64
strategy:
matrix:
python-version: ['3.10', '3.11', '3.12', '3.13', '3.14']
@@ -101,7 +101,7 @@ jobs:
django5-integration:
name: Django 5 integration tests
runs-on: ubuntu-latest
runs-on: hanzo-build-linux-amd64
steps:
- uses: actions/checkout@85e6279cec87321a52edac9c87bce653a07cf6c2
+1 -1
View File
@@ -11,7 +11,7 @@ on:
jobs:
analyze:
name: Analyze (${{ matrix.language }})
runs-on: 'ubuntu-latest'
runs-on: hanzo-build-linux-amd64
permissions:
security-events: write
# required to fetch internal or private CodeQL packs
+1 -1
View File
@@ -6,7 +6,7 @@ on:
jobs:
docs-generation:
name: Generate references
runs-on: ubuntu-latest
runs-on: hanzo-build-linux-amd64
permissions:
contents: write
steps:
+227 -36
View File
@@ -1,58 +1,249 @@
name: "Release"
on:
push:
branches:
- master
paths:
- "posthog/version.py"
pull_request:
types: [closed]
branches: [master]
workflow_dispatch:
permissions:
contents: read
# Concurrency control: only one release process can run at a time
# This prevents race conditions if multiple PRs with 'release' label merge simultaneously
concurrency:
group: release
cancel-in-progress: false
jobs:
release:
name: Publish release
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write
check-release-label:
name: Check for release label
runs-on: hanzo-build-linux-amd64
# Run when PR with 'release' label is merged to master
if: |
github.event_name == 'workflow_dispatch' ||
(github.event_name == 'pull_request' &&
github.event.pull_request.merged == true &&
contains(github.event.pull_request.labels.*.name, 'release'))
outputs:
should-release: ${{ steps.check.outputs.should-release }}
steps:
- name: Checkout the repository
uses: actions/checkout@85e6279cec87321a52edac9c87bce653a07cf6c2
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: master
fetch-depth: 0
- name: Check release conditions
id: check
run: |
changeset_count=$(find .sampo/changesets -name '*.md' 2>/dev/null | wc -l)
if [ "$changeset_count" -gt 0 ]; then
echo "should-release=true" >> "$GITHUB_OUTPUT"
echo "Found $changeset_count changeset(s), ready to release"
else
echo "should-release=false" >> "$GITHUB_OUTPUT"
echo "No changesets to release"
fi
notify-approval-needed:
name: Notify Slack - Approval Needed
needs: check-release-label
if: needs.check-release-label.outputs.should-release == 'true'
uses: posthog/.github/.github/workflows/notify-approval-needed.yml@main
with:
slack_channel_id: ${{ vars.SLACK_APPROVALS_CLIENT_LIBRARIES_CHANNEL_ID }}
slack_user_group_id: ${{ vars.GROUP_CLIENT_LIBRARIES_SLACK_GROUP_ID }}
secrets:
slack_bot_token: ${{ secrets.SLACK_CLIENT_LIBRARIES_BOT_TOKEN }}
posthog_project_api_key: ${{ secrets.POSTHOG_PROJECT_API_KEY }}
release:
name: Release and publish
needs: [check-release-label, notify-approval-needed]
runs-on: hanzo-build-linux-amd64
# Use `always()` to ensure the job runs even if notify-approval-needed is skipped,
# but still depend on it to access `needs.notify-approval-needed.outputs.slack_ts`
if: always() && needs.check-release-label.outputs.should-release == 'true'
environment: "Release" # This will require an approval from a maintainer, they are notified in Slack above
permissions:
contents: write
actions: write
id-token: write
steps:
- name: Notify Slack - Approved
if: needs.notify-approval-needed.outputs.slack_ts != ''
uses: posthog/.github/.github/actions/slack-thread-reply@main
with:
slack_bot_token: ${{ secrets.SLACK_CLIENT_LIBRARIES_BOT_TOKEN }}
slack_channel_id: ${{ vars.SLACK_APPROVALS_CLIENT_LIBRARIES_CHANNEL_ID }}
thread_ts: ${{ needs.notify-approval-needed.outputs.slack_ts }}
message: "✅ Release approved! Version bump in progress..."
emoji_reaction: "white_check_mark"
- name: Get GitHub App token
id: releaser
uses: actions/create-github-app-token@v2
with:
app-id: ${{ secrets.GH_APP_POSTHOG_PYTHON_RELEASER_APP_ID }}
private-key: ${{ secrets.GH_APP_POSTHOG_PYTHON_RELEASER_PRIVATE_KEY }}
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: master
fetch-depth: 0
token: ${{ steps.releaser.outputs.token }}
- name: Set up Python
uses: actions/setup-python@8d9ed9ac5c53483de85588cdf95a591a75ab9f55
uses: actions/setup-python@v5
with:
python-version: 3.11.11
- name: Install uv
uses: astral-sh/setup-uv@0c5e2b8115b80b4c7c5ddf6ffdd634974642d182 # v5.4.1
uses: astral-sh/setup-uv@v5
with:
enable-cache: true
pyproject-file: 'pyproject.toml'
- name: Detect version
run: echo "REPO_VERSION=$(python3 posthog/version.py)" >> $GITHUB_ENV
enable-cache: true
pyproject-file: "pyproject.toml"
- name: Prepare for building release
- name: Install Rust
uses: dtolnay/rust-toolchain@0b1efabc08b657293548b77fb76cc02d26091c7e
with:
toolchain: 1.91.1
components: cargo
- name: Cache Sampo CLI
id: cache-sampo
uses: actions/cache@v3
with:
path: ~/.cargo/bin/sampo
key: sampo-${{ runner.os }}-${{ runner.arch }}
- name: Install Sampo CLI
if: steps.cache-sampo.outputs.cache-hit != 'true'
run: cargo install sampo
- name: Install dependencies
run: uv sync --extra dev
- name: Push releases to PyPI
env:
TWINE_USERNAME: __token__
run: uv run make release && uv run make release_analytics
- name: Configure Git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Create GitHub release
- name: Prepare release with Sampo
id: sampo-release
env:
GITHUB_TOKEN: ${{ steps.releaser.outputs.token }}
run: |
sampo release
new_version=$(python3 -c "import tomllib; print(tomllib.load(open('pyproject.toml', 'rb'))['project']['version'])")
echo "new_version=$new_version" >> "$GITHUB_OUTPUT"
- name: Sync version to posthog/version.py
run: |
echo 'VERSION = "${{ steps.sampo-release.outputs.new_version }}"' > posthog/version.py
- name: Commit release changes
id: commit-release
env:
GITHUB_TOKEN: ${{ steps.releaser.outputs.token }}
run: |
git add -A
if git diff --staged --quiet; then
echo "No changes to commit"
echo "committed=false" >> "$GITHUB_OUTPUT"
else
git commit -m "chore: Release v${{ steps.sampo-release.outputs.new_version }}"
git push origin master
echo "committed=true" >> "$GITHUB_OUTPUT"
fi
# Publishing is done manually (not via `sampo publish`) because we need to
# publish both `posthog` and `posthoganalytics` packages to PyPI.
# Sampo only knows about the `posthog` package, so we handle both here.
# Both packages use PyPI OIDC trusted publishing (no API tokens needed).
- name: Build posthog
if: steps.commit-release.outputs.committed == 'true'
run: uv run make build_release
- name: Publish posthog to PyPI
if: steps.commit-release.outputs.committed == 'true'
uses: pypa/gh-action-pypi-publish@release/v1
# The `posthoganalytics` package is a mirror of `posthog` published under
# a different name for backwards compatibility. The make target handles
# copying, renaming imports, and building the dist automatically.
- name: Build posthoganalytics
if: steps.commit-release.outputs.committed == 'true'
run: uv run make build_release_analytics
- name: Publish posthoganalytics to PyPI
if: steps.commit-release.outputs.committed == 'true'
uses: pypa/gh-action-pypi-publish@release/v1
# We skip `sampo publish` (which normally creates the tag) because we
# need to publish both posthog and posthoganalytics manually, so we
# create the tag ourselves.
- name: Tag release
if: steps.commit-release.outputs.committed == 'true'
run: git tag "v${{ steps.sampo-release.outputs.new_version }}"
- name: Push tags
if: steps.commit-release.outputs.committed == 'true'
run: git push origin --tags
- name: Create GitHub Release
if: steps.commit-release.outputs.committed == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: gh release create "v${{ steps.sampo-release.outputs.new_version }}" --generate-notes
- name: Dispatch generate-references
if: steps.commit-release.outputs.committed == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release create "v${{ env.REPO_VERSION }}" \
--title "${{ env.REPO_VERSION }}" \
--generate-notes
- name: Dispatch generate-references for posthog-python
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh workflow run generate-references.yml --ref master
run: gh workflow run generate-references.yml --ref master
# Notify in case of a failure
- name: Send failure event to PostHog
if: ${{ failure() }}
uses: PostHog/posthog-github-action@v0.1
with:
posthog-token: "${{ secrets.POSTHOG_PROJECT_API_KEY }}"
event: "posthog-python-github-release-workflow-failure"
properties: >-
{
"commitSha": "${{ github.sha }}",
"jobStatus": "${{ job.status }}",
"ref": "${{ github.ref }}",
"version": "v${{ steps.sampo-release.outputs.new_version }}"
}
- name: Notify Slack - Failed
if: ${{ failure() && needs.notify-approval-needed.outputs.slack_ts != '' }}
uses: posthog/.github/.github/actions/slack-thread-reply@main
with:
slack_bot_token: ${{ secrets.SLACK_CLIENT_LIBRARIES_BOT_TOKEN }}
slack_channel_id: ${{ vars.SLACK_APPROVALS_CLIENT_LIBRARIES_CHANNEL_ID }}
thread_ts: ${{ needs.notify-approval-needed.outputs.slack_ts }}
message: "❌ Failed to release `posthog-python@v${{ steps.sampo-release.outputs.new_version }}`! <https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}|View logs>"
emoji_reaction: "x"
notify-released:
name: Notify Slack - Released
needs: [check-release-label, notify-approval-needed, release]
runs-on: hanzo-build-linux-amd64
if: always() && needs.release.result == 'success' && needs.notify-approval-needed.outputs.slack_ts != ''
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Notify Slack - Released
uses: posthog/.github/.github/actions/slack-thread-reply@main
with:
slack_bot_token: ${{ secrets.SLACK_CLIENT_LIBRARIES_BOT_TOKEN }}
slack_channel_id: ${{ vars.SLACK_APPROVALS_CLIENT_LIBRARIES_CHANNEL_ID }}
thread_ts: ${{ needs.notify-approval-needed.outputs.slack_ts }}
message: "🚀 posthog-python released successfully!"
emoji_reaction: "rocket"
+5
View File
@@ -0,0 +1,5 @@
---
pypi/posthog: patch
---
feat(llma): support fetching versioned prompts from the prompts sdk
+19
View File
@@ -0,0 +1,19 @@
# Sampo configuration
version = 1
[git]
default_branch = "master"
short_tags = "posthog" # Tag with v1.2.3 rather than posthog-v1.2.3
[github]
repository = "posthog/posthog-python"
[changelog]
# Options for release notes generation.
# show_commit_hash = true (default)
# show_acknowledgments = true (default)
[packages]
# Options for package discovery and filtering.
# ignore_unpublished = false (default)
# ignore = ["internal-*", "examples/*"]
+7 -7
View File
@@ -1,6 +1,6 @@
# Before Send Hook
The `before_send` parameter allows you to modify or filter events before they are sent to PostHog. This is useful for:
The `before_send` parameter allows you to modify or filter events before they are sent to Insights. This is useful for:
- **Privacy**: Removing or masking sensitive data (PII)
- **Filtering**: Dropping unwanted events (test events, internal users, etc.)
@@ -10,12 +10,12 @@ The `before_send` parameter allows you to modify or filter events before they ar
## Basic Usage
```python
import posthog
import hanzo_insights
from typing import Optional, Dict, Any
def my_before_send(event: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""
Process event before sending to PostHog.
Process event before sending to Insights.
Args:
event: The event dictionary containing 'event', 'distinct_id', 'properties', etc.
@@ -27,7 +27,7 @@ def my_before_send(event: Dict[str, Any]) -> Optional[Dict[str, Any]]:
return event
# Initialize client with before_send hook
client = posthog.Client(
client = hanzo_insights.Client(
api_key="your-project-api-key",
before_send=my_before_send
)
@@ -166,7 +166,7 @@ def should_drop_event(event: dict[str, Any]) -> bool:
## Error Handling
If your `before_send` function raises an exception, PostHog will:
If your `before_send` function raises an exception, Insights will:
1. Log the error
2. Continue with the original, unmodified event
@@ -184,7 +184,7 @@ def risky_before_send(event: dict[str, Any]) -> Optional[dict[str, Any]]:
## Complete Example
```python
import posthog
import hanzo_insights
from typing import Optional, Any
import re
@@ -227,7 +227,7 @@ def production_before_send(event: dict[str, Any]) -> Optional[dict[str, Any]]:
return event # Return original event on error
# Usage
client = posthog.Client(
client = hanzo_insights.Client(
api_key="your-api-key",
before_send=production_before_send
)
+121 -58
View File
@@ -1,53 +1,116 @@
# 7.8.2 - 2026-02-04
# posthog
## 7.9.7 — 2026-03-05
### Patch changes
- [b206669](https://github.com/posthog/posthog-python/commit/b206669bf62c923346ad28881dc4694d933ca424) fix(llma): use distinct_id from outer context if not provided, fix $process_person_profile for context-based identity — Thanks @ethanporcaro for your first contribution 🎉!
- [a99c7d7](https://github.com/posthog/posthog-python/commit/a99c7d73b1e0ef1f35d856c82ace21237ee253a3) Add warning log for local flag evaluation cold start — Thanks @dmarticus!
## 7.9.6 — 2026-03-02
### Patch changes
- [8d83315](https://github.com/posthog/posthog-python/commit/8d83315b67c21eb9e7d6c17bae27ada98ca2643d) add PROPERTY_OPERATORS constant for match_property — Thanks @dmarticus!
## 7.9.5 — 2026-03-02
### Patch changes
- [830244b](https://github.com/posthog/posthog-python/commit/830244bd409b1992ae2e49610f8f87d2cdfc8096) add semver targeting support to local evaluation — Thanks @dmarticus!
## 7.9.4 — 2026-02-25
### Patch changes
- [a68a6a6](https://github.com/posthog/posthog-python/commit/a68a6a6d045072c88eeee7acac441536919b5954) feat(llma): add `$ai_tokens_source` property ("sdk" or "passthrough") to all `$ai_generation` events to detect when token values are externally overridden via `posthog_properties` — Thanks @carlos-marchal-ph!
## 7.9.3 — 2026-02-18
### Patch changes
- [9f9553a](https://github.com/posthog/posthog-python/commit/9f9553a420d22e5e6435b775993f61a059280c2a) Fix posthoganalytics release, previously broken — Thanks @rafaeelaudibert!
## 7.9.2 — 2026-02-18
### Patch changes
- [f1dc4d7](https://github.com/posthog/posthog-python/commit/f1dc4d73914712983a7f715ee4fe1b70e66e770a) Add sampo to the project — Thanks @rafaeelaudibert!
## 7.9.1 - 2026-02-17
fix(llma): make prompt fetches deterministic by requiring project_api_key and sending it as token query param
## 7.9.0 - 2026-02-17
feat: Support device_id as bucketing identifier for local evaluation
## 7.8.6 - 2026-02-09
fix: limit collections scanning in code variables
## 7.8.5 - 2026-02-09
fix: further optimize code variables pattern matching
## 7.8.4 - 2026-02-09
fix: do not pattern match long values in code variables
## 7.8.3 - 2026-02-06
fix: openAI input image sanitization
## 7.8.2 - 2026-02-04
fix(llma): fix prompts default url
# 7.8.1 - 2026-02-03
## 7.8.1 - 2026-02-03
fix(llma): small fixes for prompt management
# 7.8.0 - 2026-01-28
## 7.8.0 - 2026-01-28
feat(llma): add prompt management
Adds the Prompt Management feature. At the time of release, this feature is in a closed alpha.
# 7.7.0 - 2026-01-15
## 7.7.0 - 2026-01-15
feat(ai): Add OpenAI Agents SDK integration
Automatic tracing for agent workflows, handoffs, tool calls, guardrails, and custom spans. Includes `$ai_total_tokens`, `$ai_error_type` categorization, and `$ai_framework` property.
# 7.6.0 - 2026-01-12
## 7.6.0 - 2026-01-12
feat: add device_id to flags request payload
Add device_id parameter to all feature flag methods, allowing the server to track device identifiers for flag evaluation. The device_id can be passed explicitly or set via context using `set_context_device_id()`.
# 7.5.1 - 2026-01-07
## 7.5.1 - 2026-01-07
fix: avoid return from finally block to fix Python 3.14 SyntaxWarning (#361) - thanks @jodal
# 7.5.0 - 2026-01-06
## 7.5.0 - 2026-01-06
feat: Capture Langchain, OpenAI and Anthropic errors as exceptions (if exception autocapture is enabled)
feat: Add reference to exception in LLMA trace and span events
# 7.4.3 - 2026-01-02
## 7.4.3 - 2026-01-02
Fixes cache creation cost for Langchain with Anthropic
# 7.4.2 - 2025-12-22
## 7.4.2 - 2025-12-22
feat: add `in_app_modules` option to control code variables capturing
# 7.4.1 - 2025-12-19
## 7.4.1 - 2025-12-19
fix: extract model from response for OpenAI stored prompts
When using OpenAI stored prompts, the model is defined in the OpenAI dashboard rather than passed in the API request. This fix adds a fallback to extract the model from the response object when not provided in kwargs, ensuring generations show up with the correct model and enabling cost calculations.
# 7.4.0 - 2025-12-16
## 7.4.0 - 2025-12-16
feat: Add automatic retries for feature flag requests
@@ -59,32 +122,32 @@ Feature flag API requests now automatically retry on transient failures:
Rate limit (429) and quota (402) errors are not retried.
# 7.3.1 - 2025-12-06
## 7.3.1 - 2025-12-06
fix: remove unused $exception_message and $exception_type
# 7.3.0 - 2025-12-05
## 7.3.0 - 2025-12-05
feat: improve code variables capture masking
# 7.2.0 - 2025-12-01
## 7.2.0 - 2025-12-01
feat: add $feature_flag_evaluated_at properties to $feature_flag_called events
# 7.1.0 - 2025-11-26
## 7.1.0 - 2025-11-26
Add support for the async version of Gemini.
# 7.0.2 - 2025-11-18
## 7.0.2 - 2025-11-18
Add support for Python 3.14.
Projects upgrading to Python 3.14 should ensure any Pydantic models passed into the SDK use Pydantic v2, as Pydantic v1 is not compatible with Python 3.14.
# 7.0.1 - 2025-11-15
## 7.0.1 - 2025-11-15
Try to use repr() when formatting code variables
# 7.0.0 - 2025-11-11
## 7.0.0 - 2025-11-11
NB Python 3.9 is no longer supported
@@ -98,155 +161,155 @@ NB Python 3.9 is no longer supported
- langchain-community: 0.3.29 → 0.4.1
- langgraph: 0.6.6 → 1.0.2
# 6.9.3 - 2025-11-10
## 6.9.3 - 2025-11-10
- feat(ph-ai): PostHog properties dict in GenerationMetadata
# 6.9.2 - 2025-11-10
## 6.9.2 - 2025-11-10
- fix(llma): fix cache token double subtraction in Langchain for non-Anthropic providers causing negative costs
# 6.9.1 - 2025-11-07
## 6.9.1 - 2025-11-07
- fix(error-tracking): pass code variables config from init to client
# 6.9.0 - 2025-11-06
## 6.9.0 - 2025-11-06
- feat(error-tracking): add local variables capture
# 6.8.0 - 2025-11-03
## 6.8.0 - 2025-11-03
- feat(llma): send web search calls to be used for LLM cost calculations
# 6.7.14 - 2025-11-03
## 6.7.14 - 2025-11-03
- fix(django): Handle request.user access in async middleware context to prevent SynchronousOnlyOperation errors in Django 5+ (fixes #355)
- test(django): Add Django 5 integration test suite with real ASGI application testing async middleware behavior
# 6.7.13 - 2025-11-02
## 6.7.13 - 2025-11-02
- fix(llma): cache cost calculation in the LangChain callback
# 6.7.12 - 2025-11-02
## 6.7.12 - 2025-11-02
- fix(django): Restore process_exception method to capture view and downstream middleware exceptions (fixes #329)
- fix(ai/langchain): Add LangChain 1.0+ compatibility for CallbackHandler imports (fixes #362)
# 6.7.11 - 2025-10-28
## 6.7.11 - 2025-10-28
- feat(ai): Add `$ai_framework` property for framework integrations (e.g. LangChain)
# 6.7.10 - 2025-10-24
## 6.7.10 - 2025-10-24
- fix(django): Make middleware truly hybrid - compatible with both sync (WSGI) and async (ASGI) Django stacks without breaking sync-only deployments
# 6.7.9 - 2025-10-22
## 6.7.9 - 2025-10-22
- fix(flags): multi-condition flags with static cohorts returning wrong variants
# 6.7.8 - 2025-10-16
## 6.7.8 - 2025-10-16
- fix(llma): missing async for OpenAI's streaming implementation
# 6.7.7 - 2025-10-14
## 6.7.7 - 2025-10-14
- fix: remove deprecated attribute $exception_personURL from exception events
# 6.7.6 - 2025-09-16
## 6.7.6 - 2025-09-16
- fix: don't sort condition sets with variant overrides to the top
- fix: Prevent core Client methods from raising exceptions
# 6.7.5 - 2025-09-16
## 6.7.5 - 2025-09-16
- feat: Django middleware now supports async request handling.
# 6.7.4 - 2025-09-05
## 6.7.4 - 2025-09-05
- fix: Missing system prompts for some providers
# 6.7.3 - 2025-09-04
## 6.7.3 - 2025-09-04
- fix: missing usage tokens in Gemini
# 6.7.2 - 2025-09-03
## 6.7.2 - 2025-09-03
- fix: tool call results in streaming providers
# 6.7.1 - 2025-09-01
## 6.7.1 - 2025-09-01
- fix: Add base64 inline image sanitization
# 6.7.0 - 2025-08-26
## 6.7.0 - 2025-08-26
- feat: Add support for feature flag dependencies
# 6.6.1 - 2025-08-21
## 6.6.1 - 2025-08-21
- fix: Prevent `NoneType` error when `group_properties` is `None`
# 6.6.0 - 2025-08-15
## 6.6.0 - 2025-08-15
- feat: Add `flag_keys_to_evaluate` parameter to optimize feature flag evaluation performance by only evaluating specified flags
- feat: Add `flag_keys_filter` option to `send_feature_flags` for selective flag evaluation in capture events
# 6.5.0 - 2025-08-08
## 6.5.0 - 2025-08-08
- feat: Add `$context_tags` to an event to know which properties were included as tags
# 6.4.1 - 2025-08-06
## 6.4.1 - 2025-08-06
- fix: Always pass project API key in `remote_config` requests for deterministic project routing
# 6.4.0 - 2025-08-05
## 6.4.0 - 2025-08-05
- feat: support Vertex AI for Gemini
# 6.3.4 - 2025-08-04
## 6.3.4 - 2025-08-04
- fix: set `$ai_tools` for all providers and `$ai_output_choices` for all non-streaming provider flows properly
# 6.3.3 - 2025-08-01
## 6.3.3 - 2025-08-01
- fix: `get_feature_flag_result` now correctly returns FeatureFlagResult when payload is empty string instead of None
# 6.3.2 - 2025-07-31
## 6.3.2 - 2025-07-31
- fix: Anthropic's tool calls are now handled properly
# 6.3.0 - 2025-07-22
## 6.3.0 - 2025-07-22
- feat: Enhanced `send_feature_flags` parameter to accept `SendFeatureFlagsOptions` object for declarative control over local/remote evaluation and custom properties
# 6.2.1 - 2025-07-21
## 6.2.1 - 2025-07-21
- feat: make `posthog_client` an optional argument in PostHog AI providers wrappers (`posthog.ai.*`), intuitively using the default client as the default
# 6.1.1 - 2025-07-16
## 6.1.1 - 2025-07-16
- fix: correctly capture exceptions processed by Django from views or middleware
# 6.1.0 - 2025-07-10
## 6.1.0 - 2025-07-10
- feat: decouple feature flag local evaluation from personal API keys; support decrypting remote config payloads without relying on the feature flags poller
# 6.0.4 - 2025-07-09
## 6.0.4 - 2025-07-09
- fix: add POSTHOG_MW_CLIENT setting to django middleware, to support custom clients for exception capture.
# 6.0.3 - 2025-07-07
## 6.0.3 - 2025-07-07
- feat: add a feature flag evaluation cache (local storage or redis) to support returning flag evaluations when the service is down
# 6.0.2 - 2025-07-02
## 6.0.2 - 2025-07-02
- fix: send_feature_flags changed to default to false in `Client::capture_exception`
# 6.0.1
## 6.0.1
- fix: response `$process_person_profile` property when passed to capture
# 6.0.0
## 6.0.0
This release contains a number of major breaking changes:
@@ -273,15 +336,15 @@ with posthog.new_context():
Generally, arguments are now appropriately typed, and docstrings have been updated. If something is unclear, please open an issue, or submit a PR!
# 5.4.0 - 2025-06-20
## 5.4.0 - 2025-06-20
- feat: add support to session_id context on page method
# 5.3.0 - 2025-06-19
## 5.3.0 - 2025-06-19
- fix: safely handle exception values
# 5.2.0 - 2025-06-19
## 5.2.0 - 2025-06-19
- feat: construct artificial stack traces if no traceback is available on a captured exception
Symlink
+1
View File
@@ -0,0 +1 @@
LLM.md
+48
View File
@@ -0,0 +1,48 @@
# Hanzo Insights Python SDK
## Overview
Integrate Hanzo Insights into any Python application. Package name: `hanzo-insights` on PyPI.
## Tech Stack
- **Language**: Python 3.10+
- **Package**: `hanzo_insights` (import name), `hanzo-insights` (pip name)
## Build & Run
```bash
uv sync
uv run pytest
```
## Structure
```
posthog-python/
hanzo_insights/ # Main package
__init__.py # Module-level API, Insights class
client.py # Client class
ai/ # AI provider integrations (OpenAI, Anthropic, Gemini, LangChain)
integrations/ # Framework integrations (Django middleware)
test/ # Tests
examples/
integration_tests/
pyproject.toml # Package config (name: hanzo-insights)
setup.py # Legacy setup
```
## Key Files
- `pyproject.toml` -- Package config, dependencies, test config
- `hanzo_insights/__init__.py` -- Public API surface
- `hanzo_insights/client.py` -- Client implementation
## Rebrand Notes
- Main class: `Insights` (no backward compat aliases)
- Django middleware: `InsightsContextMiddleware` (no backward compat aliases)
- OpenAI Agents: `InsightsTracingProcessor` (no backward compat aliases)
- `$lib` protocol value: `insights-python`
- Ingestion URLs: `us.i.insights.hanzo.ai` / `eu.i.insights.hanzo.ai`
- AI wrapper kwargs: `insights_*` (e.g. `insights_distinct_id`, `insights_trace_id`)
- Exception attrs: `__insights_exception_captured`, `__insights_exception_uuid`
- Context var: `insights_context_stack`
- Redis prefix: `insights:flags:`
- Redaction sentinels: `$$_insights_redacted_*`, `$$_insights_value_too_long_*`
- Django settings: `INSIGHTS_MW_*` only (no `POSTHOG_MW_*` fallback)
- Django headers: `X-INSIGHTS-SESSION-ID`, `X-INSIGHTS-DISTINCT-ID` only
+32 -18
View File
@@ -5,28 +5,42 @@ test:
coverage run -m pytest
coverage report
release:
build_release:
rm -rf dist/*
python setup.py sdist bdist_wheel
twine upload dist/*
release_analytics:
# Builds the `posthoganalytics` PyPI package, which is a mirror of `hanzo_insights`
# published under a different name for backward compatibility with the upstream
# posthog/posthog project.
#
# The process works in three phases:
# 1. hanzo_insights -> posthoganalytics: Copy the source, rewrite all imports,
# remove the original hanzo_insights/ dir, and build the dist.
# 2. posthoganalytics -> hanzo_insights: Reverse the import rewrites, copy
# everything back into hanzo_insights/, and clean up.
# 3. Restore pyproject.toml from backup (setup_analytics.py modifies it).
#
# This ensures the working tree is left in the same state it started in.
#
# NOTE: This target clears dist/ before building. In the release workflow,
# `build_release` (hanzo_insights) must be published BEFORE running this target,
# otherwise the hanzo_insights dist artifacts will be lost.
build_release_analytics:
rm -rf dist
rm -rf build
rm -rf posthoganalytics
mkdir posthoganalytics
cp -r posthog/* posthoganalytics/
find ./posthoganalytics -type f -name "*.py" -exec sed -i.bak -e 's/from posthog /from posthoganalytics /g' {} \;
find ./posthoganalytics -type f -name "*.py" -exec sed -i.bak -e 's/from posthog\./from posthoganalytics\./g' {} \;
cp -r hanzo_insights/* posthoganalytics/
find ./posthoganalytics -type f -name "*.py" -exec sed -i.bak -e 's/from hanzo_insights /from posthoganalytics /g' {} \;
find ./posthoganalytics -type f -name "*.py" -exec sed -i.bak -e 's/from hanzo_insights\./from posthoganalytics\./g' {} \;
find ./posthoganalytics -name "*.bak" -delete
rm -rf posthog
rm -rf hanzo_insights
python setup_analytics.py sdist bdist_wheel
twine upload dist/*
mkdir posthog
find ./posthoganalytics -type f -name "*.py" -exec sed -i.bak -e 's/from posthoganalytics /from posthog /g' {} \;
find ./posthoganalytics -type f -name "*.py" -exec sed -i.bak -e 's/from posthoganalytics\./from posthog\./g' {} \;
mkdir hanzo_insights
find ./posthoganalytics -type f -name "*.py" -exec sed -i.bak -e 's/from posthoganalytics /from hanzo_insights /g' {} \;
find ./posthoganalytics -type f -name "*.py" -exec sed -i.bak -e 's/from posthoganalytics\./from hanzo_insights\./g' {} \;
find ./posthoganalytics -name "*.bak" -delete
cp -r posthoganalytics/* posthog/
cp -r posthoganalytics/* hanzo_insights/
rm -rf posthoganalytics
rm -f pyproject.toml
cp pyproject.toml.backup pyproject.toml
@@ -41,17 +55,17 @@ prep_local:
cp -r . ../posthog-python-local/
cd ../posthog-python-local && rm -rf dist build posthoganalytics .git
cd ../posthog-python-local && mkdir posthoganalytics
cd ../posthog-python-local && cp -r posthog/* posthoganalytics/
cd ../posthog-python-local && find ./posthoganalytics -type f -name "*.py" -exec sed -i.bak -e 's/from posthog /from posthoganalytics /g' {} \;
cd ../posthog-python-local && find ./posthoganalytics -type f -name "*.py" -exec sed -i.bak -e 's/from posthog\./from posthoganalytics\./g' {} \;
cd ../posthog-python-local && cp -r hanzo_insights/* posthoganalytics/
cd ../posthog-python-local && find ./posthoganalytics -type f -name "*.py" -exec sed -i.bak -e 's/from hanzo_insights /from posthoganalytics /g' {} \;
cd ../posthog-python-local && find ./posthoganalytics -type f -name "*.py" -exec sed -i.bak -e 's/from hanzo_insights\./from posthoganalytics\./g' {} \;
cd ../posthog-python-local && find ./posthoganalytics -name "*.bak" -delete
cd ../posthog-python-local && rm -rf posthog
cd ../posthog-python-local && rm -rf hanzo_insights
cd ../posthog-python-local && sed -i.bak 's/from version import VERSION/from posthoganalytics.version import VERSION/' setup_analytics.py
cd ../posthog-python-local && rm setup_analytics.py.bak
cd ../posthog-python-local && sed -i.bak 's/"posthog"/"posthoganalytics"/' setup.py
cd ../posthog-python-local && sed -i.bak 's/"hanzo_insights"/"posthoganalytics"/' setup.py
cd ../posthog-python-local && rm setup.py.bak
cd ../posthog-python-local && python -c "import setup_analytics" 2>/dev/null || true
@echo "Local copy created at ../posthog-python-local"
@echo "Install with: pip install -e ../posthog-python-local"
.PHONY: test lint release e2e_test prep_local
.PHONY: test lint build_release build_release_analytics e2e_test prep_local
+51 -53
View File
@@ -1,41 +1,53 @@
# PostHog Python
<p align="center"><img src=".github/hero.svg" alt="insights-python" width="880"></p>
<p align="center">
<img alt="posthoglogo" src="https://user-images.githubusercontent.com/65415371/205059737-c8a4f836-4889-4654-902e-f302b187b6a0.png">
</p>
<p align="center">
<a href="https://pypi.org/project/posthog/"><img alt="pypi installs" src="https://img.shields.io/pypi/v/posthog"/></a>
<img alt="GitHub contributors" src="https://img.shields.io/github/contributors/posthog/posthog-python">
<img alt="GitHub commit activity" src="https://img.shields.io/github/commit-activity/m/posthog/posthog-python"/>
<img alt="GitHub closed issues" src="https://img.shields.io/github/issues-closed/posthog/posthog-python"/>
</p>
# Hanzo Insights Python SDK
Please see the [Python integration docs](https://posthog.com/docs/integrations/python-integration) for details.
Integrate [Hanzo Insights](https://insights.hanzo.ai) into any Python application.
## Installation
```bash
pip install hanzo-insights
```
## Quick Start
```python
from hanzo_insights import Insights
client = Insights('<your_project_api_key>', host='https://insights.hanzo.ai')
# Capture an event
client.capture('user_123', 'purchase', properties={'product': 'widget'})
# Feature flags
if client.feature_enabled('new-checkout', 'user_123'):
show_new_checkout()
```
## Module-level usage
```python
import hanzo_insights
hanzo_insights.api_key = '<your_project_api_key>'
hanzo_insights.host = 'https://insights.hanzo.ai'
hanzo_insights.capture('movie_played', distinct_id='user_123', properties={'movie_id': '42'})
hanzo_insights.shutdown()
```
## Python Version Support
| SDK Version | Python Versions Supported | Notes |
|-------------|---------------------------|-------|
| 7.3.1+ | 3.10, 3.11, 3.12, 3.13, 3.14 | Added Python 3.14 support |
| 7.0.0 - 7.0.1 | 3.10, 3.11, 3.12, 3.13 | Dropped Python 3.9 support |
| 4.0.1 - 6.x | 3.9, 3.10, 3.11, 3.12, 3.13 | Python 3.9+ required |
| SDK Version | Python Versions Supported |
| -------------- | ----------------------------- |
| 7.3.1+ | 3.10, 3.11, 3.12, 3.13, 3.14 |
| 7.0.0 - 7.0.1 | 3.10, 3.11, 3.12, 3.13 |
| 4.0.1 - 6.x | 3.9, 3.10, 3.11, 3.12, 3.13 |
## Development
### Testing Locally
We recommend using [uv](https://docs.astral.sh/uv/). It's super fast.
1. Run `uv venv env` (creates virtual environment called "env")
* or `python3 -m venv env`
2. Run `source env/bin/activate` (activates the virtual environment)
3. Run `uv sync --extra dev --extra test` (installs the package in develop mode, along with test dependencies)
* or `pip install -e ".[dev,test]"`
4. you have to run `pre-commit install` to have auto linting pre commit
5. Run `make test`
1. To run a specific test do `pytest -k test_no_api_key`
## PostHog recommends `uv` so...
We use [uv](https://docs.astral.sh/uv/).
```bash
uv python install 3.12
@@ -47,28 +59,14 @@ pre-commit install
make test
```
### Running Locally
### Running Tests
Assuming you have a [local version of PostHog](https://posthog.com/docs/developing-locally) running, you can run `python3 example.py` to see the library in action.
### Releasing Versions
Updates are released automatically using GitHub Actions when `version.py` is updated on `master`. After bumping `version.py` in `master` and adding to `CHANGELOG.md`, the [release workflow](https://github.com/PostHog/posthog-python/blob/master/.github/workflows/release.yaml) will automatically trigger and deploy the new version.
If you need to check the latest runs or manually trigger a release, you can go to [our release workflow's page](https://github.com/PostHog/posthog-python/actions/workflows/release.yaml) and dispatch it manually, using workflow from `master`.
### Testing changes locally with the PostHog app
You can run `make prep_local`, and it'll create a new folder alongside the SDK repo one called `posthog-python-local`, which you can then import into the posthog project by changing pyproject.toml to look like this:
```toml
dependencies = [
...
"posthoganalytics" #NOTE: no version number
...
]
...
[tools.uv.sources]
posthoganalytics = { path = "../posthog-python-local" }
```bash
make test
# or run a specific test:
pytest -k test_no_api_key
```
This'll let you build and test SDK changes fully locally, incorporating them into your local posthog app stack. It mainly takes care of the `posthog -> posthoganalytics` module renaming. You'll need to re-run `make prep_local` each time you make a change, and re-run `uv sync --active` in the posthog app project.
## License
MIT
+11
View File
@@ -0,0 +1,11 @@
# posthoganalytics
> **Do not use this package.** Use [`posthog`](https://pypi.org/project/posthog/) instead.
```bash
pip install posthog
```
This package exists solely for internal use by [posthog/posthog](https://github.com/posthog/posthog) to avoid import conflicts with the local `posthog` package in that repository. It is an automatically generated mirror of `posthog` — same code, same versions, just published under a different name.
If you are not working on the PostHog main repository, you should never need this package. All documentation, issues, and development happen in [`posthog-python`](https://github.com/posthog/posthog-python).
+1 -1
View File
@@ -4,5 +4,5 @@
source bin/helpers/_utils.sh
set_source_and_root_dir
flake8 posthog --ignore E501,W503
flake8 hanzo_insights --ignore E501,W503
mypy --no-site-packages --config-file mypy.ini . | mypy-baseline filter
+1 -1
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
#/ Usage: bin/docs
#/ Description: Generate documentation for the PostHog Python SDK
#/ Description: Generate documentation for the Insights Python SDK
source bin/helpers/_utils.sh
set_source_and_root_dir
ensure_virtual_env
+6 -6
View File
@@ -1,15 +1,15 @@
"""
Constants for PostHog Python SDK documentation generation.
Constants for Insights Python SDK documentation generation.
"""
from typing import Dict, Union
from posthog.version import VERSION
from hanzo_insights.version import VERSION
# Documentation generation metadata
DOCUMENTATION_METADATA = {
"hogRef": "0.3",
"slugPrefix": "posthog-python",
"specUrl": "https://github.com/PostHog/posthog-python",
"slugPrefix": "insights-python",
"specUrl": "https://github.com/Insights/insights-python",
}
# Docstring parsing patterns for new format
@@ -29,8 +29,8 @@ DOCSTRING_PATTERNS = {
# Output file configuration
OUTPUT_CONFIG: Dict[str, Union[str, int]] = {
"output_dir": "./references",
"filename": f"posthog-python-references-{VERSION}.json",
"filename_latest": "posthog-python-references-latest.json",
"filename": f"insights-python-references-{VERSION}.json",
"filename_latest": "insights-python-references-latest.json",
"indent": 2,
}
+26 -26
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
"""
Generate comprehensive SDK documentation JSON from PostHog Python SDK.
Generate comprehensive SDK documentation JSON from Insights Python SDK.
This script inspects the code and docstrings to create documentation in the specified format.
"""
@@ -337,19 +337,19 @@ def analyze_type(cls) -> dict:
def generate_sdk_documentation():
"""Generate complete SDK documentation in the requested format."""
# Import PostHog components
import posthog
from posthog.client import Client
import posthog.types as types_module
import posthog.args as args_module
from posthog.version import VERSION
# Import Insights components
import hanzo_insights
from hanzo_insights.client import Client
import hanzo_insights.types as types_module
import hanzo_insights.args as args_module
from hanzo_insights.version import VERSION
# Main SDK info
sdk_info = {
"version": VERSION,
"id": "posthog-python",
"title": "PostHog Python SDK",
"description": "Integrate PostHog into any python application.",
"id": "insights-python",
"title": "Insights Python SDK",
"description": "Integrate Insights into any python application.",
"slugPrefix": DOCUMENTATION_METADATA["slugPrefix"],
"specUrl": DOCUMENTATION_METADATA["specUrl"],
}
@@ -357,7 +357,7 @@ def generate_sdk_documentation():
# Collect types
types_list = []
# Types from posthog.types
# Types from hanzo_insights.types
for name in dir(types_module):
obj = getattr(types_module, name)
if inspect.isclass(obj) and not name.startswith("_"):
@@ -367,7 +367,7 @@ def generate_sdk_documentation():
except Exception as e:
print(f"Error analyzing type {name}: {e}")
# Types from posthog.args
# Types from hanzo_insights.args
for name in dir(args_module):
obj = getattr(args_module, name)
if inspect.isclass(obj) and not name.startswith("_"):
@@ -388,26 +388,26 @@ def generate_sdk_documentation():
# Collect classes
classes_list = []
# Main PostHog class (renamed from Client)
# Main Insights class (renamed from Client)
client_class = analyze_class(Client)
client_class["id"] = "PostHog"
client_class["title"] = "PostHog"
client_class["id"] = "Insights"
client_class["title"] = "Insights"
classes_list.append(client_class)
# Global module functions (functions callable as posthog.function_name)
# Global module functions (functions callable as hanzo_insights.function_name)
global_functions = []
for func_name in dir(posthog):
for func_name in dir(hanzo_insights):
# Skip private functions and non-callables
if func_name.startswith("_") or not callable(getattr(posthog, func_name)):
if func_name.startswith("_") or not callable(getattr(hanzo_insights, func_name)):
continue
func = getattr(posthog, func_name)
# Only include functions actually defined in the posthog module (not imported)
func = getattr(hanzo_insights, func_name)
# Only include functions actually defined in the hanzo_insights module (not imported)
# and exclude class references
if (
func_name not in ["Client", "Posthog"]
func_name not in ["Client", "Insights"]
and hasattr(func, "__module__")
and func.__module__ == "posthog"
and func.__module__ == "hanzo_insights"
):
try:
func_info = analyze_function(func, func_name)
@@ -421,8 +421,8 @@ def generate_sdk_documentation():
classes_list.append(
{
"id": "PostHogModule",
"title": "PostHog Module Functions",
"description": "Global functions available in the PostHog module",
"title": "Insights Module Functions",
"description": "Global functions available in the Insights module",
"functions": global_functions,
}
)
@@ -443,7 +443,7 @@ def generate_sdk_documentation():
# Create the final structure
result = {
"id": "posthog-python",
"id": "insights-python",
"hogRef": DOCUMENTATION_METADATA["hogRef"],
"info": sdk_info,
"types": types_list,
@@ -455,7 +455,7 @@ def generate_sdk_documentation():
if __name__ == "__main__":
print("Generating PostHog Python SDK documentation...")
print("Generating Insights Python SDK documentation...")
try:
documentation = generate_sdk_documentation()
+87 -87
View File
@@ -1,18 +1,18 @@
# PostHog Python library example
# Hanzo Insights Python library example
#
# This script demonstrates various PostHog Python SDK capabilities including:
# This script demonstrates various Hanzo Insights Python SDK capabilities including:
# - Basic event capture and user identification
# - Feature flag local evaluation
# - Feature flag payloads
# - Context management and tagging
#
# Setup:
# 1. Copy .env.example to .env and fill in your PostHog credentials
# 1. Copy .env.example to .env and fill in your Insights credentials
# 2. Run this script and choose from the interactive menu
import os
import posthog
import hanzo_insights
def load_env_file():
@@ -31,30 +31,30 @@ def load_env_file():
load_env_file()
# Get configuration
project_key = os.getenv("POSTHOG_PROJECT_API_KEY", "")
personal_api_key = os.getenv("POSTHOG_PERSONAL_API_KEY", "")
host = os.getenv("POSTHOG_HOST", "http://localhost:8000")
project_key = os.getenv("INSIGHTS_PROJECT_API_KEY", "")
personal_api_key = os.getenv("INSIGHTS_PERSONAL_API_KEY", "")
host = os.getenv("INSIGHTS_HOST", "http://localhost:8000")
# Check if project key is provided (required)
if not project_key:
print("❌ Missing PostHog project API key!")
print(" Please set POSTHOG_PROJECT_API_KEY environment variable")
print("❌ Missing Insights project API key!")
print(" Please set INSIGHTS_PROJECT_API_KEY environment variable")
print(" or copy .env.example to .env and fill in your values")
exit(1)
# Configure PostHog with credentials
posthog.debug = False
posthog.api_key = project_key
posthog.project_api_key = project_key
posthog.host = host
posthog.poll_interval = 10
# Configure Insights with credentials
hanzo_insights.debug = False
hanzo_insights.api_key = project_key
hanzo_insights.project_api_key = project_key
hanzo_insights.host = host
hanzo_insights.poll_interval = 10
# Check if personal API key is available for local evaluation
local_eval_available = bool(personal_api_key)
if personal_api_key:
posthog.personal_api_key = personal_api_key
hanzo_insights.personal_api_key = personal_api_key
print("🔑 PostHog Configuration:")
print("🔑 Insights Configuration:")
print(f" Project API Key: {project_key[:9]}...")
if local_eval_available:
print(" Personal API Key: [SET]")
@@ -63,7 +63,7 @@ else:
print(f" Host: {host}\n")
# Display menu and get user choice
print("🚀 PostHog Python SDK Demo - Choose an example to run:\n")
print("🚀 Hanzo Insights Python SDK Demo - Choose an example to run:\n")
print("1. Identify and capture examples")
local_eval_note = "" if local_eval_available else " [requires personal API key]"
print(f"2. Feature flag local evaluation examples{local_eval_note}")
@@ -79,11 +79,11 @@ if choice == "1":
print("IDENTIFY AND CAPTURE EXAMPLES")
print("=" * 60)
posthog.debug = True
hanzo_insights.debug = True
# Capture an event
print("📊 Capturing events...")
posthog.capture(
hanzo_insights.capture(
"event",
distinct_id="distinct_id",
properties={"property1": "value", "property2": "value"},
@@ -92,14 +92,14 @@ if choice == "1":
# Alias a previous distinct id with a new one
print("🔗 Creating alias...")
posthog.alias("distinct_id", "new_distinct_id")
hanzo_insights.alias("distinct_id", "new_distinct_id")
posthog.capture(
hanzo_insights.capture(
"event2",
distinct_id="new_distinct_id",
properties={"property1": "value", "property2": "value"},
)
posthog.capture(
hanzo_insights.capture(
"event-with-groups",
distinct_id="new_distinct_id",
properties={"property1": "value", "property2": "value"},
@@ -108,28 +108,28 @@ if choice == "1":
# Add properties to the person
print("👤 Identifying user...")
posthog.set(
hanzo_insights.set(
distinct_id="new_distinct_id", properties={"email": "something@something.com"}
)
# Add properties to a group
print("🏢 Identifying group...")
posthog.group_identify("company", "id:5", {"employees": 11})
hanzo_insights.group_identify("company", "id:5", {"employees": 11})
# Properties set only once to the person
print("🔒 Setting properties once...")
posthog.set_once(
hanzo_insights.set_once(
distinct_id="new_distinct_id", properties={"self_serve_signup": True}
)
# This will not change the property (because it was already set)
posthog.set_once(
hanzo_insights.set_once(
distinct_id="new_distinct_id", properties={"self_serve_signup": False}
)
print("🔄 Updating properties...")
posthog.set(distinct_id="new_distinct_id", properties={"current_browser": "Chrome"})
posthog.set(
hanzo_insights.set(distinct_id="new_distinct_id", properties={"current_browser": "Chrome"})
hanzo_insights.set(
distinct_id="new_distinct_id", properties={"current_browser": "Firefox"}
)
@@ -137,45 +137,45 @@ elif choice == "2":
if not local_eval_available:
print("\n❌ This example requires a personal API key for local evaluation.")
print(
" Set POSTHOG_PERSONAL_API_KEY environment variable to run this example."
" Set INSIGHTS_PERSONAL_API_KEY environment variable to run this example."
)
posthog.shutdown()
hanzo_insights.shutdown()
exit(1)
print("\n" + "=" * 60)
print("FEATURE FLAG LOCAL EVALUATION EXAMPLES")
print("=" * 60)
posthog.debug = True
hanzo_insights.debug = True
print("🏁 Testing basic feature flags...")
print(
f"beta-feature for 'distinct_id': {posthog.feature_enabled('beta-feature', 'distinct_id')}"
f"beta-feature for 'distinct_id': {hanzo_insights.feature_enabled('beta-feature', 'distinct_id')}"
)
print(
f"beta-feature for 'new_distinct_id': {posthog.feature_enabled('beta-feature', 'new_distinct_id')}"
f"beta-feature for 'new_distinct_id': {hanzo_insights.feature_enabled('beta-feature', 'new_distinct_id')}"
)
print(
f"beta-feature with groups: {posthog.feature_enabled('beta-feature-groups', 'distinct_id', groups={'company': 'id:5'})}"
f"beta-feature with groups: {hanzo_insights.feature_enabled('beta-feature-groups', 'distinct_id', groups={'company': 'id:5'})}"
)
print("\n🌍 Testing location-based flags...")
# Assume test-flag has `City Name = Sydney` as a person property set
print(
f"Sydney user: {posthog.feature_enabled('test-flag', 'random_id_12345', person_properties={'$geoip_city_name': 'Sydney'})}"
f"Sydney user: {hanzo_insights.feature_enabled('test-flag', 'random_id_12345', person_properties={'$geoip_city_name': 'Sydney'})}"
)
print(
f"Sydney user (local only): {posthog.feature_enabled('test-flag', 'distinct_id_random_22', person_properties={'$geoip_city_name': 'Sydney'}, only_evaluate_locally=True)}"
f"Sydney user (local only): {hanzo_insights.feature_enabled('test-flag', 'distinct_id_random_22', person_properties={'$geoip_city_name': 'Sydney'}, only_evaluate_locally=True)}"
)
print("\n📋 Getting all flags...")
print(f"All flags: {posthog.get_all_flags('distinct_id_random_22')}")
print(f"All flags: {hanzo_insights.get_all_flags('distinct_id_random_22')}")
print(
f"All flags (local): {posthog.get_all_flags('distinct_id_random_22', only_evaluate_locally=True)}"
f"All flags (local): {hanzo_insights.get_all_flags('distinct_id_random_22', only_evaluate_locally=True)}"
)
print(
f"All flags with properties: {posthog.get_all_flags('distinct_id_random_22', person_properties={'$geoip_city_name': 'Sydney'}, only_evaluate_locally=True)}"
f"All flags with properties: {hanzo_insights.get_all_flags('distinct_id_random_22', person_properties={'$geoip_city_name': 'Sydney'}, only_evaluate_locally=True)}"
)
elif choice == "3":
@@ -183,22 +183,22 @@ elif choice == "3":
print("FEATURE FLAG PAYLOAD EXAMPLES")
print("=" * 60)
posthog.debug = True
hanzo_insights.debug = True
print("📦 Testing feature flag payloads...")
print(
f"beta-feature payload: {posthog.get_feature_flag_payload('beta-feature', 'distinct_id')}"
f"beta-feature payload: {hanzo_insights.get_feature_flag_payload('beta-feature', 'distinct_id')}"
)
print(
f"All flags and payloads: {posthog.get_all_flags_and_payloads('distinct_id')}"
f"All flags and payloads: {hanzo_insights.get_all_flags_and_payloads('distinct_id')}"
)
print(
f"Remote config payload: {posthog.get_remote_config_payload('encrypted_payload_flag_key')}"
f"Remote config payload: {hanzo_insights.get_remote_config_payload('encrypted_payload_flag_key')}"
)
# Get feature flag result with all details (enabled, variant, payload, key, reason)
print("\n🔍 Getting detailed flag result...")
result = posthog.get_feature_flag_result("beta-feature", "distinct_id")
result = hanzo_insights.get_feature_flag_result("beta-feature", "distinct_id")
if result:
print(f"Flag key: {result.key}")
print(f"Flag enabled: {result.enabled}")
@@ -212,9 +212,9 @@ elif choice == "4":
if not local_eval_available:
print("\n❌ This example requires a personal API key for local evaluation.")
print(
" Set POSTHOG_PERSONAL_API_KEY environment variable to run this example."
" Set INSIGHTS_PERSONAL_API_KEY environment variable to run this example."
)
posthog.shutdown()
hanzo_insights.shutdown()
exit(1)
print("\n" + "=" * 60)
@@ -234,10 +234,10 @@ elif choice == "4":
print(" - Rollout: 100%")
print("")
posthog.debug = True
hanzo_insights.debug = True
# Test @example.com user (should satisfy dependency if flags exist)
result1 = posthog.feature_enabled(
result1 = hanzo_insights.feature_enabled(
"test-flag-dependency",
"example_user",
person_properties={"email": "user@example.com"},
@@ -246,7 +246,7 @@ elif choice == "4":
print(f"✅ @example.com user (test-flag-dependency): {result1}")
# Test non-example.com user (dependency should not be satisfied)
result2 = posthog.feature_enabled(
result2 = hanzo_insights.feature_enabled(
"test-flag-dependency",
"regular_user",
person_properties={"email": "user@other.com"},
@@ -255,13 +255,13 @@ elif choice == "4":
print(f"❌ Regular user (test-flag-dependency): {result2}")
# Test beta-feature directly for comparison
beta1 = posthog.feature_enabled(
beta1 = hanzo_insights.feature_enabled(
"beta-feature",
"example_user",
person_properties={"email": "user@example.com"},
only_evaluate_locally=True,
)
beta2 = posthog.feature_enabled(
beta2 = hanzo_insights.feature_enabled(
"beta-feature",
"regular_user",
person_properties={"email": "user@other.com"},
@@ -303,7 +303,7 @@ elif choice == "4":
print("")
# Test pineapple -> blue -> breaking-bad chain
dependent_result3 = posthog.get_feature_flag(
dependent_result3 = hanzo_insights.get_feature_flag(
"multivariate-root-flag",
"regular_user",
person_properties={"email": "pineapple@example.com"},
@@ -317,7 +317,7 @@ elif choice == "4":
print("'multivariate-root-flag' with email pineapple@example.com succeeded")
# Test mango -> red -> the-wire chain
dependent_result4 = posthog.get_feature_flag(
dependent_result4 = hanzo_insights.get_feature_flag(
"multivariate-root-flag",
"regular_user",
person_properties={"email": "mango@example.com"},
@@ -336,19 +336,19 @@ elif choice == "4":
("pineapple@example.com", ["pineapple", "blue", "breaking-bad"]),
("mango@example.com", ["mango", "red", "the-wire"]),
]:
leaf = posthog.get_feature_flag(
leaf = hanzo_insights.get_feature_flag(
"multivariate-leaf-flag",
"regular_user",
person_properties={"email": email},
only_evaluate_locally=True,
)
intermediate = posthog.get_feature_flag(
intermediate = hanzo_insights.get_feature_flag(
"multivariate-intermediate-flag",
"regular_user",
person_properties={"email": email},
only_evaluate_locally=True,
)
root = posthog.get_feature_flag(
root = hanzo_insights.get_feature_flag(
"multivariate-root-flag",
"regular_user",
person_properties={"email": email},
@@ -373,7 +373,7 @@ elif choice == "5":
print("CONTEXT MANAGEMENT AND TAGGING EXAMPLES")
print("=" * 60)
posthog.debug = True
hanzo_insights.debug = True
print("🏷️ Testing context management...")
print(
@@ -384,12 +384,12 @@ elif choice == "5":
# and tagged with the context tags. Other events captured will also be tagged with the context tags. By default,
# the new context inherits tags from the parent context.
try:
with posthog.new_context():
posthog.tag("transaction_id", "abc123")
posthog.tag("some_arbitrary_value", {"tags": "can be dicts"})
with hanzo_insights.new_context():
hanzo_insights.tag("transaction_id", "abc123")
hanzo_insights.tag("some_arbitrary_value", {"tags": "can be dicts"})
# This event will be captured with the tags set above
posthog.capture("order_processed")
hanzo_insights.capture("order_processed")
print("✅ Event captured with inherited context tags")
# This exception will be captured with the tags set above
# raise Exception("Order processing failed")
@@ -398,30 +398,30 @@ elif choice == "5":
# Use fresh=True to start with a clean context (no inherited tags)
try:
with posthog.new_context(fresh=True):
posthog.tag("session_id", "xyz789")
with hanzo_insights.new_context(fresh=True):
hanzo_insights.tag("session_id", "xyz789")
# Only session_id tag will be present, no inherited tags
posthog.capture("session_event")
hanzo_insights.capture("session_event")
print("✅ Event captured with fresh context tags")
# raise Exception("Session handling failed")
except Exception as e:
print(f"Exception captured: {e}")
# You can also use the `@posthog.scoped()` decorator to enter a new context.
# You can also use the `@hanzo_insights.scoped()` decorator to enter a new context.
# By default, it inherits tags from the parent context
@posthog.scoped()
@hanzo_insights.scoped()
def process_order(order_id):
posthog.tag("order_id", order_id)
posthog.capture("order_step_completed")
hanzo_insights.tag("order_id", order_id)
hanzo_insights.capture("order_step_completed")
print(f"✅ Order {order_id} processed with scoped context")
# Exception will be captured and tagged automatically
# raise Exception("Order processing failed")
# Use fresh=True to start with a clean context (no inherited tags)
@posthog.scoped(fresh=True)
@hanzo_insights.scoped(fresh=True)
def process_payment(payment_id):
posthog.tag("payment_id", payment_id)
posthog.capture("payment_processed")
hanzo_insights.tag("payment_id", payment_id)
hanzo_insights.capture("payment_processed")
print(f"✅ Payment {payment_id} processed with fresh scoped context")
# Only payment_id tag will be present, no inherited tags
# raise Exception("Payment processing failed")
@@ -436,18 +436,18 @@ elif choice == "6":
# Run example 1
print(f"\n{'🔸' * 20} IDENTIFY AND CAPTURE {'🔸' * 20}")
posthog.debug = True
hanzo_insights.debug = True
print("📊 Capturing events...")
posthog.capture(
hanzo_insights.capture(
"event",
distinct_id="distinct_id",
properties={"property1": "value", "property2": "value"},
send_feature_flags=True,
)
print("🔗 Creating alias...")
posthog.alias("distinct_id", "new_distinct_id")
hanzo_insights.alias("distinct_id", "new_distinct_id")
print("👤 Identifying user...")
posthog.set(
hanzo_insights.set(
distinct_id="new_distinct_id", properties={"email": "something@something.com"}
)
@@ -455,27 +455,27 @@ elif choice == "6":
if local_eval_available:
print(f"\n{'🔸' * 20} FEATURE FLAGS {'🔸' * 20}")
print("🏁 Testing basic feature flags...")
print(f"beta-feature: {posthog.feature_enabled('beta-feature', 'distinct_id')}")
print(f"beta-feature: {hanzo_insights.feature_enabled('beta-feature', 'distinct_id')}")
print(
f"Sydney user: {posthog.feature_enabled('test-flag', 'random_id_12345', person_properties={'$geoip_city_name': 'Sydney'})}"
f"Sydney user: {hanzo_insights.feature_enabled('test-flag', 'random_id_12345', person_properties={'$geoip_city_name': 'Sydney'})}"
)
# Run example 3
print(f"\n{'🔸' * 20} PAYLOADS {'🔸' * 20}")
print("📦 Testing payloads...")
print(f"Payload: {posthog.get_feature_flag_payload('beta-feature', 'distinct_id')}")
print(f"Payload: {hanzo_insights.get_feature_flag_payload('beta-feature', 'distinct_id')}")
# Run example 4 (requires local evaluation)
if local_eval_available:
print(f"\n{'🔸' * 20} FLAG DEPENDENCIES {'🔸' * 20}")
print("🔗 Testing flag dependencies...")
result1 = posthog.feature_enabled(
result1 = hanzo_insights.feature_enabled(
"test-flag-dependency",
"demo_user",
person_properties={"email": "user@example.com"},
only_evaluate_locally=True,
)
result2 = posthog.feature_enabled(
result2 = hanzo_insights.feature_enabled(
"test-flag-dependency",
"demo_user2",
person_properties={"email": "user@other.com"},
@@ -486,23 +486,23 @@ elif choice == "6":
# Run example 5
print(f"\n{'🔸' * 20} CONTEXT MANAGEMENT {'🔸' * 20}")
print("🏷️ Testing context management...")
with posthog.new_context():
posthog.tag("demo_run", "all_examples")
posthog.capture("demo_completed")
with hanzo_insights.new_context():
hanzo_insights.tag("demo_run", "all_examples")
hanzo_insights.capture("demo_completed")
print("✅ Demo completed with context tags")
elif choice == "7":
print("👋 Goodbye!")
posthog.shutdown()
hanzo_insights.shutdown()
exit()
else:
print("❌ Invalid choice. Please run again and select 1-7.")
posthog.shutdown()
hanzo_insights.shutdown()
exit()
print("\n" + "=" * 60)
print("✅ Example completed!")
print("=" * 60)
posthog.shutdown()
hanzo_insights.shutdown()
+11 -11
View File
@@ -1,17 +1,17 @@
"""
Redis-based distributed cache for PostHog feature flag definitions.
Redis-based distributed cache for Insights feature flag definitions.
This example demonstrates how to implement a FlagDefinitionCacheProvider
using Redis for multi-instance deployments (leader election pattern).
Usage:
import redis
from posthog import Posthog
from hanzo_insights import Insights
redis_client = redis.Redis(host='localhost', port=6379, decode_responses=True)
cache = RedisFlagCache(redis_client, service_key="my-service")
posthog = Posthog(
client = Insights(
"<project_api_key>",
personal_api_key="<personal_api_key>",
flag_definition_cache_provider=cache,
@@ -24,17 +24,17 @@ Requirements:
import json
import uuid
from posthog import FlagDefinitionCacheData, FlagDefinitionCacheProvider
from hanzo_insights import FlagDefinitionCacheData, FlagDefinitionCacheProvider
from redis import Redis
from typing import Optional
class RedisFlagCache(FlagDefinitionCacheProvider):
"""
A distributed cache for PostHog feature flag definitions using Redis.
A distributed cache for Insights feature flag definitions using Redis.
In a multi-instance deployment (e.g., multiple serverless functions or containers),
we want only ONE instance to poll PostHog for flag updates, while all instances
we want only ONE instance to poll Insights for flag updates, while all instances
share the cached results. This prevents N instances from making N redundant API calls.
The implementation uses leader election:
@@ -83,8 +83,8 @@ class RedisFlagCache(FlagDefinitionCacheProvider):
Examples: "my-api-prod", "checkout-service", "staging".
Redis Keys Created:
- posthog:flags:{service_key} - Cached flag definitions (JSON)
- posthog:flags:{service_key}:lock - Leader election lock
- insights:flags:{service_key} - Cached flag definitions (JSON)
- insights:flags:{service_key}:lock - Leader election lock
Example:
redis_client = redis.Redis(
@@ -95,8 +95,8 @@ class RedisFlagCache(FlagDefinitionCacheProvider):
cache = RedisFlagCache(redis_client, service_key="my-api-prod")
"""
self._redis = redis
self._cache_key = f"posthog:flags:{service_key}"
self._lock_key = f"posthog:flags:{service_key}:lock"
self._cache_key = f"insights:flags:{service_key}"
self._lock_key = f"insights:flags:{service_key}:lock"
self._instance_id = str(uuid.uuid4())
self._try_lead = self._redis.register_script(self._LUA_TRY_LEAD)
self._stop_lead = self._redis.register_script(self._LUA_STOP_LEAD)
@@ -113,7 +113,7 @@ class RedisFlagCache(FlagDefinitionCacheProvider):
def should_fetch_flag_definitions(self) -> bool:
"""
Determines if this instance should fetch flag definitions from PostHog.
Determines if this instance should fetch flag definitions from Insights.
Atomically either:
- Acquires the lock if no one holds it, OR
+8 -8
View File
@@ -1,15 +1,15 @@
#!/usr/bin/env python3
"""
Simple test script for PostHog remote config endpoint.
Simple test script for Insights remote config endpoint.
"""
import posthog
import hanzo_insights
# Initialize PostHog client
posthog.api_key = "phc_..."
posthog.personal_api_key = "phs_..." # or "phx_..."
posthog.host = "http://localhost:8000" # or "https://us.posthog.com"
posthog.debug = True
# Initialize Insights client
hanzo_insights.api_key = "phc_..."
hanzo_insights.personal_api_key = "phs_..." # or "phx_..."
hanzo_insights.host = "http://localhost:8000" # or "https://us.insights.hanzo.ai"
hanzo_insights.debug = True
def test_remote_config():
@@ -21,7 +21,7 @@ def test_remote_config():
try:
# Get remote config payload
payload = posthog.get_remote_config_payload(flag_key)
payload = hanzo_insights.get_remote_config_payload(flag_key)
print(f"✅ Success! Remote config payload for '{flag_key}': {payload}")
except Exception as e:
@@ -3,66 +3,66 @@ from typing import Any, Callable, Dict, Optional # noqa: F401
from typing_extensions import Unpack
from posthog.args import ExceptionArg, OptionalCaptureArgs, OptionalSetArgs
from posthog.client import Client
from posthog.contexts import (
from hanzo_insights.args import ExceptionArg, OptionalCaptureArgs, OptionalSetArgs
from hanzo_insights.client import Client
from hanzo_insights.contexts import (
identify_context as inner_identify_context,
)
from posthog.contexts import (
from hanzo_insights.contexts import (
new_context as inner_new_context,
)
from posthog.contexts import (
from hanzo_insights.contexts import (
scoped as inner_scoped,
)
from posthog.contexts import (
from hanzo_insights.contexts import (
set_capture_exception_code_variables_context as inner_set_capture_exception_code_variables_context,
)
from posthog.contexts import (
from hanzo_insights.contexts import (
set_code_variables_ignore_patterns_context as inner_set_code_variables_ignore_patterns_context,
)
from posthog.contexts import (
from hanzo_insights.contexts import (
set_code_variables_mask_patterns_context as inner_set_code_variables_mask_patterns_context,
)
from posthog.contexts import (
from hanzo_insights.contexts import (
set_context_device_id as inner_set_context_device_id,
)
from posthog.contexts import (
from hanzo_insights.contexts import (
set_context_session as inner_set_context_session,
)
from posthog.contexts import (
from hanzo_insights.contexts import (
tag as inner_tag,
)
from posthog.contexts import (
from hanzo_insights.contexts import (
get_tags as inner_get_tags,
)
from posthog.exception_utils import (
from hanzo_insights.exception_utils import (
DEFAULT_CODE_VARIABLES_IGNORE_PATTERNS,
DEFAULT_CODE_VARIABLES_MASK_PATTERNS,
)
from posthog.feature_flags import (
from hanzo_insights.feature_flags import (
InconclusiveMatchError as InconclusiveMatchError,
)
from posthog.feature_flags import (
from hanzo_insights.feature_flags import (
RequiresServerEvaluation as RequiresServerEvaluation,
)
from posthog.flag_definition_cache import (
from hanzo_insights.flag_definition_cache import (
FlagDefinitionCacheData as FlagDefinitionCacheData,
FlagDefinitionCacheProvider as FlagDefinitionCacheProvider,
)
from posthog.request import (
from hanzo_insights.request import (
disable_connection_reuse as disable_connection_reuse,
enable_keep_alive as enable_keep_alive,
set_socket_options as set_socket_options,
SocketOptions as SocketOptions,
)
from posthog.types import (
from hanzo_insights.types import (
FeatureFlag,
FlagsAndPayloads,
)
from posthog.types import (
from hanzo_insights.types import (
FeatureFlagResult as FeatureFlagResult,
)
from posthog.version import VERSION
from hanzo_insights.version import VERSION
__version__ = VERSION
@@ -76,11 +76,11 @@ def new_context(fresh=False, capture_exceptions=True, client=None):
Args:
fresh: Whether to start with a fresh context (default: False)
capture_exceptions: Whether to capture exceptions raised within the context (default: True)
client: Optional Posthog client instance to use for this context (default: None)
client: Optional Insights client instance to use for this context (default: None)
Examples:
```python
from posthog import new_context, tag, capture
from hanzo_insights import new_context, tag, capture
with new_context():
tag("request_id", "123")
capture("event_name", properties={"property": "value"})
@@ -100,11 +100,11 @@ def scoped(fresh=False, capture_exceptions=True):
Args:
fresh: Whether to start with a fresh context (default: False)
capture_exceptions: Whether to capture and track exceptions with posthog error tracking (default: True)
capture_exceptions: Whether to capture and track exceptions with Insights error tracking (default: True)
Examples:
```python
from posthog import scoped, tag, capture
from hanzo_insights import scoped, tag, capture
@scoped()
def process_payment(payment_id):
tag("payment_id", payment_id)
@@ -126,7 +126,7 @@ def set_context_session(session_id: str):
Examples:
```python
from posthog import set_context_session
from hanzo_insights import set_context_session
set_context_session("session_123")
```
@@ -146,7 +146,7 @@ def set_context_device_id(device_id: str):
Examples:
```python
from posthog import set_context_device_id
from hanzo_insights import set_context_device_id
set_context_device_id("device_123")
```
@@ -165,7 +165,7 @@ def identify_context(distinct_id: str):
Examples:
```python
from posthog import identify_context
from hanzo_insights import identify_context
identify_context("user_123")
```
@@ -206,7 +206,7 @@ def tag(name: str, value: Any):
Examples:
```python
from posthog import tag
from hanzo_insights import tag
tag("user_id", "123")
```
@@ -263,7 +263,7 @@ in_app_modules = None # type: Optional[list[str]]
# NOTE - this and following functions take unpacked kwargs because we needed to make
# it impossible to write `posthog.capture(distinct-id, event-name)` - basically, to enforce
# it impossible to write `hanzo_insights.capture(distinct-id, event-name)` - basically, to enforce
# the breaking change made between 5.3.0 and 6.0.0. This decision can be unrolled in later
# versions, without a breaking change, to get back the type information in function signatures
def capture(event: str, **kwargs: Unpack[OptionalCaptureArgs]) -> Optional[str]:
@@ -280,12 +280,12 @@ def capture(event: str, **kwargs: Unpack[OptionalCaptureArgs]) -> Optional[str]:
disable_geoip: Whether to disable GeoIP lookup
Details:
Capture allows you to capture anything a user does within your system, which you can later use in PostHog to find patterns in usage, work out which features to improve or where people are giving up. A capture call requires an event name to specify the event. We recommend using [verb] [noun], like `movie played` or `movie updated` to easily identify what your events mean later on. Capture takes a number of optional arguments, which are defined by the `OptionalCaptureArgs` type.
Capture allows you to capture anything a user does within your system, which you can later use in Insights to find patterns in usage, work out which features to improve or where people are giving up. A capture call requires an event name to specify the event. We recommend using [verb] [noun], like `movie played` or `movie updated` to easily identify what your events mean later on. Capture takes a number of optional arguments, which are defined by the `OptionalCaptureArgs` type.
Examples:
```python
# Context and capture usage
from posthog import new_context, identify_context, tag_context, capture
from hanzo_insights import new_context, identify_context, tag_context, capture
# Enter a new context (e.g. a request/response cycle, an instance of a background job, etc)
with new_context():
# Associate this context with some user, by distinct_id
@@ -312,7 +312,7 @@ def capture(event: str, **kwargs: Unpack[OptionalCaptureArgs]) -> Optional[str]:
```
```python
# Set event properties
from posthog import capture
from hanzo_insights import capture
capture(
"user_signed_up",
distinct_id="distinct_id_of_the_user",
@@ -339,7 +339,7 @@ def set(**kwargs: Unpack[OptionalSetArgs]) -> Optional[str]:
Examples:
```python
# Set person properties
from posthog import capture
from hanzo_insights import capture
capture(
'distinct_id',
event='event_name',
@@ -366,7 +366,7 @@ def set_once(**kwargs: Unpack[OptionalSetArgs]) -> Optional[str]:
Examples:
```python
# Set property once
from posthog import capture
from hanzo_insights import capture
capture(
'distinct_id',
event='event_name',
@@ -406,7 +406,7 @@ def group_identify(
Examples:
```python
# Group identify
from posthog import group_identify
from hanzo_insights import group_identify
group_identify('company', 'company_id_in_your_db', {
'name': 'Awesome Inc.',
'employees': 11
@@ -451,7 +451,7 @@ def alias(
Examples:
```python
# Alias user
from posthog import alias
from hanzo_insights import alias
alias(previous_id='distinct_id', distinct_id='alias_id')
```
Category:
@@ -479,12 +479,12 @@ def capture_exception(
exception: The exception to capture. If not provided, the current exception is captured via `sys.exc_info()`
Details:
Capture exception is idempotent - if it is called twice with the same exception instance, only a occurrence will be tracked in posthog. This is because, generally, contexts will cause exceptions to be captured automatically. However, to ensure you track an exception, if you catch and do not re-raise it, capturing it manually is recommended, unless you are certain it will have crossed a context boundary (e.g. by existing a `with posthog.new_context():` block already). If the passed exception was raised and caught, the captured stack trace will consist of every frame between where the exception was raised and the point at which it is captured (the "traceback"). If the passed exception was never raised, e.g. if you call `posthog.capture_exception(ValueError("Some Error"))`, the stack trace captured will be the full stack trace at the moment the exception was captured. Note that heavy use of contexts will lead to truncated stack traces, as the exception will be captured by the context entered most recently, which may not be the point you catch the exception for the final time in your code. It's recommended to use contexts sparingly, for this reason. `capture_exception` takes the same set of optional arguments as `capture`.
Capture exception is idempotent - if it is called twice with the same exception instance, only a occurrence will be tracked in hanzo_insights. This is because, generally, contexts will cause exceptions to be captured automatically. However, to ensure you track an exception, if you catch and do not re-raise it, capturing it manually is recommended, unless you are certain it will have crossed a context boundary (e.g. by existing a `with hanzo_insights.new_context():` block already). If the passed exception was raised and caught, the captured stack trace will consist of every frame between where the exception was raised and the point at which it is captured (the "traceback"). If the passed exception was never raised, e.g. if you call `hanzo_insights.capture_exception(ValueError("Some Error"))`, the stack trace captured will be the full stack trace at the moment the exception was captured. Note that heavy use of contexts will lead to truncated stack traces, as the exception will be captured by the context entered most recently, which may not be the point you catch the exception for the final time in your code. It's recommended to use contexts sparingly, for this reason. `capture_exception` takes the same set of optional arguments as `capture`.
Examples:
```python
# Capture exception
from posthog import capture_exception
from hanzo_insights import capture_exception
try:
risky_operation()
except Exception as e:
@@ -523,12 +523,12 @@ def feature_enabled(
disable_geoip: Whether to disable GeoIP lookup
Details:
You can call `posthog.load_feature_flags()` before to make sure you're not doing unexpected requests.
You can call `hanzo_insights.load_feature_flags()` before to make sure you're not doing unexpected requests.
Examples:
```python
# Boolean feature flag
from posthog import feature_enabled, get_feature_flag_payload
from hanzo_insights import feature_enabled, get_feature_flag_payload
is_my_flag_enabled = feature_enabled('flag-key', 'distinct_id_of_your_user')
if is_my_flag_enabled:
matched_flag_payload = get_feature_flag_payload('flag-key', 'distinct_id_of_your_user')
@@ -575,12 +575,12 @@ def get_feature_flag(
disable_geoip: Whether to disable GeoIP lookup
Details:
`groups` are a mapping from group type to group key. So, if you have a group type of "organization" and a group key of "5", you would pass groups={"organization": "5"}. `group_properties` take the format: { group_type_name: { group_properties } }. So, for example, if you have the group type "organization" and the group key "5", with the properties name, and employee count, you'll send these as: group_properties={"organization": {"name": "PostHog", "employees": 11}}.
`groups` are a mapping from group type to group key. So, if you have a group type of "organization" and a group key of "5", you would pass groups={"organization": "5"}. `group_properties` take the format: { group_type_name: { group_properties } }. So, for example, if you have the group type "organization" and the group key "5", with the properties name, and employee count, you'll send these as: group_properties={"organization": {"name": "Hanzo", "employees": 11}}.
Examples:
```python
# Multivariate feature flag
from posthog import get_feature_flag, get_feature_flag_payload
from hanzo_insights import get_feature_flag, get_feature_flag_payload
enabled_variant = get_feature_flag('flag-key', 'distinct_id_of_your_user')
if enabled_variant == 'variant-key':
matched_flag_payload = get_feature_flag_payload('flag-key', 'distinct_id_of_your_user')
@@ -628,7 +628,7 @@ def get_all_flags(
Examples:
```python
# All flags for user
from posthog import get_all_flags
from hanzo_insights import get_all_flags
get_all_flags('distinct_id_of_your_user')
```
Category:
@@ -670,7 +670,7 @@ def get_feature_flag_result(
Example:
```python
result = posthog.get_feature_flag_result('beta-feature', 'distinct_id')
result = hanzo_insights.get_feature_flag_result('beta-feature', 'distinct_id')
if result and result.enabled:
# Use the variant and payload
print(f"Variant: {result.variant}")
@@ -768,7 +768,7 @@ def feature_flag_definitions():
Examples:
```python
from posthog import feature_flag_definitions
from hanzo_insights import feature_flag_definitions
definitions = feature_flag_definitions()
```
@@ -780,11 +780,11 @@ def feature_flag_definitions():
def load_feature_flags():
"""
Load feature flag definitions from PostHog.
Load feature flag definitions from the server.
Examples:
```python
from posthog import load_feature_flags
from hanzo_insights import load_feature_flags
load_feature_flags()
```
@@ -800,7 +800,7 @@ def flush():
Examples:
```python
from posthog import flush
from hanzo_insights import flush
flush()
```
@@ -816,7 +816,7 @@ def join():
Examples:
```python
from posthog import join
from hanzo_insights import join
join()
```
@@ -832,7 +832,7 @@ def shutdown():
Examples:
```python
from posthog import shutdown
from hanzo_insights import shutdown
shutdown()
```
@@ -888,5 +888,7 @@ def _proxy(method, *args, **kwargs):
return fn(*args, **kwargs)
class Posthog(Client):
class Insights(Client):
"""Hanzo Insights client for product analytics."""
pass
+3
View File
@@ -0,0 +1,3 @@
from hanzo_insights.ai.prompts import Prompts
__all__ = ["Prompts"]
@@ -10,38 +10,38 @@ import time
import uuid
from typing import Any, Dict, List, Optional
from posthog.ai.types import StreamingContentBlock, TokenUsage, ToolInProgress
from posthog.ai.utils import (
from hanzo_insights.ai.types import StreamingContentBlock, TokenUsage, ToolInProgress
from hanzo_insights.ai.utils import (
call_llm_and_track_usage,
merge_usage_stats,
)
from posthog.ai.anthropic.anthropic_converter import (
from hanzo_insights.ai.anthropic.anthropic_converter import (
extract_anthropic_usage_from_event,
handle_anthropic_content_block_start,
handle_anthropic_text_delta,
handle_anthropic_tool_delta,
finalize_anthropic_tool_input,
)
from posthog.ai.sanitization import sanitize_anthropic
from posthog.client import Client as PostHogClient
from posthog import setup
from hanzo_insights.ai.sanitization import sanitize_anthropic
from hanzo_insights.client import Client as InsightsClient
from hanzo_insights import setup
class Anthropic(anthropic.Anthropic):
"""
A wrapper around the Anthropic SDK that automatically sends LLM usage events to PostHog.
A wrapper around the Anthropic SDK that automatically sends LLM usage events to Insights.
"""
_ph_client: PostHogClient
_ph_client: InsightsClient
def __init__(self, posthog_client: Optional[PostHogClient] = None, **kwargs):
def __init__(self, insights_client: Optional[InsightsClient] = None, **kwargs):
"""
Args:
posthog_client: PostHog client for tracking usage
insights_client: Insights client for tracking usage
**kwargs: Additional arguments passed to the Anthropic client
"""
super().__init__(**kwargs)
self._ph_client = posthog_client or setup()
self._ph_client = insights_client or setup()
self.messages = WrappedMessages(self)
@@ -50,46 +50,46 @@ class WrappedMessages(Messages):
def create(
self,
posthog_distinct_id: Optional[str] = None,
posthog_trace_id: Optional[str] = None,
posthog_properties: Optional[Dict[str, Any]] = None,
posthog_privacy_mode: bool = False,
posthog_groups: Optional[Dict[str, Any]] = None,
insights_distinct_id: Optional[str] = None,
insights_trace_id: Optional[str] = None,
insights_properties: Optional[Dict[str, Any]] = None,
insights_privacy_mode: bool = False,
insights_groups: Optional[Dict[str, Any]] = None,
**kwargs: Any,
):
"""
Create a message using Anthropic's API while tracking usage in PostHog.
Create a message using Anthropic's API while tracking usage in Insights.
Args:
posthog_distinct_id: Optional ID to associate with the usage event
posthog_trace_id: Optional trace UUID for linking events
posthog_properties: Optional dictionary of extra properties to include in the event
posthog_privacy_mode: Whether to redact sensitive information in tracking
posthog_groups: Optional group analytics properties
insights_distinct_id: Optional ID to associate with the usage event
insights_trace_id: Optional trace UUID for linking events
insights_properties: Optional dictionary of extra properties to include in the event
insights_privacy_mode: Whether to redact sensitive information in tracking
insights_groups: Optional group analytics properties
**kwargs: Arguments passed to Anthropic's messages.create
"""
if posthog_trace_id is None:
posthog_trace_id = str(uuid.uuid4())
if insights_trace_id is None:
insights_trace_id = str(uuid.uuid4())
if kwargs.get("stream", False):
return self._create_streaming(
posthog_distinct_id,
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_distinct_id,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
**kwargs,
)
return call_llm_and_track_usage(
posthog_distinct_id,
insights_distinct_id,
self._client._ph_client,
"anthropic",
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
self._client.base_url,
super().create,
**kwargs,
@@ -97,32 +97,32 @@ class WrappedMessages(Messages):
def stream(
self,
posthog_distinct_id: Optional[str] = None,
posthog_trace_id: Optional[str] = None,
posthog_properties: Optional[Dict[str, Any]] = None,
posthog_privacy_mode: bool = False,
posthog_groups: Optional[Dict[str, Any]] = None,
insights_distinct_id: Optional[str] = None,
insights_trace_id: Optional[str] = None,
insights_properties: Optional[Dict[str, Any]] = None,
insights_privacy_mode: bool = False,
insights_groups: Optional[Dict[str, Any]] = None,
**kwargs: Any,
):
if posthog_trace_id is None:
posthog_trace_id = str(uuid.uuid4())
if insights_trace_id is None:
insights_trace_id = str(uuid.uuid4())
return self._create_streaming(
posthog_distinct_id,
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_distinct_id,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
**kwargs,
)
def _create_streaming(
self,
posthog_distinct_id: Optional[str],
posthog_trace_id: Optional[str],
posthog_properties: Optional[Dict[str, Any]],
posthog_privacy_mode: bool,
posthog_groups: Optional[Dict[str, Any]],
insights_distinct_id: Optional[str],
insights_trace_id: Optional[str],
insights_properties: Optional[Dict[str, Any]],
insights_privacy_mode: bool,
insights_groups: Optional[Dict[str, Any]],
**kwargs: Any,
):
start_time = time.time()
@@ -188,11 +188,11 @@ class WrappedMessages(Messages):
latency = end_time - start_time
self._capture_streaming_event(
posthog_distinct_id,
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_distinct_id,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
kwargs,
usage_stats,
latency,
@@ -204,23 +204,23 @@ class WrappedMessages(Messages):
def _capture_streaming_event(
self,
posthog_distinct_id: Optional[str],
posthog_trace_id: Optional[str],
posthog_properties: Optional[Dict[str, Any]],
posthog_privacy_mode: bool,
posthog_groups: Optional[Dict[str, Any]],
insights_distinct_id: Optional[str],
insights_trace_id: Optional[str],
insights_properties: Optional[Dict[str, Any]],
insights_privacy_mode: bool,
insights_groups: Optional[Dict[str, Any]],
kwargs: Dict[str, Any],
usage_stats: TokenUsage,
latency: float,
content_blocks: List[StreamingContentBlock],
accumulated_content: str,
):
from posthog.ai.types import StreamingEventData
from posthog.ai.anthropic.anthropic_converter import (
from hanzo_insights.ai.types import StreamingEventData
from hanzo_insights.ai.anthropic.anthropic_converter import (
format_anthropic_streaming_input,
format_anthropic_streaming_output_complete,
)
from posthog.ai.utils import capture_streaming_event
from hanzo_insights.ai.utils import capture_streaming_event
# Prepare standardized event data
formatted_input = format_anthropic_streaming_input(kwargs)
@@ -237,11 +237,11 @@ class WrappedMessages(Messages):
),
usage_stats=usage_stats,
latency=latency,
distinct_id=posthog_distinct_id,
trace_id=posthog_trace_id,
properties=posthog_properties,
privacy_mode=posthog_privacy_mode,
groups=posthog_groups,
distinct_id=insights_distinct_id,
trace_id=insights_trace_id,
properties=insights_properties,
privacy_mode=insights_privacy_mode,
groups=insights_groups,
)
# Use the common capture function
@@ -10,38 +10,38 @@ import time
import uuid
from typing import Any, Dict, List, Optional
from posthog import setup
from posthog.ai.types import StreamingContentBlock, TokenUsage, ToolInProgress
from posthog.ai.utils import (
from hanzo_insights import setup
from hanzo_insights.ai.types import StreamingContentBlock, TokenUsage, ToolInProgress
from hanzo_insights.ai.utils import (
call_llm_and_track_usage_async,
merge_usage_stats,
)
from posthog.ai.anthropic.anthropic_converter import (
from hanzo_insights.ai.anthropic.anthropic_converter import (
extract_anthropic_usage_from_event,
handle_anthropic_content_block_start,
handle_anthropic_text_delta,
handle_anthropic_tool_delta,
finalize_anthropic_tool_input,
)
from posthog.ai.sanitization import sanitize_anthropic
from posthog.client import Client as PostHogClient
from hanzo_insights.ai.sanitization import sanitize_anthropic
from hanzo_insights.client import Client as InsightsClient
class AsyncAnthropic(anthropic.AsyncAnthropic):
"""
An async wrapper around the Anthropic SDK that automatically sends LLM usage events to PostHog.
An async wrapper around the Anthropic SDK that automatically sends LLM usage events to Insights.
"""
_ph_client: PostHogClient
_ph_client: InsightsClient
def __init__(self, posthog_client: Optional[PostHogClient] = None, **kwargs):
def __init__(self, insights_client: Optional[InsightsClient] = None, **kwargs):
"""
Args:
posthog_client: PostHog client for tracking usage
insights_client: Insights client for tracking usage
**kwargs: Additional arguments passed to the Anthropic client
"""
super().__init__(**kwargs)
self._ph_client = posthog_client or setup()
self._ph_client = insights_client or setup()
self.messages = AsyncWrappedMessages(self)
@@ -50,46 +50,46 @@ class AsyncWrappedMessages(AsyncMessages):
async def create(
self,
posthog_distinct_id: Optional[str] = None,
posthog_trace_id: Optional[str] = None,
posthog_properties: Optional[Dict[str, Any]] = None,
posthog_privacy_mode: bool = False,
posthog_groups: Optional[Dict[str, Any]] = None,
insights_distinct_id: Optional[str] = None,
insights_trace_id: Optional[str] = None,
insights_properties: Optional[Dict[str, Any]] = None,
insights_privacy_mode: bool = False,
insights_groups: Optional[Dict[str, Any]] = None,
**kwargs: Any,
):
"""
Create a message using Anthropic's API while tracking usage in PostHog.
Create a message using Anthropic's API while tracking usage in Insights.
Args:
posthog_distinct_id: Optional ID to associate with the usage event
posthog_trace_id: Optional trace UUID for linking events
posthog_properties: Optional dictionary of extra properties to include in the event
posthog_privacy_mode: Whether to redact sensitive information in tracking
posthog_groups: Optional group analytics properties
insights_distinct_id: Optional ID to associate with the usage event
insights_trace_id: Optional trace UUID for linking events
insights_properties: Optional dictionary of extra properties to include in the event
insights_privacy_mode: Whether to redact sensitive information in tracking
insights_groups: Optional group analytics properties
**kwargs: Arguments passed to Anthropic's messages.create
"""
if posthog_trace_id is None:
posthog_trace_id = str(uuid.uuid4())
if insights_trace_id is None:
insights_trace_id = str(uuid.uuid4())
if kwargs.get("stream", False):
return await self._create_streaming(
posthog_distinct_id,
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_distinct_id,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
**kwargs,
)
return await call_llm_and_track_usage_async(
posthog_distinct_id,
insights_distinct_id,
self._client._ph_client,
"anthropic",
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
self._client.base_url,
super().create,
**kwargs,
@@ -97,32 +97,32 @@ class AsyncWrappedMessages(AsyncMessages):
async def stream(
self,
posthog_distinct_id: Optional[str] = None,
posthog_trace_id: Optional[str] = None,
posthog_properties: Optional[Dict[str, Any]] = None,
posthog_privacy_mode: bool = False,
posthog_groups: Optional[Dict[str, Any]] = None,
insights_distinct_id: Optional[str] = None,
insights_trace_id: Optional[str] = None,
insights_properties: Optional[Dict[str, Any]] = None,
insights_privacy_mode: bool = False,
insights_groups: Optional[Dict[str, Any]] = None,
**kwargs: Any,
):
if posthog_trace_id is None:
posthog_trace_id = str(uuid.uuid4())
if insights_trace_id is None:
insights_trace_id = str(uuid.uuid4())
return await self._create_streaming(
posthog_distinct_id,
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_distinct_id,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
**kwargs,
)
async def _create_streaming(
self,
posthog_distinct_id: Optional[str],
posthog_trace_id: Optional[str],
posthog_properties: Optional[Dict[str, Any]],
posthog_privacy_mode: bool,
posthog_groups: Optional[Dict[str, Any]],
insights_distinct_id: Optional[str],
insights_trace_id: Optional[str],
insights_properties: Optional[Dict[str, Any]],
insights_privacy_mode: bool,
insights_groups: Optional[Dict[str, Any]],
**kwargs: Any,
):
start_time = time.time()
@@ -188,11 +188,11 @@ class AsyncWrappedMessages(AsyncMessages):
latency = end_time - start_time
await self._capture_streaming_event(
posthog_distinct_id,
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_distinct_id,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
kwargs,
usage_stats,
latency,
@@ -204,23 +204,23 @@ class AsyncWrappedMessages(AsyncMessages):
async def _capture_streaming_event(
self,
posthog_distinct_id: Optional[str],
posthog_trace_id: Optional[str],
posthog_properties: Optional[Dict[str, Any]],
posthog_privacy_mode: bool,
posthog_groups: Optional[Dict[str, Any]],
insights_distinct_id: Optional[str],
insights_trace_id: Optional[str],
insights_properties: Optional[Dict[str, Any]],
insights_privacy_mode: bool,
insights_groups: Optional[Dict[str, Any]],
kwargs: Dict[str, Any],
usage_stats: TokenUsage,
latency: float,
content_blocks: List[StreamingContentBlock],
accumulated_content: str,
):
from posthog.ai.types import StreamingEventData
from posthog.ai.anthropic.anthropic_converter import (
from hanzo_insights.ai.types import StreamingEventData
from hanzo_insights.ai.anthropic.anthropic_converter import (
format_anthropic_streaming_input,
format_anthropic_streaming_output_complete,
)
from posthog.ai.utils import capture_streaming_event
from hanzo_insights.ai.utils import capture_streaming_event
# Prepare standardized event data
formatted_input = format_anthropic_streaming_input(kwargs)
@@ -237,11 +237,11 @@ class AsyncWrappedMessages(AsyncMessages):
),
usage_stats=usage_stats,
latency=latency,
distinct_id=posthog_distinct_id,
trace_id=posthog_trace_id,
properties=posthog_properties,
privacy_mode=posthog_privacy_mode,
groups=posthog_groups,
distinct_id=insights_distinct_id,
trace_id=insights_trace_id,
properties=insights_properties,
privacy_mode=insights_privacy_mode,
groups=insights_groups,
)
# Use the common capture function
@@ -2,13 +2,13 @@
Anthropic-specific conversion utilities.
This module handles the conversion of Anthropic API responses and inputs
into standardized formats for PostHog tracking.
into standardized formats for Insights tracking.
"""
import json
from typing import Any, Dict, List, Optional, Tuple
from posthog.ai.types import (
from hanzo_insights.ai.types import (
FormattedContentItem,
FormattedFunctionCall,
FormattedMessage,
@@ -17,7 +17,7 @@ from posthog.ai.types import (
TokenUsage,
ToolInProgress,
)
from posthog.ai.utils import serialize_raw_usage
from hanzo_insights.ai.utils import serialize_raw_usage
def format_anthropic_response(response: Any) -> List[FormattedMessage]:
@@ -425,9 +425,9 @@ def format_anthropic_streaming_input(kwargs: Dict[str, Any]) -> Any:
kwargs: Keyword arguments passed to Anthropic API
Returns:
Formatted input ready for PostHog tracking
Formatted input ready for Insights tracking
"""
from posthog.ai.utils import merge_system_prompt
from hanzo_insights.ai.utils import merge_system_prompt
return merge_system_prompt(kwargs, "anthropic")
@@ -445,7 +445,7 @@ def format_anthropic_streaming_output_complete(
accumulated_content: Raw accumulated text content as fallback
Returns:
Formatted messages ready for PostHog tracking
Formatted messages ready for Insights tracking
"""
formatted_content = format_anthropic_streaming_content(content_blocks)
@@ -7,59 +7,59 @@ except ImportError:
from typing import Optional
from posthog.ai.anthropic.anthropic import WrappedMessages
from posthog.ai.anthropic.anthropic_async import AsyncWrappedMessages
from posthog.client import Client as PostHogClient
from posthog import setup
from hanzo_insights.ai.anthropic.anthropic import WrappedMessages
from hanzo_insights.ai.anthropic.anthropic_async import AsyncWrappedMessages
from hanzo_insights.client import Client as InsightsClient
from hanzo_insights import setup
class AnthropicBedrock(anthropic.AnthropicBedrock):
"""
A wrapper around the Anthropic Bedrock SDK that automatically sends LLM usage events to PostHog.
A wrapper around the Anthropic Bedrock SDK that automatically sends LLM usage events to Insights.
"""
_ph_client: PostHogClient
_ph_client: InsightsClient
def __init__(self, posthog_client: Optional[PostHogClient] = None, **kwargs):
def __init__(self, insights_client: Optional[InsightsClient] = None, **kwargs):
super().__init__(**kwargs)
self._ph_client = posthog_client or setup()
self._ph_client = insights_client or setup()
self.messages = WrappedMessages(self)
class AsyncAnthropicBedrock(anthropic.AsyncAnthropicBedrock):
"""
A wrapper around the Anthropic Bedrock SDK that automatically sends LLM usage events to PostHog.
A wrapper around the Anthropic Bedrock SDK that automatically sends LLM usage events to Insights.
"""
_ph_client: PostHogClient
_ph_client: InsightsClient
def __init__(self, posthog_client: Optional[PostHogClient] = None, **kwargs):
def __init__(self, insights_client: Optional[InsightsClient] = None, **kwargs):
super().__init__(**kwargs)
self._ph_client = posthog_client or setup()
self._ph_client = insights_client or setup()
self.messages = AsyncWrappedMessages(self)
class AnthropicVertex(anthropic.AnthropicVertex):
"""
A wrapper around the Anthropic Vertex SDK that automatically sends LLM usage events to PostHog.
A wrapper around the Anthropic Vertex SDK that automatically sends LLM usage events to Insights.
"""
_ph_client: PostHogClient
_ph_client: InsightsClient
def __init__(self, posthog_client: Optional[PostHogClient] = None, **kwargs):
def __init__(self, insights_client: Optional[InsightsClient] = None, **kwargs):
super().__init__(**kwargs)
self._ph_client = posthog_client or setup()
self._ph_client = insights_client or setup()
self.messages = WrappedMessages(self)
class AsyncAnthropicVertex(anthropic.AsyncAnthropicVertex):
"""
A wrapper around the Anthropic Vertex SDK that automatically sends LLM usage events to PostHog.
A wrapper around the Anthropic Vertex SDK that automatically sends LLM usage events to Insights.
"""
_ph_client: PostHogClient
_ph_client: InsightsClient
def __init__(self, posthog_client: Optional[PostHogClient] = None, **kwargs):
def __init__(self, insights_client: Optional[InsightsClient] = None, **kwargs):
super().__init__(**kwargs)
self._ph_client = posthog_client or setup()
self._ph_client = insights_client or setup()
self.messages = AsyncWrappedMessages(self)
@@ -3,8 +3,8 @@ import time
import uuid
from typing import Any, Dict, Optional
from posthog.ai.types import TokenUsage, StreamingEventData
from posthog.ai.utils import merge_system_prompt
from hanzo_insights.ai.types import TokenUsage, StreamingEventData
from hanzo_insights.ai.utils import merge_system_prompt
try:
from google import genai
@@ -13,40 +13,40 @@ except ImportError:
"Please install the Google Gemini SDK to use this feature: 'pip install google-genai'"
)
from posthog import setup
from posthog.ai.utils import (
from hanzo_insights import setup
from hanzo_insights.ai.utils import (
call_llm_and_track_usage,
capture_streaming_event,
merge_usage_stats,
)
from posthog.ai.gemini.gemini_converter import (
from hanzo_insights.ai.gemini.gemini_converter import (
extract_gemini_usage_from_chunk,
extract_gemini_content_from_chunk,
format_gemini_streaming_output,
)
from posthog.ai.sanitization import sanitize_gemini
from posthog.client import Client as PostHogClient
from hanzo_insights.ai.sanitization import sanitize_gemini
from hanzo_insights.client import Client as InsightsClient
class Client:
"""
A drop-in replacement for genai.Client that automatically sends LLM usage events to PostHog.
A drop-in replacement for genai.Client that automatically sends LLM usage events to Insights.
Usage:
client = Client(
api_key="your_api_key",
posthog_client=posthog_client,
posthog_distinct_id="default_user", # Optional defaults
posthog_properties={"team": "ai"} # Optional defaults
insights_client=insights_client,
insights_distinct_id="default_user", # Optional defaults
insights_properties={"team": "ai"} # Optional defaults
)
response = client.models.generate_content(
model="gemini-2.0-flash",
contents=["Hello world"],
posthog_distinct_id="specific_user" # Override default
insights_distinct_id="specific_user" # Override default
)
"""
_ph_client: PostHogClient
_ph_client: InsightsClient
def __init__(
self,
@@ -57,11 +57,11 @@ class Client:
location: Optional[str] = None,
debug_config: Optional[Any] = None,
http_options: Optional[Any] = None,
posthog_client: Optional[PostHogClient] = None,
posthog_distinct_id: Optional[str] = None,
posthog_properties: Optional[Dict[str, Any]] = None,
posthog_privacy_mode: bool = False,
posthog_groups: Optional[Dict[str, Any]] = None,
insights_client: Optional[InsightsClient] = None,
insights_distinct_id: Optional[str] = None,
insights_properties: Optional[Dict[str, Any]] = None,
insights_privacy_mode: bool = False,
insights_groups: Optional[Dict[str, Any]] = None,
**kwargs,
):
"""
@@ -73,18 +73,18 @@ class Client:
location: GCP location for Vertex AI
debug_config: Debug configuration for the client
http_options: HTTP options for the client
posthog_client: PostHog client for tracking usage
posthog_distinct_id: Default distinct ID for all calls (can be overridden per call)
posthog_properties: Default properties for all calls (can be overridden per call)
posthog_privacy_mode: Default privacy mode for all calls (can be overridden per call)
posthog_groups: Default groups for all calls (can be overridden per call)
insights_client: Insights client for tracking usage
insights_distinct_id: Default distinct ID for all calls (can be overridden per call)
insights_properties: Default properties for all calls (can be overridden per call)
insights_privacy_mode: Default privacy mode for all calls (can be overridden per call)
insights_groups: Default groups for all calls (can be overridden per call)
**kwargs: Additional arguments (for future compatibility)
"""
self._ph_client = posthog_client or setup()
self._ph_client = insights_client or setup()
if self._ph_client is None:
raise ValueError("posthog_client is required for PostHog tracking")
raise ValueError("insights_client is required for Insights tracking")
self.models = Models(
api_key=api_key,
@@ -94,21 +94,21 @@ class Client:
location=location,
debug_config=debug_config,
http_options=http_options,
posthog_client=self._ph_client,
posthog_distinct_id=posthog_distinct_id,
posthog_properties=posthog_properties,
posthog_privacy_mode=posthog_privacy_mode,
posthog_groups=posthog_groups,
insights_client=self._ph_client,
insights_distinct_id=insights_distinct_id,
insights_properties=insights_properties,
insights_privacy_mode=insights_privacy_mode,
insights_groups=insights_groups,
**kwargs,
)
class Models:
"""
Models interface that mimics genai.Client().models with PostHog tracking.
Models interface that mimics genai.Client().models with Insights tracking.
"""
_ph_client: PostHogClient # Not None after __init__ validation
_ph_client: InsightsClient # Not None after __init__ validation
def __init__(
self,
@@ -119,11 +119,11 @@ class Models:
location: Optional[str] = None,
debug_config: Optional[Any] = None,
http_options: Optional[Any] = None,
posthog_client: Optional[PostHogClient] = None,
posthog_distinct_id: Optional[str] = None,
posthog_properties: Optional[Dict[str, Any]] = None,
posthog_privacy_mode: bool = False,
posthog_groups: Optional[Dict[str, Any]] = None,
insights_client: Optional[InsightsClient] = None,
insights_distinct_id: Optional[str] = None,
insights_properties: Optional[Dict[str, Any]] = None,
insights_privacy_mode: bool = False,
insights_groups: Optional[Dict[str, Any]] = None,
**kwargs,
):
"""
@@ -135,24 +135,24 @@ class Models:
location: GCP location for Vertex AI
debug_config: Debug configuration for the client
http_options: HTTP options for the client
posthog_client: PostHog client for tracking usage
posthog_distinct_id: Default distinct ID for all calls
posthog_properties: Default properties for all calls
posthog_privacy_mode: Default privacy mode for all calls
posthog_groups: Default groups for all calls
insights_client: Insights client for tracking usage
insights_distinct_id: Default distinct ID for all calls
insights_properties: Default properties for all calls
insights_privacy_mode: Default privacy mode for all calls
insights_groups: Default groups for all calls
**kwargs: Additional arguments (for future compatibility)
"""
self._ph_client = posthog_client or setup()
self._ph_client = insights_client or setup()
if self._ph_client is None:
raise ValueError("posthog_client is required for PostHog tracking")
raise ValueError("insights_client is required for Insights tracking")
# Store default PostHog settings
self._default_distinct_id = posthog_distinct_id
self._default_properties = posthog_properties or {}
self._default_privacy_mode = posthog_privacy_mode
self._default_groups = posthog_groups
# Store default Insights settings
self._default_distinct_id = insights_distinct_id
self._default_properties = insights_properties or {}
self._default_privacy_mode = insights_privacy_mode
self._default_groups = insights_groups
# Build genai.Client arguments
client_args: Dict[str, Any] = {}
@@ -196,7 +196,7 @@ class Models:
self._client = genai.Client(**client_args)
self._base_url = "https://generativelanguage.googleapis.com"
def _merge_posthog_params(
def _merge_insights_params(
self,
call_distinct_id: Optional[str],
call_trace_id: Optional[str],
@@ -204,7 +204,7 @@ class Models:
call_privacy_mode: Optional[bool],
call_groups: Optional[Dict[str, Any]],
):
"""Merge call-level PostHog parameters with client defaults."""
"""Merge call-level Insights parameters with client defaults."""
# Use call-level values if provided, otherwise fall back to defaults
distinct_id = (
@@ -234,38 +234,38 @@ class Models:
self,
model: str,
contents,
posthog_distinct_id: Optional[str] = None,
posthog_trace_id: Optional[str] = None,
posthog_properties: Optional[Dict[str, Any]] = None,
posthog_privacy_mode: Optional[bool] = None,
posthog_groups: Optional[Dict[str, Any]] = None,
insights_distinct_id: Optional[str] = None,
insights_trace_id: Optional[str] = None,
insights_properties: Optional[Dict[str, Any]] = None,
insights_privacy_mode: Optional[bool] = None,
insights_groups: Optional[Dict[str, Any]] = None,
**kwargs: Any,
):
"""
Generate content using Gemini's API while tracking usage in PostHog.
Generate content using Gemini's API while tracking usage in Insights.
This method signature exactly matches genai.Client().models.generate_content()
with additional PostHog tracking parameters.
with additional Insights tracking parameters.
Args:
model: The model to use (e.g., 'gemini-2.0-flash')
contents: The input content for generation
posthog_distinct_id: ID to associate with the usage event (overrides client default)
posthog_trace_id: Trace UUID for linking events (auto-generated if not provided)
posthog_properties: Extra properties to include in the event (merged with client defaults)
posthog_privacy_mode: Whether to redact sensitive information (overrides client default)
posthog_groups: Group analytics properties (overrides client default)
insights_distinct_id: ID to associate with the usage event (overrides client default)
insights_trace_id: Trace UUID for linking events (auto-generated if not provided)
insights_properties: Extra properties to include in the event (merged with client defaults)
insights_privacy_mode: Whether to redact sensitive information (overrides client default)
insights_groups: Group analytics properties (overrides client default)
**kwargs: Arguments passed to Gemini's generate_content
"""
# Merge PostHog parameters
# Merge Insights parameters
distinct_id, trace_id, properties, privacy_mode, groups = (
self._merge_posthog_params(
posthog_distinct_id,
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
self._merge_insights_params(
insights_distinct_id,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
)
)
@@ -380,7 +380,7 @@ class Models:
capture_streaming_event(self._ph_client, event_data)
def _format_input(self, contents, **kwargs):
"""Format input contents for PostHog tracking"""
"""Format input contents for Insights tracking"""
# Create kwargs dict with contents for merge_system_prompt
input_kwargs = {"contents": contents, **kwargs}
@@ -390,21 +390,21 @@ class Models:
self,
model: str,
contents,
posthog_distinct_id: Optional[str] = None,
posthog_trace_id: Optional[str] = None,
posthog_properties: Optional[Dict[str, Any]] = None,
posthog_privacy_mode: Optional[bool] = None,
posthog_groups: Optional[Dict[str, Any]] = None,
insights_distinct_id: Optional[str] = None,
insights_trace_id: Optional[str] = None,
insights_properties: Optional[Dict[str, Any]] = None,
insights_privacy_mode: Optional[bool] = None,
insights_groups: Optional[Dict[str, Any]] = None,
**kwargs: Any,
):
# Merge PostHog parameters
# Merge Insights parameters
distinct_id, trace_id, properties, privacy_mode, groups = (
self._merge_posthog_params(
posthog_distinct_id,
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
self._merge_insights_params(
insights_distinct_id,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
)
)
@@ -3,8 +3,8 @@ import time
import uuid
from typing import Any, Dict, Optional
from posthog.ai.types import TokenUsage, StreamingEventData
from posthog.ai.utils import merge_system_prompt
from hanzo_insights.ai.types import TokenUsage, StreamingEventData
from hanzo_insights.ai.utils import merge_system_prompt
try:
from google import genai
@@ -13,40 +13,40 @@ except ImportError:
"Please install the Google Gemini SDK to use this feature: 'pip install google-genai'"
)
from posthog import setup
from posthog.ai.utils import (
from hanzo_insights import setup
from hanzo_insights.ai.utils import (
call_llm_and_track_usage_async,
capture_streaming_event,
merge_usage_stats,
)
from posthog.ai.gemini.gemini_converter import (
from hanzo_insights.ai.gemini.gemini_converter import (
extract_gemini_usage_from_chunk,
extract_gemini_content_from_chunk,
format_gemini_streaming_output,
)
from posthog.ai.sanitization import sanitize_gemini
from posthog.client import Client as PostHogClient
from hanzo_insights.ai.sanitization import sanitize_gemini
from hanzo_insights.client import Client as InsightsClient
class AsyncClient:
"""
An async drop-in replacement for genai.Client that automatically sends LLM usage events to PostHog.
An async drop-in replacement for genai.Client that automatically sends LLM usage events to Insights.
Usage:
client = AsyncClient(
api_key="your_api_key",
posthog_client=posthog_client,
posthog_distinct_id="default_user", # Optional defaults
posthog_properties={"team": "ai"} # Optional defaults
insights_client=insights_client,
insights_distinct_id="default_user", # Optional defaults
insights_properties={"team": "ai"} # Optional defaults
)
response = await client.models.generate_content(
model="gemini-2.0-flash",
contents=["Hello world"],
posthog_distinct_id="specific_user" # Override default
insights_distinct_id="specific_user" # Override default
)
"""
_ph_client: PostHogClient
_ph_client: InsightsClient
def __init__(
self,
@@ -57,11 +57,11 @@ class AsyncClient:
location: Optional[str] = None,
debug_config: Optional[Any] = None,
http_options: Optional[Any] = None,
posthog_client: Optional[PostHogClient] = None,
posthog_distinct_id: Optional[str] = None,
posthog_properties: Optional[Dict[str, Any]] = None,
posthog_privacy_mode: bool = False,
posthog_groups: Optional[Dict[str, Any]] = None,
insights_client: Optional[InsightsClient] = None,
insights_distinct_id: Optional[str] = None,
insights_properties: Optional[Dict[str, Any]] = None,
insights_privacy_mode: bool = False,
insights_groups: Optional[Dict[str, Any]] = None,
**kwargs,
):
"""
@@ -73,18 +73,18 @@ class AsyncClient:
location: GCP location for Vertex AI
debug_config: Debug configuration for the client
http_options: HTTP options for the client
posthog_client: PostHog client for tracking usage
posthog_distinct_id: Default distinct ID for all calls (can be overridden per call)
posthog_properties: Default properties for all calls (can be overridden per call)
posthog_privacy_mode: Default privacy mode for all calls (can be overridden per call)
posthog_groups: Default groups for all calls (can be overridden per call)
insights_client: Insights client for tracking usage
insights_distinct_id: Default distinct ID for all calls (can be overridden per call)
insights_properties: Default properties for all calls (can be overridden per call)
insights_privacy_mode: Default privacy mode for all calls (can be overridden per call)
insights_groups: Default groups for all calls (can be overridden per call)
**kwargs: Additional arguments (for future compatibility)
"""
self._ph_client = posthog_client or setup()
self._ph_client = insights_client or setup()
if self._ph_client is None:
raise ValueError("posthog_client is required for PostHog tracking")
raise ValueError("insights_client is required for Insights tracking")
self.models = AsyncModels(
api_key=api_key,
@@ -94,21 +94,21 @@ class AsyncClient:
location=location,
debug_config=debug_config,
http_options=http_options,
posthog_client=self._ph_client,
posthog_distinct_id=posthog_distinct_id,
posthog_properties=posthog_properties,
posthog_privacy_mode=posthog_privacy_mode,
posthog_groups=posthog_groups,
insights_client=self._ph_client,
insights_distinct_id=insights_distinct_id,
insights_properties=insights_properties,
insights_privacy_mode=insights_privacy_mode,
insights_groups=insights_groups,
**kwargs,
)
class AsyncModels:
"""
Async Models interface that mimics genai.Client().aio.models with PostHog tracking.
Async Models interface that mimics genai.Client().aio.models with Insights tracking.
"""
_ph_client: PostHogClient # Not None after __init__ validation
_ph_client: InsightsClient # Not None after __init__ validation
def __init__(
self,
@@ -119,11 +119,11 @@ class AsyncModels:
location: Optional[str] = None,
debug_config: Optional[Any] = None,
http_options: Optional[Any] = None,
posthog_client: Optional[PostHogClient] = None,
posthog_distinct_id: Optional[str] = None,
posthog_properties: Optional[Dict[str, Any]] = None,
posthog_privacy_mode: bool = False,
posthog_groups: Optional[Dict[str, Any]] = None,
insights_client: Optional[InsightsClient] = None,
insights_distinct_id: Optional[str] = None,
insights_properties: Optional[Dict[str, Any]] = None,
insights_privacy_mode: bool = False,
insights_groups: Optional[Dict[str, Any]] = None,
**kwargs,
):
"""
@@ -135,24 +135,24 @@ class AsyncModels:
location: GCP location for Vertex AI
debug_config: Debug configuration for the client
http_options: HTTP options for the client
posthog_client: PostHog client for tracking usage
posthog_distinct_id: Default distinct ID for all calls
posthog_properties: Default properties for all calls
posthog_privacy_mode: Default privacy mode for all calls
posthog_groups: Default groups for all calls
insights_client: Insights client for tracking usage
insights_distinct_id: Default distinct ID for all calls
insights_properties: Default properties for all calls
insights_privacy_mode: Default privacy mode for all calls
insights_groups: Default groups for all calls
**kwargs: Additional arguments (for future compatibility)
"""
self._ph_client = posthog_client or setup()
self._ph_client = insights_client or setup()
if self._ph_client is None:
raise ValueError("posthog_client is required for PostHog tracking")
raise ValueError("insights_client is required for Insights tracking")
# Store default PostHog settings
self._default_distinct_id = posthog_distinct_id
self._default_properties = posthog_properties or {}
self._default_privacy_mode = posthog_privacy_mode
self._default_groups = posthog_groups
# Store default Insights settings
self._default_distinct_id = insights_distinct_id
self._default_properties = insights_properties or {}
self._default_privacy_mode = insights_privacy_mode
self._default_groups = insights_groups
# Build genai.Client arguments
client_args: Dict[str, Any] = {}
@@ -196,7 +196,7 @@ class AsyncModels:
self._client = genai.Client(**client_args)
self._base_url = "https://generativelanguage.googleapis.com"
def _merge_posthog_params(
def _merge_insights_params(
self,
call_distinct_id: Optional[str],
call_trace_id: Optional[str],
@@ -204,7 +204,7 @@ class AsyncModels:
call_privacy_mode: Optional[bool],
call_groups: Optional[Dict[str, Any]],
):
"""Merge call-level PostHog parameters with client defaults."""
"""Merge call-level Insights parameters with client defaults."""
# Use call-level values if provided, otherwise fall back to defaults
distinct_id = (
@@ -234,38 +234,38 @@ class AsyncModels:
self,
model: str,
contents,
posthog_distinct_id: Optional[str] = None,
posthog_trace_id: Optional[str] = None,
posthog_properties: Optional[Dict[str, Any]] = None,
posthog_privacy_mode: Optional[bool] = None,
posthog_groups: Optional[Dict[str, Any]] = None,
insights_distinct_id: Optional[str] = None,
insights_trace_id: Optional[str] = None,
insights_properties: Optional[Dict[str, Any]] = None,
insights_privacy_mode: Optional[bool] = None,
insights_groups: Optional[Dict[str, Any]] = None,
**kwargs: Any,
):
"""
Generate content using Gemini's API while tracking usage in PostHog.
Generate content using Gemini's API while tracking usage in Insights.
This method signature exactly matches genai.Client().aio.models.generate_content()
with additional PostHog tracking parameters.
with additional Insights tracking parameters.
Args:
model: The model to use (e.g., 'gemini-2.0-flash')
contents: The input content for generation
posthog_distinct_id: ID to associate with the usage event (overrides client default)
posthog_trace_id: Trace UUID for linking events (auto-generated if not provided)
posthog_properties: Extra properties to include in the event (merged with client defaults)
posthog_privacy_mode: Whether to redact sensitive information (overrides client default)
posthog_groups: Group analytics properties (overrides client default)
insights_distinct_id: ID to associate with the usage event (overrides client default)
insights_trace_id: Trace UUID for linking events (auto-generated if not provided)
insights_properties: Extra properties to include in the event (merged with client defaults)
insights_privacy_mode: Whether to redact sensitive information (overrides client default)
insights_groups: Group analytics properties (overrides client default)
**kwargs: Arguments passed to Gemini's generate_content
"""
# Merge PostHog parameters
# Merge Insights parameters
distinct_id, trace_id, properties, privacy_mode, groups = (
self._merge_posthog_params(
posthog_distinct_id,
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
self._merge_insights_params(
insights_distinct_id,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
)
)
@@ -383,7 +383,7 @@ class AsyncModels:
capture_streaming_event(self._ph_client, event_data)
def _format_input(self, contents, **kwargs):
"""Format input contents for PostHog tracking"""
"""Format input contents for Insights tracking"""
# Create kwargs dict with contents for merge_system_prompt
input_kwargs = {"contents": contents, **kwargs}
@@ -393,21 +393,21 @@ class AsyncModels:
self,
model: str,
contents,
posthog_distinct_id: Optional[str] = None,
posthog_trace_id: Optional[str] = None,
posthog_properties: Optional[Dict[str, Any]] = None,
posthog_privacy_mode: Optional[bool] = None,
posthog_groups: Optional[Dict[str, Any]] = None,
insights_distinct_id: Optional[str] = None,
insights_trace_id: Optional[str] = None,
insights_properties: Optional[Dict[str, Any]] = None,
insights_privacy_mode: Optional[bool] = None,
insights_groups: Optional[Dict[str, Any]] = None,
**kwargs: Any,
):
# Merge PostHog parameters
# Merge Insights parameters
distinct_id, trace_id, properties, privacy_mode, groups = (
self._merge_posthog_params(
posthog_distinct_id,
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
self._merge_insights_params(
insights_distinct_id,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
)
)
@@ -2,17 +2,17 @@
Gemini-specific conversion utilities.
This module handles the conversion of Gemini API responses and inputs
into standardized formats for PostHog tracking.
into standardized formats for Insights tracking.
"""
from typing import Any, Dict, List, Optional, TypedDict, Union
from posthog.ai.types import (
from hanzo_insights.ai.types import (
FormattedContentItem,
FormattedMessage,
TokenUsage,
)
from posthog.ai.utils import serialize_raw_usage
from hanzo_insights.ai.utils import serialize_raw_usage
class GeminiPart(TypedDict, total=False):
@@ -349,7 +349,7 @@ def format_gemini_input_with_system(
if system_instruction is not None:
has_system = any(msg.get("role") == "system" for msg in formatted_messages)
if not has_system:
from posthog.ai.types import FormattedMessage
from hanzo_insights.ai.types import FormattedMessage
system_message: FormattedMessage = {
"role": "system",
@@ -362,7 +362,7 @@ def format_gemini_input_with_system(
def format_gemini_input(contents: Any) -> List[FormattedMessage]:
"""
Format Gemini input contents into standardized message format for PostHog tracking.
Format Gemini input contents into standardized message format for Insights tracking.
This function handles various input formats:
- String inputs
@@ -41,12 +41,12 @@ from langchain_core.messages import (
from langchain_core.outputs import ChatGeneration, LLMResult
from pydantic import BaseModel
from posthog import setup
from posthog.ai.sanitization import sanitize_langchain
from posthog.ai.utils import get_model_params, with_privacy_mode
from posthog.client import Client
from hanzo_insights import setup
from hanzo_insights.ai.sanitization import sanitize_langchain
from hanzo_insights.ai.utils import get_model_params, with_privacy_mode
from hanzo_insights.client import Client
log = logging.getLogger("posthog")
log = logging.getLogger("hanzo_insights")
@dataclass
@@ -79,8 +79,8 @@ class GenerationMetadata(SpanMetadata):
"""Base URL of the provider's API used in the run."""
tools: Optional[List[Dict[str, Any]]] = None
"""Tools provided to the model."""
posthog_properties: Optional[Dict[str, Any]] = None
"""PostHog properties of the run."""
insights_properties: Optional[Dict[str, Any]] = None
"""Insights properties of the run."""
RunMetadata = Union[SpanMetadata, GenerationMetadata]
@@ -89,11 +89,11 @@ RunMetadataStorage = Dict[UUID, RunMetadata]
class CallbackHandler(BaseCallbackHandler):
"""
The PostHog LLM observability callback handler for LangChain.
The Insights LLM observability callback handler for LangChain.
"""
_ph_client: Client
"""PostHog client instance."""
"""Insights client instance."""
_distinct_id: Optional[Union[str, int, UUID]]
"""Distinct ID of the user to associate the trace with."""
@@ -131,12 +131,12 @@ class CallbackHandler(BaseCallbackHandler):
):
"""
Args:
client: PostHog client instance.
client: Insights client instance.
distinct_id: Optional distinct ID of the user to associate the trace with.
trace_id: Optional trace ID to use for the event.
properties: Optional additional metadata to use for the trace.
privacy_mode: Whether to redact the input and output of the trace.
groups: Optional additional PostHog groups to use for the trace.
groups: Optional additional Insights groups to use for the trace.
"""
self._ph_client = client or setup()
self._distinct_id = distinct_id
@@ -423,7 +423,7 @@ class CallbackHandler(BaseCallbackHandler):
if provider := metadata.get("ls_provider"):
generation.provider = provider
generation.posthog_properties = metadata.get("posthog_properties")
generation.insights_properties = metadata.get("insights_properties")
try:
base_url = serialized["kwargs"]["openai_api_base"]
if base_url is not None:
@@ -578,8 +578,8 @@ class CallbackHandler(BaseCallbackHandler):
"$ai_framework": "langchain",
}
if isinstance(run.posthog_properties, dict):
event_properties.update(run.posthog_properties)
if isinstance(run.insights_properties, dict):
event_properties.update(run.insights_properties)
if run.tools:
event_properties["$ai_tools"] = run.tools
@@ -2,7 +2,7 @@ import time
import uuid
from typing import Any, Dict, List, Optional
from posthog.ai.types import TokenUsage
from hanzo_insights.ai.types import TokenUsage
try:
import openai
@@ -11,40 +11,40 @@ except ImportError:
"Please install the OpenAI SDK to use this feature: 'pip install openai'"
)
from posthog.ai.utils import (
from hanzo_insights.ai.utils import (
call_llm_and_track_usage,
extract_available_tool_calls,
merge_usage_stats,
with_privacy_mode,
)
from posthog.ai.openai.openai_converter import (
from hanzo_insights.ai.openai.openai_converter import (
extract_openai_usage_from_chunk,
extract_openai_content_from_chunk,
extract_openai_tool_calls_from_chunk,
accumulate_openai_tool_calls,
)
from posthog.ai.sanitization import sanitize_openai, sanitize_openai_response
from posthog.client import Client as PostHogClient
from posthog import setup
from hanzo_insights.ai.sanitization import sanitize_openai, sanitize_openai_response
from hanzo_insights.client import Client as InsightsClient
from hanzo_insights import setup
class OpenAI(openai.OpenAI):
"""
A wrapper around the OpenAI SDK that automatically sends LLM usage events to PostHog.
A wrapper around the OpenAI SDK that automatically sends LLM usage events to Insights.
"""
_ph_client: PostHogClient
_ph_client: InsightsClient
def __init__(self, posthog_client: Optional[PostHogClient] = None, **kwargs):
def __init__(self, insights_client: Optional[InsightsClient] = None, **kwargs):
"""
Args:
api_key: OpenAI API key.
posthog_client: If provided, events will be captured via this client instead of the global `posthog`.
insights_client: If provided, events will be captured via this client instead of the global client.
**openai_config: Any additional keyword args to set on openai (e.g. organization="xxx").
"""
super().__init__(**kwargs)
self._ph_client = posthog_client or setup()
self._ph_client = insights_client or setup()
# Store original objects after parent initialization (only if they exist)
self._original_chat = getattr(self, "chat", None)
@@ -67,7 +67,7 @@ class OpenAI(openai.OpenAI):
class WrappedResponses:
"""Wrapper for OpenAI responses that tracks usage in PostHog."""
"""Wrapper for OpenAI responses that tracks usage in Insights."""
def __init__(self, client: OpenAI, original_responses):
self._client = client
@@ -79,34 +79,34 @@ class WrappedResponses:
def create(
self,
posthog_distinct_id: Optional[str] = None,
posthog_trace_id: Optional[str] = None,
posthog_properties: Optional[Dict[str, Any]] = None,
posthog_privacy_mode: bool = False,
posthog_groups: Optional[Dict[str, Any]] = None,
insights_distinct_id: Optional[str] = None,
insights_trace_id: Optional[str] = None,
insights_properties: Optional[Dict[str, Any]] = None,
insights_privacy_mode: bool = False,
insights_groups: Optional[Dict[str, Any]] = None,
**kwargs: Any,
):
if posthog_trace_id is None:
posthog_trace_id = str(uuid.uuid4())
if insights_trace_id is None:
insights_trace_id = str(uuid.uuid4())
if kwargs.get("stream", False):
return self._create_streaming(
posthog_distinct_id,
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_distinct_id,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
**kwargs,
)
return call_llm_and_track_usage(
posthog_distinct_id,
insights_distinct_id,
self._client._ph_client,
"openai",
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
self._client.base_url,
self._original.create,
**kwargs,
@@ -114,11 +114,11 @@ class WrappedResponses:
def _create_streaming(
self,
posthog_distinct_id: Optional[str],
posthog_trace_id: Optional[str],
posthog_properties: Optional[Dict[str, Any]],
posthog_privacy_mode: bool,
posthog_groups: Optional[Dict[str, Any]],
insights_distinct_id: Optional[str],
insights_trace_id: Optional[str],
insights_properties: Optional[Dict[str, Any]],
insights_privacy_mode: bool,
insights_groups: Optional[Dict[str, Any]],
**kwargs: Any,
):
start_time = time.time()
@@ -160,11 +160,11 @@ class WrappedResponses:
latency = end_time - start_time
output = final_content
self._capture_streaming_event(
posthog_distinct_id,
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_distinct_id,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
kwargs,
usage_stats,
latency,
@@ -177,11 +177,11 @@ class WrappedResponses:
def _capture_streaming_event(
self,
posthog_distinct_id: Optional[str],
posthog_trace_id: Optional[str],
posthog_properties: Optional[Dict[str, Any]],
posthog_privacy_mode: bool,
posthog_groups: Optional[Dict[str, Any]],
insights_distinct_id: Optional[str],
insights_trace_id: Optional[str],
insights_properties: Optional[Dict[str, Any]],
insights_privacy_mode: bool,
insights_groups: Optional[Dict[str, Any]],
kwargs: Dict[str, Any],
usage_stats: TokenUsage,
latency: float,
@@ -189,12 +189,12 @@ class WrappedResponses:
available_tool_calls: Optional[List[Dict[str, Any]]] = None,
model_from_response: Optional[str] = None,
):
from posthog.ai.types import StreamingEventData
from posthog.ai.openai.openai_converter import (
from hanzo_insights.ai.types import StreamingEventData
from hanzo_insights.ai.openai.openai_converter import (
format_openai_streaming_input,
format_openai_streaming_output,
)
from posthog.ai.utils import capture_streaming_event
from hanzo_insights.ai.utils import capture_streaming_event
# Prepare standardized event data
formatted_input = format_openai_streaming_input(kwargs, "responses")
@@ -212,11 +212,11 @@ class WrappedResponses:
formatted_output=format_openai_streaming_output(output, "responses"),
usage_stats=usage_stats,
latency=latency,
distinct_id=posthog_distinct_id,
trace_id=posthog_trace_id,
properties=posthog_properties,
privacy_mode=posthog_privacy_mode,
groups=posthog_groups,
distinct_id=insights_distinct_id,
trace_id=insights_trace_id,
properties=insights_properties,
privacy_mode=insights_privacy_mode,
groups=insights_groups,
)
# Use the common capture function
@@ -224,35 +224,35 @@ class WrappedResponses:
def parse(
self,
posthog_distinct_id: Optional[str] = None,
posthog_trace_id: Optional[str] = None,
posthog_properties: Optional[Dict[str, Any]] = None,
posthog_privacy_mode: bool = False,
posthog_groups: Optional[Dict[str, Any]] = None,
insights_distinct_id: Optional[str] = None,
insights_trace_id: Optional[str] = None,
insights_properties: Optional[Dict[str, Any]] = None,
insights_privacy_mode: bool = False,
insights_groups: Optional[Dict[str, Any]] = None,
**kwargs: Any,
):
"""
Parse structured output using OpenAI's 'responses.parse' method, but also track usage in PostHog.
Parse structured output using OpenAI's 'responses.parse' method, but also track usage in Insights.
Args:
posthog_distinct_id: Optional ID to associate with the usage event.
posthog_trace_id: Optional trace UUID for linking events.
posthog_properties: Optional dictionary of extra properties to include in the event.
posthog_privacy_mode: Whether to anonymize the input and output.
posthog_groups: Optional dictionary of groups to associate with the event.
insights_distinct_id: Optional ID to associate with the usage event.
insights_trace_id: Optional trace UUID for linking events.
insights_properties: Optional dictionary of extra properties to include in the event.
insights_privacy_mode: Whether to anonymize the input and output.
insights_groups: Optional dictionary of groups to associate with the event.
**kwargs: Any additional parameters for the OpenAI Responses Parse API.
Returns:
The response from OpenAI's responses.parse call.
"""
return call_llm_and_track_usage(
posthog_distinct_id,
insights_distinct_id,
self._client._ph_client,
"openai",
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
self._client.base_url,
self._original.parse,
**kwargs,
@@ -260,7 +260,7 @@ class WrappedResponses:
class WrappedChat:
"""Wrapper for OpenAI chat that tracks usage in PostHog."""
"""Wrapper for OpenAI chat that tracks usage in Insights."""
def __init__(self, client: OpenAI, original_chat):
self._client = client
@@ -276,7 +276,7 @@ class WrappedChat:
class WrappedCompletions:
"""Wrapper for OpenAI chat completions that tracks usage in PostHog."""
"""Wrapper for OpenAI chat completions that tracks usage in Insights."""
def __init__(self, client: OpenAI, original_completions):
self._client = client
@@ -288,34 +288,34 @@ class WrappedCompletions:
def create(
self,
posthog_distinct_id: Optional[str] = None,
posthog_trace_id: Optional[str] = None,
posthog_properties: Optional[Dict[str, Any]] = None,
posthog_privacy_mode: bool = False,
posthog_groups: Optional[Dict[str, Any]] = None,
insights_distinct_id: Optional[str] = None,
insights_trace_id: Optional[str] = None,
insights_properties: Optional[Dict[str, Any]] = None,
insights_privacy_mode: bool = False,
insights_groups: Optional[Dict[str, Any]] = None,
**kwargs: Any,
):
if posthog_trace_id is None:
posthog_trace_id = str(uuid.uuid4())
if insights_trace_id is None:
insights_trace_id = str(uuid.uuid4())
if kwargs.get("stream", False):
return self._create_streaming(
posthog_distinct_id,
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_distinct_id,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
**kwargs,
)
return call_llm_and_track_usage(
posthog_distinct_id,
insights_distinct_id,
self._client._ph_client,
"openai",
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
self._client.base_url,
self._original.create,
**kwargs,
@@ -323,11 +323,11 @@ class WrappedCompletions:
def _create_streaming(
self,
posthog_distinct_id: Optional[str],
posthog_trace_id: Optional[str],
posthog_properties: Optional[Dict[str, Any]],
posthog_privacy_mode: bool,
posthog_groups: Optional[Dict[str, Any]],
insights_distinct_id: Optional[str],
insights_trace_id: Optional[str],
insights_properties: Optional[Dict[str, Any]],
insights_privacy_mode: bool,
insights_groups: Optional[Dict[str, Any]],
**kwargs: Any,
):
start_time = time.time()
@@ -385,11 +385,11 @@ class WrappedCompletions:
)
self._capture_streaming_event(
posthog_distinct_id,
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_distinct_id,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
kwargs,
usage_stats,
latency,
@@ -403,11 +403,11 @@ class WrappedCompletions:
def _capture_streaming_event(
self,
posthog_distinct_id: Optional[str],
posthog_trace_id: Optional[str],
posthog_properties: Optional[Dict[str, Any]],
posthog_privacy_mode: bool,
posthog_groups: Optional[Dict[str, Any]],
insights_distinct_id: Optional[str],
insights_trace_id: Optional[str],
insights_properties: Optional[Dict[str, Any]],
insights_privacy_mode: bool,
insights_groups: Optional[Dict[str, Any]],
kwargs: Dict[str, Any],
usage_stats: TokenUsage,
latency: float,
@@ -416,12 +416,12 @@ class WrappedCompletions:
available_tool_calls: Optional[List[Dict[str, Any]]] = None,
model_from_response: Optional[str] = None,
):
from posthog.ai.types import StreamingEventData
from posthog.ai.openai.openai_converter import (
from hanzo_insights.ai.types import StreamingEventData
from hanzo_insights.ai.openai.openai_converter import (
format_openai_streaming_input,
format_openai_streaming_output,
)
from posthog.ai.utils import capture_streaming_event
from hanzo_insights.ai.utils import capture_streaming_event
# Prepare standardized event data
formatted_input = format_openai_streaming_input(kwargs, "chat")
@@ -439,11 +439,11 @@ class WrappedCompletions:
formatted_output=format_openai_streaming_output(output, "chat", tool_calls),
usage_stats=usage_stats,
latency=latency,
distinct_id=posthog_distinct_id,
trace_id=posthog_trace_id,
properties=posthog_properties,
privacy_mode=posthog_privacy_mode,
groups=posthog_groups,
distinct_id=insights_distinct_id,
trace_id=insights_trace_id,
properties=insights_properties,
privacy_mode=insights_privacy_mode,
groups=insights_groups,
)
# Use the common capture function
@@ -451,7 +451,7 @@ class WrappedCompletions:
class WrappedEmbeddings:
"""Wrapper for OpenAI embeddings that tracks usage in PostHog."""
"""Wrapper for OpenAI embeddings that tracks usage in Insights."""
def __init__(self, client: OpenAI, original_embeddings):
self._client = client
@@ -463,30 +463,30 @@ class WrappedEmbeddings:
def create(
self,
posthog_distinct_id: Optional[str] = None,
posthog_trace_id: Optional[str] = None,
posthog_properties: Optional[Dict[str, Any]] = None,
posthog_privacy_mode: bool = False,
posthog_groups: Optional[Dict[str, Any]] = None,
insights_distinct_id: Optional[str] = None,
insights_trace_id: Optional[str] = None,
insights_properties: Optional[Dict[str, Any]] = None,
insights_privacy_mode: bool = False,
insights_groups: Optional[Dict[str, Any]] = None,
**kwargs: Any,
):
"""
Create an embedding using OpenAI's 'embeddings.create' method, but also track usage in PostHog.
Create an embedding using OpenAI's 'embeddings.create' method, but also track usage in Insights.
Args:
posthog_distinct_id: Optional ID to associate with the usage event.
posthog_trace_id: Optional trace UUID for linking events.
posthog_properties: Optional dictionary of extra properties to include in the event.
posthog_privacy_mode: Whether to anonymize the input and output.
posthog_groups: Optional dictionary of groups to associate with the event.
insights_distinct_id: Optional ID to associate with the usage event.
insights_trace_id: Optional trace UUID for linking events.
insights_properties: Optional dictionary of extra properties to include in the event.
insights_privacy_mode: Whether to anonymize the input and output.
insights_groups: Optional dictionary of groups to associate with the event.
**kwargs: Any additional parameters for the OpenAI Embeddings API.
Returns:
The response from OpenAI's embeddings.create call.
"""
if posthog_trace_id is None:
posthog_trace_id = str(uuid.uuid4())
if insights_trace_id is None:
insights_trace_id = str(uuid.uuid4())
start_time = time.time()
response = self._original.create(**kwargs)
@@ -508,34 +508,34 @@ class WrappedEmbeddings:
"$ai_model": kwargs.get("model"),
"$ai_input": with_privacy_mode(
self._client._ph_client,
posthog_privacy_mode,
insights_privacy_mode,
sanitize_openai_response(kwargs.get("input")),
),
"$ai_http_status": 200,
"$ai_input_tokens": usage_stats.get("prompt_tokens", 0),
"$ai_latency": latency,
"$ai_trace_id": posthog_trace_id,
"$ai_trace_id": insights_trace_id,
"$ai_base_url": str(self._client.base_url),
**(posthog_properties or {}),
**(insights_properties or {}),
}
if posthog_distinct_id is None:
if insights_distinct_id is None:
event_properties["$process_person_profile"] = False
# Send capture event for embeddings
if hasattr(self._client._ph_client, "capture"):
self._client._ph_client.capture(
distinct_id=posthog_distinct_id or posthog_trace_id,
distinct_id=insights_distinct_id or insights_trace_id,
event="$ai_embedding",
properties=event_properties,
groups=posthog_groups,
groups=insights_groups,
)
return response
class WrappedBeta:
"""Wrapper for OpenAI beta features that tracks usage in PostHog."""
"""Wrapper for OpenAI beta features that tracks usage in Insights."""
def __init__(self, client: OpenAI, original_beta):
self._client = client
@@ -551,7 +551,7 @@ class WrappedBeta:
class WrappedBetaChat:
"""Wrapper for OpenAI beta chat that tracks usage in PostHog."""
"""Wrapper for OpenAI beta chat that tracks usage in Insights."""
def __init__(self, client: OpenAI, original_beta_chat):
self._client = client
@@ -567,7 +567,7 @@ class WrappedBetaChat:
class WrappedBetaCompletions:
"""Wrapper for OpenAI beta chat completions that tracks usage in PostHog."""
"""Wrapper for OpenAI beta chat completions that tracks usage in Insights."""
def __init__(self, client: OpenAI, original_beta_completions):
self._client = client
@@ -579,21 +579,21 @@ class WrappedBetaCompletions:
def parse(
self,
posthog_distinct_id: Optional[str] = None,
posthog_trace_id: Optional[str] = None,
posthog_properties: Optional[Dict[str, Any]] = None,
posthog_privacy_mode: bool = False,
posthog_groups: Optional[Dict[str, Any]] = None,
insights_distinct_id: Optional[str] = None,
insights_trace_id: Optional[str] = None,
insights_properties: Optional[Dict[str, Any]] = None,
insights_privacy_mode: bool = False,
insights_groups: Optional[Dict[str, Any]] = None,
**kwargs: Any,
):
return call_llm_and_track_usage(
posthog_distinct_id,
insights_distinct_id,
self._client._ph_client,
"openai",
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
self._client.base_url,
self._original.parse,
**kwargs,
@@ -2,7 +2,7 @@ import time
import uuid
from typing import Any, Dict, List, Optional
from posthog.ai.types import TokenUsage
from hanzo_insights.ai.types import TokenUsage
try:
import openai
@@ -11,43 +11,43 @@ except ImportError:
"Please install the OpenAI SDK to use this feature: 'pip install openai'"
)
from posthog import setup
from posthog.ai.utils import (
from hanzo_insights import setup
from hanzo_insights.ai.utils import (
call_llm_and_track_usage_async,
extract_available_tool_calls,
get_model_params,
merge_usage_stats,
with_privacy_mode,
)
from posthog.ai.openai.openai_converter import (
from hanzo_insights.ai.openai.openai_converter import (
extract_openai_usage_from_chunk,
extract_openai_content_from_chunk,
extract_openai_tool_calls_from_chunk,
accumulate_openai_tool_calls,
format_openai_streaming_output,
)
from posthog.ai.sanitization import sanitize_openai, sanitize_openai_response
from posthog.client import Client as PostHogClient
from hanzo_insights.ai.sanitization import sanitize_openai, sanitize_openai_response
from hanzo_insights.client import Client as InsightsClient
class AsyncOpenAI(openai.AsyncOpenAI):
"""
An async wrapper around the OpenAI SDK that automatically sends LLM usage events to PostHog.
An async wrapper around the OpenAI SDK that automatically sends LLM usage events to Insights.
"""
_ph_client: PostHogClient
_ph_client: InsightsClient
def __init__(self, posthog_client: Optional[PostHogClient] = None, **kwargs):
def __init__(self, insights_client: Optional[InsightsClient] = None, **kwargs):
"""
Args:
api_key: OpenAI API key.
posthog_client: If provided, events will be captured via this client instead
of the global posthog.
insights_client: If provided, events will be captured via this client instead
of the global hanzo_insights.
**openai_config: Any additional keyword args to set on openai (e.g. organization="xxx").
"""
super().__init__(**kwargs)
self._ph_client = posthog_client or setup()
self._ph_client = insights_client or setup()
# Store original objects after parent initialization (only if they exist)
self._original_chat = getattr(self, "chat", None)
@@ -70,7 +70,7 @@ class AsyncOpenAI(openai.AsyncOpenAI):
class WrappedResponses:
"""Async wrapper for OpenAI responses that tracks usage in PostHog."""
"""Async wrapper for OpenAI responses that tracks usage in Insights."""
def __init__(self, client: AsyncOpenAI, original_responses):
self._client = client
@@ -83,34 +83,34 @@ class WrappedResponses:
async def create(
self,
posthog_distinct_id: Optional[str] = None,
posthog_trace_id: Optional[str] = None,
posthog_properties: Optional[Dict[str, Any]] = None,
posthog_privacy_mode: bool = False,
posthog_groups: Optional[Dict[str, Any]] = None,
insights_distinct_id: Optional[str] = None,
insights_trace_id: Optional[str] = None,
insights_properties: Optional[Dict[str, Any]] = None,
insights_privacy_mode: bool = False,
insights_groups: Optional[Dict[str, Any]] = None,
**kwargs: Any,
):
if posthog_trace_id is None:
posthog_trace_id = str(uuid.uuid4())
if insights_trace_id is None:
insights_trace_id = str(uuid.uuid4())
if kwargs.get("stream", False):
return await self._create_streaming(
posthog_distinct_id,
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_distinct_id,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
**kwargs,
)
return await call_llm_and_track_usage_async(
posthog_distinct_id,
insights_distinct_id,
self._client._ph_client,
"openai",
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
self._client.base_url,
self._original.create,
**kwargs,
@@ -118,11 +118,11 @@ class WrappedResponses:
async def _create_streaming(
self,
posthog_distinct_id: Optional[str],
posthog_trace_id: Optional[str],
posthog_properties: Optional[Dict[str, Any]],
posthog_privacy_mode: bool,
posthog_groups: Optional[Dict[str, Any]],
insights_distinct_id: Optional[str],
insights_trace_id: Optional[str],
insights_properties: Optional[Dict[str, Any]],
insights_privacy_mode: bool,
insights_groups: Optional[Dict[str, Any]],
**kwargs: Any,
):
start_time = time.time()
@@ -165,11 +165,11 @@ class WrappedResponses:
output = final_content
await self._capture_streaming_event(
posthog_distinct_id,
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_distinct_id,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
kwargs,
usage_stats,
latency,
@@ -182,11 +182,11 @@ class WrappedResponses:
async def _capture_streaming_event(
self,
posthog_distinct_id: Optional[str],
posthog_trace_id: Optional[str],
posthog_properties: Optional[Dict[str, Any]],
posthog_privacy_mode: bool,
posthog_groups: Optional[Dict[str, Any]],
insights_distinct_id: Optional[str],
insights_trace_id: Optional[str],
insights_properties: Optional[Dict[str, Any]],
insights_privacy_mode: bool,
insights_groups: Optional[Dict[str, Any]],
kwargs: Dict[str, Any],
usage_stats: TokenUsage,
latency: float,
@@ -194,8 +194,8 @@ class WrappedResponses:
available_tool_calls: Optional[List[Dict[str, Any]]] = None,
model_from_response: Optional[str] = None,
):
if posthog_trace_id is None:
posthog_trace_id = str(uuid.uuid4())
if insights_trace_id is None:
insights_trace_id = str(uuid.uuid4())
# Use model from kwargs, fallback to model from response
model = kwargs.get("model") or model_from_response or "unknown"
@@ -206,12 +206,12 @@ class WrappedResponses:
"$ai_model_parameters": get_model_params(kwargs),
"$ai_input": with_privacy_mode(
self._client._ph_client,
posthog_privacy_mode,
insights_privacy_mode,
sanitize_openai_response(kwargs.get("input")),
),
"$ai_output_choices": with_privacy_mode(
self._client._ph_client,
posthog_privacy_mode,
insights_privacy_mode,
format_openai_streaming_output(output, "responses"),
),
"$ai_http_status": 200,
@@ -222,9 +222,9 @@ class WrappedResponses:
),
"$ai_reasoning_tokens": usage_stats.get("reasoning_tokens", 0),
"$ai_latency": latency,
"$ai_trace_id": posthog_trace_id,
"$ai_trace_id": insights_trace_id,
"$ai_base_url": str(self._client.base_url),
**(posthog_properties or {}),
**(insights_properties or {}),
}
# Add web search count if present
@@ -239,48 +239,48 @@ class WrappedResponses:
if available_tool_calls:
event_properties["$ai_tools"] = available_tool_calls
if posthog_distinct_id is None:
if insights_distinct_id is None:
event_properties["$process_person_profile"] = False
if hasattr(self._client._ph_client, "capture"):
self._client._ph_client.capture(
distinct_id=posthog_distinct_id or posthog_trace_id,
distinct_id=insights_distinct_id or insights_trace_id,
event="$ai_generation",
properties=event_properties,
groups=posthog_groups,
groups=insights_groups,
)
async def parse(
self,
posthog_distinct_id: Optional[str] = None,
posthog_trace_id: Optional[str] = None,
posthog_properties: Optional[Dict[str, Any]] = None,
posthog_privacy_mode: bool = False,
posthog_groups: Optional[Dict[str, Any]] = None,
insights_distinct_id: Optional[str] = None,
insights_trace_id: Optional[str] = None,
insights_properties: Optional[Dict[str, Any]] = None,
insights_privacy_mode: bool = False,
insights_groups: Optional[Dict[str, Any]] = None,
**kwargs: Any,
):
"""
Parse structured output using OpenAI's 'responses.parse' method, but also track usage in PostHog.
Parse structured output using OpenAI's 'responses.parse' method, but also track usage in Insights.
Args:
posthog_distinct_id: Optional ID to associate with the usage event.
posthog_trace_id: Optional trace UUID for linking events.
posthog_properties: Optional dictionary of extra properties to include in the event.
posthog_privacy_mode: Whether to anonymize the input and output.
posthog_groups: Optional dictionary of groups to associate with the event.
insights_distinct_id: Optional ID to associate with the usage event.
insights_trace_id: Optional trace UUID for linking events.
insights_properties: Optional dictionary of extra properties to include in the event.
insights_privacy_mode: Whether to anonymize the input and output.
insights_groups: Optional dictionary of groups to associate with the event.
**kwargs: Any additional parameters for the OpenAI Responses Parse API.
Returns:
The response from OpenAI's responses.parse call.
"""
return await call_llm_and_track_usage_async(
posthog_distinct_id,
insights_distinct_id,
self._client._ph_client,
"openai",
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
self._client.base_url,
self._original.parse,
**kwargs,
@@ -288,7 +288,7 @@ class WrappedResponses:
class WrappedChat:
"""Async wrapper for OpenAI chat that tracks usage in PostHog."""
"""Async wrapper for OpenAI chat that tracks usage in Insights."""
def __init__(self, client: AsyncOpenAI, original_chat):
self._client = client
@@ -304,7 +304,7 @@ class WrappedChat:
class WrappedCompletions:
"""Async wrapper for OpenAI chat completions that tracks usage in PostHog."""
"""Async wrapper for OpenAI chat completions that tracks usage in Insights."""
def __init__(self, client: AsyncOpenAI, original_completions):
self._client = client
@@ -316,35 +316,35 @@ class WrappedCompletions:
async def create(
self,
posthog_distinct_id: Optional[str] = None,
posthog_trace_id: Optional[str] = None,
posthog_properties: Optional[Dict[str, Any]] = None,
posthog_privacy_mode: bool = False,
posthog_groups: Optional[Dict[str, Any]] = None,
insights_distinct_id: Optional[str] = None,
insights_trace_id: Optional[str] = None,
insights_properties: Optional[Dict[str, Any]] = None,
insights_privacy_mode: bool = False,
insights_groups: Optional[Dict[str, Any]] = None,
**kwargs: Any,
):
if posthog_trace_id is None:
posthog_trace_id = str(uuid.uuid4())
if insights_trace_id is None:
insights_trace_id = str(uuid.uuid4())
# If streaming, handle streaming specifically
if kwargs.get("stream", False):
return await self._create_streaming(
posthog_distinct_id,
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_distinct_id,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
**kwargs,
)
response = await call_llm_and_track_usage_async(
posthog_distinct_id,
insights_distinct_id,
self._client._ph_client,
"openai",
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
self._client.base_url,
self._original.create,
**kwargs,
@@ -353,11 +353,11 @@ class WrappedCompletions:
async def _create_streaming(
self,
posthog_distinct_id: Optional[str],
posthog_trace_id: Optional[str],
posthog_properties: Optional[Dict[str, Any]],
posthog_privacy_mode: bool,
posthog_groups: Optional[Dict[str, Any]],
insights_distinct_id: Optional[str],
insights_trace_id: Optional[str],
insights_properties: Optional[Dict[str, Any]],
insights_privacy_mode: bool,
insights_groups: Optional[Dict[str, Any]],
**kwargs: Any,
):
start_time = time.time()
@@ -414,11 +414,11 @@ class WrappedCompletions:
)
await self._capture_streaming_event(
posthog_distinct_id,
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_distinct_id,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
kwargs,
usage_stats,
latency,
@@ -432,11 +432,11 @@ class WrappedCompletions:
async def _capture_streaming_event(
self,
posthog_distinct_id: Optional[str],
posthog_trace_id: Optional[str],
posthog_properties: Optional[Dict[str, Any]],
posthog_privacy_mode: bool,
posthog_groups: Optional[Dict[str, Any]],
insights_distinct_id: Optional[str],
insights_trace_id: Optional[str],
insights_properties: Optional[Dict[str, Any]],
insights_privacy_mode: bool,
insights_groups: Optional[Dict[str, Any]],
kwargs: Dict[str, Any],
usage_stats: TokenUsage,
latency: float,
@@ -445,8 +445,8 @@ class WrappedCompletions:
available_tool_calls: Optional[List[Dict[str, Any]]] = None,
model_from_response: Optional[str] = None,
):
if posthog_trace_id is None:
posthog_trace_id = str(uuid.uuid4())
if insights_trace_id is None:
insights_trace_id = str(uuid.uuid4())
# Use model from kwargs, fallback to model from response
model = kwargs.get("model") or model_from_response or "unknown"
@@ -457,12 +457,12 @@ class WrappedCompletions:
"$ai_model_parameters": get_model_params(kwargs),
"$ai_input": with_privacy_mode(
self._client._ph_client,
posthog_privacy_mode,
insights_privacy_mode,
sanitize_openai(kwargs.get("messages")),
),
"$ai_output_choices": with_privacy_mode(
self._client._ph_client,
posthog_privacy_mode,
insights_privacy_mode,
format_openai_streaming_output(output, "chat", tool_calls),
),
"$ai_http_status": 200,
@@ -473,9 +473,9 @@ class WrappedCompletions:
),
"$ai_reasoning_tokens": usage_stats.get("reasoning_tokens", 0),
"$ai_latency": latency,
"$ai_trace_id": posthog_trace_id,
"$ai_trace_id": insights_trace_id,
"$ai_base_url": str(self._client.base_url),
**(posthog_properties or {}),
**(insights_properties or {}),
}
# Add web search count if present
@@ -491,20 +491,20 @@ class WrappedCompletions:
if available_tool_calls:
event_properties["$ai_tools"] = available_tool_calls
if posthog_distinct_id is None:
if insights_distinct_id is None:
event_properties["$process_person_profile"] = False
if hasattr(self._client._ph_client, "capture"):
self._client._ph_client.capture(
distinct_id=posthog_distinct_id or posthog_trace_id,
distinct_id=insights_distinct_id or insights_trace_id,
event="$ai_generation",
properties=event_properties,
groups=posthog_groups,
groups=insights_groups,
)
class WrappedEmbeddings:
"""Async wrapper for OpenAI embeddings that tracks usage in PostHog."""
"""Async wrapper for OpenAI embeddings that tracks usage in Insights."""
def __init__(self, client: AsyncOpenAI, original_embeddings):
self._client = client
@@ -517,30 +517,30 @@ class WrappedEmbeddings:
async def create(
self,
posthog_distinct_id: Optional[str] = None,
posthog_trace_id: Optional[str] = None,
posthog_properties: Optional[Dict[str, Any]] = None,
posthog_privacy_mode: bool = False,
posthog_groups: Optional[Dict[str, Any]] = None,
insights_distinct_id: Optional[str] = None,
insights_trace_id: Optional[str] = None,
insights_properties: Optional[Dict[str, Any]] = None,
insights_privacy_mode: bool = False,
insights_groups: Optional[Dict[str, Any]] = None,
**kwargs: Any,
):
"""
Create an embedding using OpenAI's 'embeddings.create' method, but also track usage in PostHog.
Create an embedding using OpenAI's 'embeddings.create' method, but also track usage in Insights.
Args:
posthog_distinct_id: Optional ID to associate with the usage event.
posthog_trace_id: Optional trace UUID for linking events.
posthog_properties: Optional dictionary of extra properties to include in the event.
posthog_privacy_mode: Whether to anonymize the input and output.
posthog_groups: Optional dictionary of groups to associate with the event.
insights_distinct_id: Optional ID to associate with the usage event.
insights_trace_id: Optional trace UUID for linking events.
insights_properties: Optional dictionary of extra properties to include in the event.
insights_privacy_mode: Whether to anonymize the input and output.
insights_groups: Optional dictionary of groups to associate with the event.
**kwargs: Any additional parameters for the OpenAI Embeddings API.
Returns:
The response from OpenAI's embeddings.create call.
"""
if posthog_trace_id is None:
posthog_trace_id = str(uuid.uuid4())
if insights_trace_id is None:
insights_trace_id = str(uuid.uuid4())
start_time = time.time()
response = await self._original.create(**kwargs)
@@ -563,34 +563,34 @@ class WrappedEmbeddings:
"$ai_model": kwargs.get("model"),
"$ai_input": with_privacy_mode(
self._client._ph_client,
posthog_privacy_mode,
insights_privacy_mode,
sanitize_openai_response(kwargs.get("input")),
),
"$ai_http_status": 200,
"$ai_input_tokens": usage_stats.get("input_tokens", 0),
"$ai_latency": latency,
"$ai_trace_id": posthog_trace_id,
"$ai_trace_id": insights_trace_id,
"$ai_base_url": str(self._client.base_url),
**(posthog_properties or {}),
**(insights_properties or {}),
}
if posthog_distinct_id is None:
if insights_distinct_id is None:
event_properties["$process_person_profile"] = False
# Send capture event for embeddings
if hasattr(self._client._ph_client, "capture"):
self._client._ph_client.capture(
distinct_id=posthog_distinct_id or posthog_trace_id,
distinct_id=insights_distinct_id or insights_trace_id,
event="$ai_embedding",
properties=event_properties,
groups=posthog_groups,
groups=insights_groups,
)
return response
class WrappedBeta:
"""Async wrapper for OpenAI beta features that tracks usage in PostHog."""
"""Async wrapper for OpenAI beta features that tracks usage in Insights."""
def __init__(self, client: AsyncOpenAI, original_beta):
self._client = client
@@ -607,7 +607,7 @@ class WrappedBeta:
class WrappedBetaChat:
"""Async wrapper for OpenAI beta chat that tracks usage in PostHog."""
"""Async wrapper for OpenAI beta chat that tracks usage in Insights."""
def __init__(self, client: AsyncOpenAI, original_beta_chat):
self._client = client
@@ -624,7 +624,7 @@ class WrappedBetaChat:
class WrappedBetaCompletions:
"""Async wrapper for OpenAI beta chat completions that tracks usage in PostHog."""
"""Async wrapper for OpenAI beta chat completions that tracks usage in Insights."""
def __init__(self, client: AsyncOpenAI, original_beta_completions):
self._client = client
@@ -637,21 +637,21 @@ class WrappedBetaCompletions:
async def parse(
self,
posthog_distinct_id: Optional[str] = None,
posthog_trace_id: Optional[str] = None,
posthog_properties: Optional[Dict[str, Any]] = None,
posthog_privacy_mode: bool = False,
posthog_groups: Optional[Dict[str, Any]] = None,
insights_distinct_id: Optional[str] = None,
insights_trace_id: Optional[str] = None,
insights_properties: Optional[Dict[str, Any]] = None,
insights_privacy_mode: bool = False,
insights_groups: Optional[Dict[str, Any]] = None,
**kwargs: Any,
):
return await call_llm_and_track_usage_async(
posthog_distinct_id,
insights_distinct_id,
self._client._ph_client,
"openai",
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
insights_trace_id,
insights_properties,
insights_privacy_mode,
insights_groups,
self._client.base_url,
self._original.parse,
**kwargs,
@@ -2,13 +2,13 @@
OpenAI-specific conversion utilities.
This module handles the conversion of OpenAI API responses and inputs
into standardized formats for PostHog tracking. It supports both
into standardized formats for Insights tracking. It supports both
Chat Completions API and Responses API formats.
"""
from typing import Any, Dict, List, Optional
from posthog.ai.types import (
from hanzo_insights.ai.types import (
FormattedContentItem,
FormattedFunctionCall,
FormattedImageContent,
@@ -16,7 +16,7 @@ from posthog.ai.types import (
FormattedTextContent,
TokenUsage,
)
from posthog.ai.utils import serialize_raw_usage
from hanzo_insights.ai.utils import serialize_raw_usage
def format_openai_response(response: Any) -> List[FormattedMessage]:
@@ -753,8 +753,8 @@ def format_openai_streaming_input(
api_type: Either "chat" or "responses"
Returns:
Formatted input ready for PostHog tracking
Formatted input ready for Insights tracking
"""
from posthog.ai.utils import merge_system_prompt
from hanzo_insights.ai.utils import merge_system_prompt
return merge_system_prompt(kwargs, "openai")
@@ -5,39 +5,39 @@ except ImportError:
"Please install the Open AI SDK to use this feature: 'pip install openai'"
)
from posthog.ai.openai.openai import (
from hanzo_insights.ai.openai.openai import (
WrappedBeta,
WrappedChat,
WrappedEmbeddings,
WrappedResponses,
)
from posthog.ai.openai.openai_async import WrappedBeta as AsyncWrappedBeta
from posthog.ai.openai.openai_async import WrappedChat as AsyncWrappedChat
from posthog.ai.openai.openai_async import WrappedEmbeddings as AsyncWrappedEmbeddings
from posthog.ai.openai.openai_async import WrappedResponses as AsyncWrappedResponses
from hanzo_insights.ai.openai.openai_async import WrappedBeta as AsyncWrappedBeta
from hanzo_insights.ai.openai.openai_async import WrappedChat as AsyncWrappedChat
from hanzo_insights.ai.openai.openai_async import WrappedEmbeddings as AsyncWrappedEmbeddings
from hanzo_insights.ai.openai.openai_async import WrappedResponses as AsyncWrappedResponses
from typing import Optional
from posthog.client import Client as PostHogClient
from posthog import setup
from hanzo_insights.client import Client as InsightsClient
from hanzo_insights import setup
class AzureOpenAI(openai.AzureOpenAI):
"""
A wrapper around the Azure OpenAI SDK that automatically sends LLM usage events to PostHog.
A wrapper around the Azure OpenAI SDK that automatically sends LLM usage events to Insights.
"""
_ph_client: PostHogClient
_ph_client: InsightsClient
def __init__(self, posthog_client: Optional[PostHogClient] = None, **kwargs):
def __init__(self, insights_client: Optional[InsightsClient] = None, **kwargs):
"""
Args:
api_key: Azure OpenAI API key.
posthog_client: If provided, events will be captured via this client instead
of the global posthog.
insights_client: If provided, events will be captured via this client instead
of the global hanzo_insights.
**openai_config: Any additional keyword args to set on Azure OpenAI (e.g. azure_endpoint="xxx").
"""
super().__init__(**kwargs)
self._ph_client = posthog_client or setup()
self._ph_client = insights_client or setup()
# Store original objects after parent initialization (only if they exist)
self._original_chat = getattr(self, "chat", None)
@@ -61,21 +61,21 @@ class AzureOpenAI(openai.AzureOpenAI):
class AsyncAzureOpenAI(openai.AsyncAzureOpenAI):
"""
An async wrapper around the Azure OpenAI SDK that automatically sends LLM usage events to PostHog.
An async wrapper around the Azure OpenAI SDK that automatically sends LLM usage events to Insights.
"""
_ph_client: PostHogClient
_ph_client: InsightsClient
def __init__(self, posthog_client: Optional[PostHogClient] = None, **kwargs):
def __init__(self, insights_client: Optional[InsightsClient] = None, **kwargs):
"""
Args:
api_key: Azure OpenAI API key.
posthog_client: If provided, events will be captured via this client instead
of the global posthog.
insights_client: If provided, events will be captured via this client instead
of the global hanzo_insights.
**openai_config: Any additional keyword args to set on Azure OpenAI (e.g. azure_endpoint="xxx").
"""
super().__init__(**kwargs)
self._ph_client = posthog_client or setup()
self._ph_client = insights_client or setup()
# Store original objects after parent initialization (only if they exist)
self._original_chat = getattr(self, "chat", None)
@@ -5,7 +5,7 @@ from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Union
if TYPE_CHECKING:
from agents.tracing import Trace
from posthog.client import Client
from hanzo_insights.client import Client
try:
import agents # noqa: F401
@@ -14,9 +14,9 @@ except ImportError:
"Please install the OpenAI Agents SDK to use this feature: 'pip install openai-agents'"
)
from posthog.ai.openai_agents.processor import PostHogTracingProcessor
from hanzo_insights.ai.openai_agents.processor import InsightsTracingProcessor
__all__ = ["PostHogTracingProcessor", "instrument"]
__all__ = ["InsightsTracingProcessor", "instrument"]
def instrument(
@@ -25,27 +25,27 @@ def instrument(
privacy_mode: bool = False,
groups: Optional[Dict[str, Any]] = None,
properties: Optional[Dict[str, Any]] = None,
) -> PostHogTracingProcessor:
) -> InsightsTracingProcessor:
"""
One-liner to instrument OpenAI Agents SDK with PostHog tracing.
One-liner to instrument OpenAI Agents SDK with Hanzo Insights tracing.
This registers a PostHogTracingProcessor with the OpenAI Agents SDK,
This registers an InsightsTracingProcessor with the OpenAI Agents SDK,
automatically capturing traces, spans, and LLM generations.
Args:
client: Optional PostHog client instance. If not provided, uses the default client.
client: Optional Insights client instance. If not provided, uses the default client.
distinct_id: Optional distinct ID to associate with all traces.
Can also be a callable that takes a trace and returns a distinct ID.
privacy_mode: If True, redacts input/output content from events.
groups: Optional PostHog groups to associate with events.
groups: Optional Insights groups to associate with events.
properties: Optional additional properties to include with all events.
Returns:
PostHogTracingProcessor: The registered processor instance.
InsightsTracingProcessor: The registered processor instance.
Example:
```python
from posthog.ai.openai_agents import instrument
from hanzo_insights.ai.openai_agents import instrument
# Simple setup
instrument(distinct_id="user@example.com")
@@ -57,7 +57,7 @@ def instrument(
properties={"environment": "production"}
)
# Now run agents as normal - traces automatically sent to PostHog
# Now run agents as normal - traces automatically sent to Insights
from agents import Agent, Runner
agent = Agent(name="Assistant", instructions="You are helpful.")
result = Runner.run_sync(agent, "Hello!")
@@ -65,7 +65,7 @@ def instrument(
"""
from agents.tracing import add_trace_processor
processor = PostHogTracingProcessor(
processor = InsightsTracingProcessor(
client=client,
distinct_id=distinct_id,
privacy_mode=privacy_mode,
@@ -20,10 +20,10 @@ from agents.tracing.span_data import (
TranscriptionSpanData,
)
from posthog import setup
from posthog.client import Client
from hanzo_insights import setup
from hanzo_insights.client import Client
log = logging.getLogger("posthog")
log = logging.getLogger("hanzo_insights")
def _ensure_serializable(obj: Any) -> Any:
@@ -53,27 +53,27 @@ def _parse_iso_timestamp(iso_str: Optional[str]) -> Optional[float]:
return None
class PostHogTracingProcessor(TracingProcessor):
class InsightsTracingProcessor(TracingProcessor):
"""
A tracing processor that sends OpenAI Agents SDK traces to PostHog.
A tracing processor that sends OpenAI Agents SDK traces to Hanzo Insights.
This processor implements the TracingProcessor interface from the OpenAI Agents SDK
and maps agent traces, spans, and generations to PostHog's LLM analytics events.
and maps agent traces, spans, and generations to Insights LLM analytics events.
Example:
```python
from agents import Agent, Runner
from agents.tracing import add_trace_processor
from posthog.ai.openai_agents import PostHogTracingProcessor
from hanzo_insights.ai.openai_agents import InsightsTracingProcessor
# Create and register the processor
processor = PostHogTracingProcessor(
processor = InsightsTracingProcessor(
distinct_id="user@example.com",
privacy_mode=False,
)
add_trace_processor(processor)
# Run agents as normal - traces automatically sent to PostHog
# Run agents as normal - traces automatically sent to Insights
agent = Agent(name="Assistant", instructions="You are helpful.")
result = Runner.run_sync(agent, "Hello!")
```
@@ -88,14 +88,14 @@ class PostHogTracingProcessor(TracingProcessor):
properties: Optional[Dict[str, Any]] = None,
):
"""
Initialize the PostHog tracing processor.
Initialize the Insights tracing processor.
Args:
client: Optional PostHog client instance. If not provided, uses the default client.
client: Optional Insights client instance. If not provided, uses the default client.
distinct_id: Either a string distinct ID or a callable that takes a Trace
and returns a distinct ID. If not provided, uses the trace_id.
privacy_mode: If True, redacts input/output content from events.
groups: Optional PostHog groups to associate with all events.
groups: Optional Insights groups to associate with all events.
properties: Optional additional properties to include with all events.
"""
self._client = client or setup()
@@ -173,7 +173,7 @@ class PostHogTracingProcessor(TracingProcessor):
properties: Dict[str, Any],
distinct_id: Optional[str] = None,
) -> None:
"""Capture an event to PostHog with error handling.
"""Capture an event to Insights with error handling.
Args:
distinct_id: The resolved distinct ID. When the user didn't provide
@@ -199,7 +199,7 @@ class PostHogTracingProcessor(TracingProcessor):
groups=self._groups,
)
except Exception as e:
log.debug(f"Failed to capture PostHog event: {e}")
log.debug(f"Failed to capture Insights event: {e}")
def on_trace_start(self, trace: Trace) -> None:
"""Called when a new trace begins. Stores metadata for spans; the $ai_trace event is emitted in on_trace_end."""
@@ -848,7 +848,7 @@ class PostHogTracingProcessor(TracingProcessor):
self._span_start_times.clear()
self._trace_metadata.clear()
# Flush the PostHog client if possible
# Flush the Insights client if possible
if hasattr(self._client, "flush") and callable(self._client.flush):
self._client.flush()
except Exception as e:
@@ -1,7 +1,7 @@
"""
Prompt management for PostHog AI SDK.
Prompt management for Hanzo Insights AI SDK.
Fetch and compile LLM prompts from PostHog with caching and fallback support.
Fetch and compile LLM prompts from Insights with caching and fallback support.
"""
import logging
@@ -10,15 +10,16 @@ import time
import urllib.parse
from typing import Any, Dict, Optional, Union
from posthog.request import USER_AGENT, _get_session
from posthog.utils import remove_trailing_slash
from hanzo_insights.request import USER_AGENT, _get_session
from hanzo_insights.utils import remove_trailing_slash
log = logging.getLogger("posthog")
log = logging.getLogger("hanzo_insights")
APP_ENDPOINT = "https://us.posthog.com"
APP_ENDPOINT = "https://us.insights.hanzo.ai"
DEFAULT_CACHE_TTL_SECONDS = 300 # 5 minutes
PromptVariables = Dict[str, Union[str, int, float, bool]]
PromptCacheKey = tuple[str, Optional[int]]
class CachedPrompt:
@@ -29,6 +30,19 @@ class CachedPrompt:
self.fetched_at = fetched_at
def _cache_key(name: str, version: Optional[int]) -> PromptCacheKey:
"""Build a cache key for latest or versioned prompt fetches."""
return (name, version)
def _prompt_reference(name: str, version: Optional[int]) -> str:
"""Format a prompt reference for logs and errors."""
label = f'prompt "{name}"'
if version is not None:
return f"{label} version {version}"
return label
def _is_prompt_api_response(data: Any) -> bool:
"""Check if the response is a valid prompt API response."""
return (
@@ -40,25 +54,32 @@ def _is_prompt_api_response(data: Any) -> bool:
class Prompts:
"""
Fetch and compile LLM prompts from PostHog.
Fetch and compile LLM prompts from Insights.
Can be initialized with a PostHog client or with direct options.
Can be initialized with a Insights client or with direct options.
Examples:
```python
from posthog import Posthog
from posthog.ai.prompts import Prompts
from hanzo_insights import Insights
from hanzo_insights.ai.prompts import Prompts
# With PostHog client
posthog = Posthog('phc_xxx', host='https://us.posthog.com', personal_api_key='phx_xxx')
prompts = Prompts(posthog)
# With Insights client
client = Insights('phc_xxx', host='https://us.insights.hanzo.ai', personal_api_key='phx_xxx')
prompts = Prompts(client)
# Or with direct options (no PostHog client needed)
prompts = Prompts(personal_api_key='phx_xxx', host='https://us.posthog.com')
# Or with direct options (no Insights client needed)
prompts = Prompts(
personal_api_key='phx_xxx',
project_api_key='phc_xxx',
host='https://us.insights.hanzo.ai',
)
# Fetch with caching and fallback
template = prompts.get('support-system-prompt', fallback='You are a helpful assistant.')
# Fetch a specific published version
prompt_v1 = prompts.get('support-system-prompt', version=1)
# Compile with variables
system_prompt = prompts.compile(template, {
'company': 'Acme Corp',
@@ -69,9 +90,10 @@ class Prompts:
def __init__(
self,
posthog: Optional[Any] = None,
client: Optional[Any] = None,
*,
personal_api_key: Optional[str] = None,
project_api_key: Optional[str] = None,
host: Optional[str] = None,
default_cache_ttl_seconds: Optional[int] = None,
):
@@ -79,23 +101,26 @@ class Prompts:
Initialize Prompts.
Args:
posthog: PostHog client instance (optional if personal_api_key provided)
personal_api_key: Direct API key (optional if posthog provided)
host: PostHog host (defaults to app endpoint)
client: Insights client instance (optional if personal_api_key provided)
personal_api_key: Direct personal API key (optional if client provided)
project_api_key: Direct project API key (optional if client provided)
host: Insights host (defaults to app endpoint)
default_cache_ttl_seconds: Default cache TTL (defaults to 300)
"""
self._default_cache_ttl_seconds = (
default_cache_ttl_seconds or DEFAULT_CACHE_TTL_SECONDS
)
self._cache: Dict[str, CachedPrompt] = {}
self._cache: Dict[PromptCacheKey, CachedPrompt] = {}
if posthog is not None:
self._personal_api_key = getattr(posthog, "personal_api_key", None) or ""
if client is not None:
self._personal_api_key = getattr(client, "personal_api_key", None) or ""
self._project_api_key = getattr(client, "api_key", None) or ""
self._host = remove_trailing_slash(
getattr(posthog, "raw_host", None) or APP_ENDPOINT
getattr(client, "raw_host", None) or APP_ENDPOINT
)
else:
self._personal_api_key = personal_api_key or ""
self._project_api_key = project_api_key or ""
self._host = remove_trailing_slash(host or APP_ENDPOINT)
def get(
@@ -104,9 +129,10 @@ class Prompts:
*,
cache_ttl_seconds: Optional[int] = None,
fallback: Optional[str] = None,
version: Optional[int] = None,
) -> str:
"""
Fetch a prompt by name from the PostHog API.
Fetch a prompt by name from the Insights API.
Caching behavior:
1. If cache is fresh, return cached value
@@ -118,6 +144,8 @@ class Prompts:
name: The name of the prompt to fetch
cache_ttl_seconds: Cache TTL in seconds (defaults to instance default)
fallback: Fallback prompt to use if fetch fails and no cache available
version: Specific prompt version to fetch. If None, fetches the latest
version
Returns:
The prompt string
@@ -130,9 +158,10 @@ class Prompts:
if cache_ttl_seconds is not None
else self._default_cache_ttl_seconds
)
cache_key = _cache_key(name, version)
# Check cache first
cached = self._cache.get(name)
cached = self._cache.get(cache_key)
now = time.time()
if cached is not None:
@@ -143,21 +172,22 @@ class Prompts:
# Try to fetch from API
try:
prompt = self._fetch_prompt_from_api(name)
prompt = self._fetch_prompt_from_api(name, version)
fetched_at = time.time()
# Update cache
self._cache[name] = CachedPrompt(prompt=prompt, fetched_at=fetched_at)
self._cache[cache_key] = CachedPrompt(prompt=prompt, fetched_at=fetched_at)
return prompt
except Exception as error:
prompt_reference = _prompt_reference(name, version)
# Fallback order:
# 1. Return stale cache (with warning)
if cached is not None:
log.warning(
'[PostHog Prompts] Failed to fetch prompt "%s", using stale cache: %s',
name,
"[Insights Prompts] Failed to fetch %s, using stale cache: %s",
prompt_reference,
error,
)
return cached.prompt
@@ -165,8 +195,8 @@ class Prompts:
# 2. Return fallback (with warning)
if fallback is not None:
log.warning(
'[PostHog Prompts] Failed to fetch prompt "%s", using fallback: %s',
name,
"[Insights Prompts] Failed to fetch %s, using fallback: %s",
prompt_reference,
error,
)
return fallback
@@ -199,27 +229,43 @@ class Prompts:
return re.sub(r"\{\{([\w.-]+)\}\}", replace_variable, prompt)
def clear_cache(self, name: Optional[str] = None) -> None:
def clear_cache(
self, name: Optional[str] = None, *, version: Optional[int] = None
) -> None:
"""
Clear cached prompts.
Args:
name: Specific prompt to clear. If None, clears all cached prompts.
name: Specific prompt name to clear. If None, clears all cached prompts.
version: Specific prompt version to clear. Requires name.
"""
if name is not None:
self._cache.pop(name, None)
else:
if version is not None and name is None:
raise ValueError("'version' requires 'name' to be provided")
if name is None:
self._cache.clear()
return
def _fetch_prompt_from_api(self, name: str) -> str:
if version is not None:
self._cache.pop(_cache_key(name, version), None)
return
keys_to_clear = [key for key in self._cache if key[0] == name]
for key in keys_to_clear:
self._cache.pop(key, None)
def _fetch_prompt_from_api(self, name: str, version: Optional[int] = None) -> str:
"""
Fetch prompt from PostHog API.
Fetch prompt from Insights API.
Endpoint: {host}/api/environments/@current/llm_prompts/name/{encoded_name}/
Endpoint:
{host}/api/environments/@current/llm_prompts/name/{encoded_name}/
?token={encoded_project_api_key}[&version={version}]
Auth: Bearer {personal_api_key}
Args:
name: The name of the prompt to fetch
version: Specific prompt version to fetch. If None, fetches the latest
Returns:
The prompt string
@@ -229,12 +275,23 @@ class Prompts:
"""
if not self._personal_api_key:
raise Exception(
"[PostHog Prompts] personal_api_key is required to fetch prompts. "
"[Insights Prompts] personal_api_key is required to fetch prompts. "
"Please provide it when initializing the Prompts instance."
)
if not self._project_api_key:
raise Exception(
"[Insights Prompts] project_api_key is required to fetch prompts. "
"Please provide it when initializing the Prompts instance."
)
encoded_name = urllib.parse.quote(name, safe="")
url = f"{self._host}/api/environments/@current/llm_prompts/name/{encoded_name}/"
query_params: Dict[str, Union[str, int]] = {"token": self._project_api_key}
if version is not None:
query_params["version"] = version
encoded_query = urllib.parse.urlencode(query_params)
url = f"{self._host}/api/environments/@current/llm_prompts/name/{encoded_name}/?{encoded_query}"
prompt_reference = _prompt_reference(name, version)
prompt_label = prompt_reference[:1].upper() + prompt_reference[1:]
headers = {
"Authorization": f"Bearer {self._personal_api_key}",
@@ -245,28 +302,28 @@ class Prompts:
if not response.ok:
if response.status_code == 404:
raise Exception(f'[PostHog Prompts] Prompt "{name}" not found')
raise Exception(f"[Insights Prompts] {prompt_label} not found")
if response.status_code == 403:
raise Exception(
f'[PostHog Prompts] Access denied for prompt "{name}". '
f"[Insights Prompts] Access denied for {prompt_reference}. "
"Check that your personal_api_key has the correct permissions and the LLM prompts feature is enabled."
)
raise Exception(
f'[PostHog Prompts] Failed to fetch prompt "{name}": HTTP {response.status_code}'
f"[Insights Prompts] Failed to fetch {prompt_label}: HTTP {response.status_code}"
)
try:
data = response.json()
except Exception:
raise Exception(
f'[PostHog Prompts] Invalid response format for prompt "{name}"'
f"[Insights Prompts] Invalid response format for {prompt_label}"
)
if not _is_prompt_api_response(data):
raise Exception(
f'[PostHog Prompts] Invalid response format for prompt "{name}"'
f"[Insights Prompts] Invalid response format for {prompt_label}"
)
return data["prompt"]
@@ -83,6 +83,12 @@ def sanitize_openai_image(item: Any) -> Any:
if not isinstance(item, dict):
return item
if item.get("type") == "input_image" and isinstance(item.get("image_url"), str):
return {
**item,
"image_url": redact_base64_data_url(item["image_url"]),
}
if (
item.get("type") == "image_url"
and isinstance(item.get("image_url"), dict)
@@ -1,5 +1,5 @@
"""
Common type definitions for PostHog AI SDK.
Common type definitions for Insights AI SDK.
These types are used for formatting messages and responses across different AI providers
(Anthropic, OpenAI, Gemini, etc.) to ensure consistency in tracking and data structure.
@@ -41,10 +41,10 @@ FormattedContentItem = Union[
class FormattedMessage(TypedDict):
"""
Standardized message format for PostHog tracking.
Standardized message format for Insights tracking.
Used across all providers to ensure consistent message structure
when sending events to PostHog.
when sending events to Insights.
"""
role: str
@@ -2,15 +2,37 @@ import time
import uuid
from typing import Any, Callable, Dict, List, Optional, cast
from posthog import get_tags, identify_context, new_context, tag
from posthog.ai.sanitization import (
from hanzo_insights import get_tags, identify_context, new_context, tag, contexts
from hanzo_insights.ai.sanitization import (
sanitize_anthropic,
sanitize_gemini,
sanitize_langchain,
sanitize_openai,
)
from posthog.ai.types import FormattedMessage, StreamingEventData, TokenUsage
from posthog.client import Client as PostHogClient
from hanzo_insights.ai.types import FormattedMessage, StreamingEventData, TokenUsage
from hanzo_insights.client import Client as InsightsClient
_TOKEN_PROPERTY_KEYS = frozenset(
{
"$ai_input_tokens",
"$ai_output_tokens",
"$ai_cache_read_input_tokens",
"$ai_cache_creation_input_tokens",
"$ai_total_tokens",
"$ai_reasoning_tokens",
}
)
def _get_tokens_source(
sdk_tags: Dict[str, Any], insights_properties: Optional[Dict[str, Any]]
) -> str:
if insights_properties and any(
key in insights_properties for key in _TOKEN_PROPERTY_KEYS
):
return "passthrough"
return "sdk"
def serialize_raw_usage(raw_usage: Any) -> Optional[Dict[str, Any]]:
@@ -18,7 +40,7 @@ def serialize_raw_usage(raw_usage: Any) -> Optional[Dict[str, Any]]:
Convert raw provider usage objects to JSON-serializable dicts.
Handles Pydantic models (OpenAI/Anthropic) and protobuf-like objects (Gemini)
with a fallback chain to ensure we never pass unserializable objects to PostHog.
with a fallback chain to ensure we never pass unserializable objects to Insights.
Args:
raw_usage: Raw usage object from provider SDK
@@ -171,19 +193,19 @@ def get_usage(response, provider: str) -> TokenUsage:
Delegates to provider-specific converter functions.
"""
if provider == "anthropic":
from posthog.ai.anthropic.anthropic_converter import (
from hanzo_insights.ai.anthropic.anthropic_converter import (
extract_anthropic_usage_from_response,
)
return extract_anthropic_usage_from_response(response)
elif provider == "openai":
from posthog.ai.openai.openai_converter import (
from hanzo_insights.ai.openai.openai_converter import (
extract_openai_usage_from_response,
)
return extract_openai_usage_from_response(response)
elif provider == "gemini":
from posthog.ai.gemini.gemini_converter import (
from hanzo_insights.ai.gemini.gemini_converter import (
extract_gemini_usage_from_response,
)
@@ -197,15 +219,15 @@ def format_response(response, provider: str):
Format a regular (non-streaming) response.
"""
if provider == "anthropic":
from posthog.ai.anthropic.anthropic_converter import format_anthropic_response
from hanzo_insights.ai.anthropic.anthropic_converter import format_anthropic_response
return format_anthropic_response(response)
elif provider == "openai":
from posthog.ai.openai.openai_converter import format_openai_response
from hanzo_insights.ai.openai.openai_converter import format_openai_response
return format_openai_response(response)
elif provider == "gemini":
from posthog.ai.gemini.gemini_converter import format_gemini_response
from hanzo_insights.ai.gemini.gemini_converter import format_gemini_response
return format_gemini_response(response)
return []
@@ -216,15 +238,15 @@ def extract_available_tool_calls(provider: str, kwargs: Dict[str, Any]):
Extract available tool calls for the given provider.
"""
if provider == "anthropic":
from posthog.ai.anthropic.anthropic_converter import extract_anthropic_tools
from hanzo_insights.ai.anthropic.anthropic_converter import extract_anthropic_tools
return extract_anthropic_tools(kwargs)
elif provider == "gemini":
from posthog.ai.gemini.gemini_converter import extract_gemini_tools
from hanzo_insights.ai.gemini.gemini_converter import extract_gemini_tools
return extract_gemini_tools(kwargs)
elif provider == "openai":
from posthog.ai.openai.openai_converter import extract_openai_tools
from hanzo_insights.ai.openai.openai_converter import extract_openai_tools
return extract_openai_tools(kwargs)
return None
@@ -237,19 +259,19 @@ def merge_system_prompt(
Merge system prompts and format messages for the given provider.
"""
if provider == "anthropic":
from posthog.ai.anthropic.anthropic_converter import format_anthropic_input
from hanzo_insights.ai.anthropic.anthropic_converter import format_anthropic_input
messages = kwargs.get("messages") or []
system = kwargs.get("system")
return format_anthropic_input(messages, system)
elif provider == "gemini":
from posthog.ai.gemini.gemini_converter import format_gemini_input_with_system
from hanzo_insights.ai.gemini.gemini_converter import format_gemini_input_with_system
contents = kwargs.get("contents", [])
config = kwargs.get("config")
return format_gemini_input_with_system(contents, config)
elif provider == "openai":
from posthog.ai.openai.openai_converter import format_openai_input
from hanzo_insights.ai.openai.openai_converter import format_openai_input
# For OpenAI, handle both Chat Completions and Responses API
messages_param = kwargs.get("messages")
@@ -297,13 +319,13 @@ def merge_system_prompt(
def call_llm_and_track_usage(
posthog_distinct_id: Optional[str],
ph_client: PostHogClient,
insights_distinct_id: Optional[str],
ph_client: InsightsClient,
provider: str,
posthog_trace_id: Optional[str],
posthog_properties: Optional[Dict[str, Any]],
posthog_privacy_mode: bool,
posthog_groups: Optional[Dict[str, Any]],
insights_trace_id: Optional[str],
insights_properties: Optional[Dict[str, Any]],
insights_privacy_mode: bool,
insights_groups: Optional[Dict[str, Any]],
base_url: str,
call_method: Callable[..., Any],
**kwargs: Any,
@@ -320,8 +342,8 @@ def call_llm_and_track_usage(
error_params: Dict[str, Any] = {}
with new_context(client=ph_client, capture_exceptions=False):
if posthog_distinct_id:
identify_context(posthog_distinct_id)
if insights_distinct_id:
identify_context(insights_distinct_id)
try:
response = call_method(**kwargs)
@@ -341,8 +363,18 @@ def call_llm_and_track_usage(
end_time = time.time()
latency = end_time - start_time
if posthog_trace_id is None:
posthog_trace_id = str(uuid.uuid4())
if insights_trace_id is None:
insights_trace_id = str(uuid.uuid4())
# Check if we have a real user distinct_id (from param or outer context)
has_person_distinct_id = (
insights_distinct_id is not None
or contexts.get_context_distinct_id() is not None
)
if not has_person_distinct_id:
# Fall back to trace_id as distinct_id when no real user id is available.
identify_context(insights_trace_id)
if response and (
hasattr(response, "usage")
@@ -358,19 +390,19 @@ def call_llm_and_track_usage(
tag("$ai_model_parameters", get_model_params(kwargs))
tag(
"$ai_input",
with_privacy_mode(ph_client, posthog_privacy_mode, sanitized_messages),
with_privacy_mode(ph_client, insights_privacy_mode, sanitized_messages),
)
tag(
"$ai_output_choices",
with_privacy_mode(
ph_client, posthog_privacy_mode, format_response(response, provider)
ph_client, insights_privacy_mode, format_response(response, provider)
),
)
tag("$ai_http_status", http_status)
tag("$ai_input_tokens", usage.get("input_tokens", 0))
tag("$ai_output_tokens", usage.get("output_tokens", 0))
tag("$ai_latency", latency)
tag("$ai_trace_id", posthog_trace_id)
tag("$ai_trace_id", insights_trace_id)
tag("$ai_base_url", str(base_url))
available_tool_calls = extract_available_tool_calls(provider, kwargs)
@@ -399,7 +431,7 @@ def call_llm_and_track_usage(
# Already serialized by converters
tag("$ai_usage", raw_usage)
if posthog_distinct_id is None:
if not has_person_distinct_id:
tag("$process_person_profile", False)
# Process instructions for Responses API
@@ -407,21 +439,26 @@ def call_llm_and_track_usage(
tag(
"$ai_instructions",
with_privacy_mode(
ph_client, posthog_privacy_mode, kwargs.get("instructions")
ph_client, insights_privacy_mode, kwargs.get("instructions")
),
)
# send the event to posthog
# send the event to Insights
if hasattr(ph_client, "capture") and callable(ph_client.capture):
sdk_tags = get_tags()
merged_properties = {
**sdk_tags,
**(insights_properties or {}),
**(error_params or {}),
}
merged_properties["$ai_tokens_source"] = _get_tokens_source(
sdk_tags, insights_properties
)
ph_client.capture(
distinct_id=posthog_distinct_id or posthog_trace_id,
distinct_id=contexts.get_context_distinct_id(),
event="$ai_generation",
properties={
**get_tags(),
**(posthog_properties or {}),
**(error_params or {}),
},
groups=posthog_groups,
properties=merged_properties,
groups=insights_groups,
)
if error:
@@ -431,13 +468,13 @@ def call_llm_and_track_usage(
async def call_llm_and_track_usage_async(
posthog_distinct_id: Optional[str],
ph_client: PostHogClient,
insights_distinct_id: Optional[str],
ph_client: InsightsClient,
provider: str,
posthog_trace_id: Optional[str],
posthog_properties: Optional[Dict[str, Any]],
posthog_privacy_mode: bool,
posthog_groups: Optional[Dict[str, Any]],
insights_trace_id: Optional[str],
insights_properties: Optional[Dict[str, Any]],
insights_privacy_mode: bool,
insights_groups: Optional[Dict[str, Any]],
base_url: str,
call_async_method: Callable[..., Any],
**kwargs: Any,
@@ -450,8 +487,8 @@ async def call_llm_and_track_usage_async(
error_params: Dict[str, Any] = {}
with new_context(client=ph_client, capture_exceptions=False):
if posthog_distinct_id:
identify_context(posthog_distinct_id)
if insights_distinct_id:
identify_context(insights_distinct_id)
try:
response = await call_async_method(**kwargs)
@@ -471,8 +508,18 @@ async def call_llm_and_track_usage_async(
end_time = time.time()
latency = end_time - start_time
if posthog_trace_id is None:
posthog_trace_id = str(uuid.uuid4())
if insights_trace_id is None:
insights_trace_id = str(uuid.uuid4())
# Check if we have a real user distinct_id (from param or outer context)
has_person_distinct_id = (
insights_distinct_id is not None
or contexts.get_context_distinct_id() is not None
)
if not has_person_distinct_id:
# Fall back to trace_id as distinct_id when no real user id is available.
identify_context(insights_trace_id)
if response and (
hasattr(response, "usage")
@@ -488,19 +535,19 @@ async def call_llm_and_track_usage_async(
tag("$ai_model_parameters", get_model_params(kwargs))
tag(
"$ai_input",
with_privacy_mode(ph_client, posthog_privacy_mode, sanitized_messages),
with_privacy_mode(ph_client, insights_privacy_mode, sanitized_messages),
)
tag(
"$ai_output_choices",
with_privacy_mode(
ph_client, posthog_privacy_mode, format_response(response, provider)
ph_client, insights_privacy_mode, format_response(response, provider)
),
)
tag("$ai_http_status", http_status)
tag("$ai_input_tokens", usage.get("input_tokens", 0))
tag("$ai_output_tokens", usage.get("output_tokens", 0))
tag("$ai_latency", latency)
tag("$ai_trace_id", posthog_trace_id)
tag("$ai_trace_id", insights_trace_id)
tag("$ai_base_url", str(base_url))
available_tool_calls = extract_available_tool_calls(provider, kwargs)
@@ -529,7 +576,7 @@ async def call_llm_and_track_usage_async(
# Already serialized by converters
tag("$ai_usage", raw_usage)
if posthog_distinct_id is None:
if not has_person_distinct_id:
tag("$process_person_profile", False)
# Process instructions for Responses API
@@ -537,21 +584,26 @@ async def call_llm_and_track_usage_async(
tag(
"$ai_instructions",
with_privacy_mode(
ph_client, posthog_privacy_mode, kwargs.get("instructions")
ph_client, insights_privacy_mode, kwargs.get("instructions")
),
)
# send the event to posthog
# send the event to Insights
if hasattr(ph_client, "capture") and callable(ph_client.capture):
sdk_tags = get_tags()
merged_properties = {
**sdk_tags,
**(insights_properties or {}),
**(error_params or {}),
}
merged_properties["$ai_tokens_source"] = _get_tokens_source(
sdk_tags, insights_properties
)
ph_client.capture(
distinct_id=posthog_distinct_id or posthog_trace_id,
distinct_id=contexts.get_context_distinct_id(),
event="$ai_generation",
properties={
**get_tags(),
**(posthog_properties or {}),
**(error_params or {}),
},
groups=posthog_groups,
properties=merged_properties,
groups=insights_groups,
)
if error:
@@ -573,14 +625,14 @@ def sanitize_messages(data: Any, provider: str) -> Any:
return data
def with_privacy_mode(ph_client: PostHogClient, privacy_mode: bool, value: Any):
def with_privacy_mode(ph_client: InsightsClient, privacy_mode: bool, value: Any):
if ph_client.privacy_mode or privacy_mode:
return None
return value
def capture_streaming_event(
ph_client: PostHogClient,
ph_client: InsightsClient,
event_data: StreamingEventData,
):
"""
@@ -590,15 +642,15 @@ def capture_streaming_event(
All provider-specific formatting should be done BEFORE calling this function.
The function handles:
- Building PostHog event properties
- Building Insights event properties
- Extracting and adding tools based on provider
- Applying privacy mode
- Adding special token fields (cache, reasoning)
- Provider-specific fields (e.g., OpenAI instructions)
- Sending the event to PostHog
- Sending the event to Insights
Args:
ph_client: PostHog client instance
ph_client: Insights client instance
event_data: Standardized streaming event data containing all necessary information
"""
trace_id = event_data.get("trace_id") or str(uuid.uuid4())
@@ -627,6 +679,15 @@ def capture_streaming_event(
**(event_data.get("properties") or {}),
}
# Determine token source: SDK-computed vs externally overridden
sdk_token_tags = {
"$ai_input_tokens": event_data["usage_stats"].get("input_tokens", 0),
"$ai_output_tokens": event_data["usage_stats"].get("output_tokens", 0),
}
event_properties["$ai_tokens_source"] = _get_tokens_source(
sdk_token_tags, event_data.get("properties")
)
# Extract and add tools based on provider
available_tools = extract_available_tool_calls(
event_data["provider"],
@@ -686,7 +747,7 @@ def capture_streaming_event(
if event_data.get("distinct_id") is None:
event_properties["$process_person_profile"] = False
# Send event to PostHog
# Send event to Insights
if hasattr(ph_client, "capture"):
ph_client.capture(
distinct_id=event_data.get("distinct_id") or trace_id,
+1 -1
View File
@@ -5,7 +5,7 @@ from datetime import datetime
import numbers
from uuid import UUID
from posthog.types import SendFeatureFlagsOptions
from hanzo_insights.types import SendFeatureFlagsOptions
ID_TYPES = Union[numbers.Number, str, UUID, int]
+117 -76
View File
@@ -11,9 +11,9 @@ from dateutil.tz import tzutc
from six import string_types
from typing_extensions import Unpack
from posthog.args import ID_TYPES, ExceptionArg, OptionalCaptureArgs, OptionalSetArgs
from posthog.consumer import Consumer
from posthog.contexts import (
from hanzo_insights.args import ID_TYPES, ExceptionArg, OptionalCaptureArgs, OptionalSetArgs
from hanzo_insights.consumer import Consumer
from hanzo_insights.contexts import (
_get_current_context,
get_capture_exception_code_variables_context,
get_code_variables_ignore_patterns_context,
@@ -23,8 +23,8 @@ from posthog.contexts import (
get_context_session_id,
new_context,
)
from posthog.exception_capture import ExceptionCapture
from posthog.exception_utils import (
from hanzo_insights.exception_capture import ExceptionCapture
from hanzo_insights.exception_utils import (
DEFAULT_CODE_VARIABLES_IGNORE_PATTERNS,
DEFAULT_CODE_VARIABLES_MASK_PATTERNS,
exc_info_from_error,
@@ -34,17 +34,18 @@ from posthog.exception_utils import (
mark_exception_as_captured,
try_attach_code_variables_to_frames,
)
from posthog.feature_flags import (
from hanzo_insights.feature_flags import (
InconclusiveMatchError,
RequiresServerEvaluation,
match_feature_flag_properties,
resolve_bucketing_value,
)
from posthog.flag_definition_cache import (
from hanzo_insights.flag_definition_cache import (
FlagDefinitionCacheData,
FlagDefinitionCacheProvider,
)
from posthog.poller import Poller
from posthog.request import (
from hanzo_insights.poller import Poller
from hanzo_insights.request import (
DEFAULT_HOST,
APIError,
QuotaLimitError,
@@ -56,7 +57,7 @@ from posthog.request import (
get,
remote_config,
)
from posthog.types import (
from hanzo_insights.types import (
FeatureFlag,
FeatureFlagError,
FeatureFlagResult,
@@ -70,7 +71,7 @@ from posthog.types import (
to_payloads,
to_values,
)
from posthog.utils import (
from hanzo_insights.utils import (
FlagCache,
RedisFlagCache,
SizeLimitedDict,
@@ -78,7 +79,7 @@ from posthog.utils import (
guess_timezone,
system_context,
)
from posthog.version import VERSION
from hanzo_insights.version import VERSION
try:
import queue
@@ -148,22 +149,22 @@ def no_throw(default_return=None):
class Client(object):
"""
This is the SDK reference for the PostHog Python SDK.
This is the SDK reference for the Hanzo Insights Python SDK.
You can learn more about example usage in the [Python SDK documentation](/docs/libraries/python).
You can also follow [Flask](/docs/libraries/flask) and [Django](/docs/libraries/django)
guides to integrate PostHog into your project.
guides to integrate Insights into your project.
Examples:
```python
from posthog import Posthog
posthog = Posthog('<ph_project_api_key>', host='<ph_client_api_host>')
posthog.debug = True
from hanzo_insights import Insights
client = Insights('<ph_project_api_key>', host='<ph_client_api_host>')
hanzo_insights.debug = True
if settings.TEST:
posthog.disabled = True
hanzo_insights.disabled = True
```
"""
log = logging.getLogger("posthog")
log = logging.getLogger("hanzo_insights")
def __init__(
self,
@@ -201,7 +202,7 @@ class Client(object):
in_app_modules: list[str] | None = None,
):
"""
Initialize a new PostHog client instance.
Initialize a new Insights client instance.
Args:
project_api_key: The project API key.
@@ -210,9 +211,9 @@ class Client(object):
Examples:
```python
from posthog import Posthog
from hanzo_insights import Insights
posthog = Posthog('<ph_project_api_key>', host='<ph_app_host>')
client = Insights('<ph_project_api_key>', host='<ph_app_host>')
```
Category:
@@ -341,9 +342,9 @@ class Client(object):
Examples:
```python
with posthog.new_context():
with hanzo_insights.new_context():
identify_context('<distinct_id>')
posthog.capture('event_name')
hanzo_insights.capture('event_name')
```
Category:
@@ -437,7 +438,7 @@ class Client(object):
Examples:
```python
payloads = posthog.get_feature_payloads('<distinct_id>')
payloads = hanzo_insights.get_feature_payloads('<distinct_id>')
```
Category:
@@ -479,7 +480,7 @@ class Client(object):
Examples:
```python
result = posthog.get_feature_flags_and_payloads('<distinct_id>')
result = hanzo_insights.get_feature_flags_and_payloads('<distinct_id>')
```
Category:
@@ -521,7 +522,7 @@ class Client(object):
Examples:
```python
decision = posthog.get_flags_decision('user123')
decision = hanzo_insights.get_flags_decision('user123')
```
Category:
@@ -569,7 +570,7 @@ class Client(object):
self, event: str, **kwargs: Unpack[OptionalCaptureArgs]
) -> Optional[str]:
"""
Captures an event manually. [Learn about capture best practices](https://posthog.com/docs/product-analytics/capture-events)
Captures an event manually. [Learn about capture best practices](https://insights.hanzo.ai/docs/product-analytics/capture-events)
Args:
event: The event name to capture.
@@ -584,20 +585,20 @@ class Client(object):
Examples:
```python
# Anonymous event
posthog.capture('some-anon-event')
hanzo_insights.capture('some-anon-event')
```
```python
# Context usage
from posthog import identify_context, new_context
from hanzo_insights import identify_context, new_context
with new_context():
identify_context('distinct_id_of_the_user')
posthog.capture('user_signed_up')
posthog.capture('user_logged_in')
posthog.capture('some-custom-action', distinct_id='distinct_id_of_the_user')
hanzo_insights.capture('user_signed_up')
hanzo_insights.capture('user_logged_in')
hanzo_insights.capture('some-custom-action', distinct_id='distinct_id_of_the_user')
```
```python
# Set event properties
posthog.capture(
hanzo_insights.capture(
"user_signed_up",
distinct_id="distinct_id_of_the_user",
properties={
@@ -608,7 +609,7 @@ class Client(object):
```
```python
# Page view event
posthog.capture('$pageview', distinct_id="distinct_id_of_the_user", properties={'$current_url': 'https://example.com'})
hanzo_insights.capture('$pageview', distinct_id="distinct_id_of_the_user", properties={'$current_url': 'https://example.com'})
```
Category:
@@ -768,7 +769,7 @@ class Client(object):
Examples:
```python
# Set with distinct id
posthog.set(distinct_id='user123', properties={'name': 'Max Hedgehog'})
hanzo_insights.set(distinct_id='user123', properties={'name': 'Max Hedgehog'})
```
Category:
@@ -815,7 +816,7 @@ class Client(object):
Examples:
```python
posthog.set_once(distinct_id='user123', properties={'initial_signup_date': '2024-01-01'})
hanzo_insights.set_once(distinct_id='user123', properties={'initial_signup_date': '2024-01-01'})
```
Category:
@@ -872,7 +873,7 @@ class Client(object):
Examples:
```python
posthog.group_identify('company', 'company_id_in_your_db', {
hanzo_insights.group_identify('company', 'company_id_in_your_db', {
'name': 'Awesome Inc.',
'employees': 11
})
@@ -927,7 +928,7 @@ class Client(object):
Examples:
```python
posthog.alias(previous_id='distinct_id', distinct_id='alias_id')
hanzo_insights.alias(previous_id='distinct_id', distinct_id='alias_id')
```
Category:
@@ -977,7 +978,7 @@ class Client(object):
# Some code that might fail
pass
except Exception as e:
posthog.capture_exception(e, 'user_distinct_id', properties=additional_properties)
hanzo_insights.capture_exception(e, 'user_distinct_id', properties=additional_properties)
```
Category:
@@ -1107,7 +1108,7 @@ class Client(object):
if not msg.get("properties"):
msg["properties"] = {}
msg["properties"]["$lib"] = "posthog-python"
msg["properties"]["$lib"] = "insights-python"
msg["properties"]["$lib_version"] = VERSION
if disable_geoip is None:
@@ -1167,8 +1168,8 @@ class Client(object):
Examples:
```python
posthog.capture('event_name')
posthog.flush() # Ensures the event is sent immediately
hanzo_insights.capture('event_name')
hanzo_insights.flush() # Ensures the event is sent immediately
```
"""
queue = self.queue
@@ -1183,7 +1184,7 @@ class Client(object):
Examples:
```python
posthog.join()
hanzo_insights.join()
```
"""
if self.consumers:
@@ -1211,7 +1212,7 @@ class Client(object):
Examples:
```python
posthog.shutdown()
hanzo_insights.shutdown()
```
"""
self.flush()
@@ -1284,7 +1285,7 @@ class Client(object):
self._fetch_feature_flags_from_api()
def _fetch_feature_flags_from_api(self):
"""Fetch feature flags from the PostHog API."""
"""Fetch feature flags from the Insights API."""
try:
# Store old flags to detect changes
old_flags_by_key: dict[str, dict] = self.feature_flags_by_key or {}
@@ -1333,18 +1334,25 @@ class Client(object):
except APIError as e:
if e.status == 401:
self.log.error(
"[FEATURE FLAGS] Error loading feature flags: To use feature flags, please set a valid personal_api_key. More information: https://posthog.com/docs/api/overview"
"[FEATURE FLAGS] Error loading feature flags: To use feature flags, please set a valid personal_api_key. More information: https://insights.hanzo.ai/docs/api/overview"
)
self.feature_flags = []
self.group_type_mapping = {}
self.cohorts = {}
if self.flag_cache:
self.flag_cache.clear()
if self.debug:
raise APIError(
status=401,
message="You are using a write-only key with feature flags. "
"To use feature flags, please set a personal_api_key "
"More information: https://posthog.com/docs/api/overview",
"More information: https://insights.hanzo.ai/docs/api/overview",
)
elif e.status == 402:
self.log.warning(
"[FEATURE FLAGS] PostHog feature flags quota limited, resetting feature flag data. Learn more about billing limits at https://posthog.com/docs/billing/limits-alerts"
"[FEATURE FLAGS] Insights feature flags quota limited, resetting feature flag data. Learn more about billing limits at https://insights.hanzo.ai/docs/billing/limits-alerts"
)
# Reset all feature flag data when quota limited
self.feature_flags = []
@@ -1358,7 +1366,7 @@ class Client(object):
if self.debug:
raise APIError(
status=402,
message="PostHog feature flags quota limited",
message="Insights feature flags quota limited",
)
else:
self.log.error(f"[FEATURE FLAGS] Error loading feature flags: {e}")
@@ -1377,7 +1385,7 @@ class Client(object):
Examples:
```python
posthog.load_feature_flags()
hanzo_insights.load_feature_flags()
```
Category:
@@ -1411,6 +1419,7 @@ class Client(object):
person_properties=None,
group_properties=None,
warn_on_unknown_groups=True,
device_id=None,
) -> FlagValue:
groups = groups or {}
person_properties = person_properties or {}
@@ -1451,22 +1460,35 @@ class Client(object):
)
return False
if group_name not in group_properties:
raise InconclusiveMatchError(
f"Flag has no group properties for group '{group_name}'"
)
focused_group_properties = group_properties[group_name]
group_key = groups[group_name]
return match_feature_flag_properties(
feature_flag,
groups[group_name],
group_key,
focused_group_properties,
self.feature_flags_by_key,
evaluation_cache,
cohort_properties=self.cohorts,
flags_by_key=self.feature_flags_by_key,
evaluation_cache=evaluation_cache,
device_id=device_id,
bucketing_value=group_key,
)
else:
bucketing_value = resolve_bucketing_value(
feature_flag, distinct_id, device_id
)
return match_feature_flag_properties(
feature_flag,
distinct_id,
person_properties,
self.cohorts,
self.feature_flags_by_key,
evaluation_cache,
cohort_properties=self.cohorts,
flags_by_key=self.feature_flags_by_key,
evaluation_cache=evaluation_cache,
device_id=device_id,
bucketing_value=bucketing_value,
)
def feature_enabled(
@@ -1498,11 +1520,11 @@ class Client(object):
Examples:
```python
is_my_flag_enabled = posthog.feature_enabled('flag-key', 'distinct_id_of_your_user')
is_my_flag_enabled = hanzo_insights.feature_enabled('flag-key', 'distinct_id_of_your_user')
if is_my_flag_enabled:
# Do something differently for this user
# Optional: fetch the payload
matched_flag_payload = posthog.get_feature_flag_payload('flag-key', 'distinct_id_of_your_user')
matched_flag_payload = hanzo_insights.get_feature_flag_payload('flag-key', 'distinct_id_of_your_user')
```
Category:
@@ -1573,8 +1595,12 @@ class Client(object):
evaluated_at = None
feature_flag_error: Optional[str] = None
# Resolve device_id from context if not provided
if device_id is None:
device_id = get_context_device_id()
flag_value = self._locally_evaluate_flag(
key, distinct_id, groups, person_properties, group_properties
key, distinct_id, groups, person_properties, group_properties, device_id
)
flag_was_locally_evaluated = flag_value is not None
@@ -1594,7 +1620,13 @@ class Client(object):
self.flag_cache.set_cached_flag(
distinct_id, key, flag_result, self.flag_definition_version
)
elif not only_evaluate_locally:
elif only_evaluate_locally:
if self.feature_flags is None:
self.log.warning(
"[FEATURE FLAGS] Local evaluation called but feature flag definitions are not loaded yet. "
"Returning None. You can call load_feature_flags() to load flags explicitly."
)
else:
try:
flag_details, request_id, evaluated_at, errors_while_computing = (
self._get_feature_flag_details_from_server(
@@ -1686,7 +1718,7 @@ class Client(object):
Examples:
```python
flag_result = posthog.get_feature_flag_result('flag-key', 'distinct_id_of_your_user')
flag_result = hanzo_insights.get_feature_flag_result('flag-key', 'distinct_id_of_your_user')
if flag_result and flag_result.get_value() == 'variant-key':
# Do something differently for this user
# Optional: fetch the payload
@@ -1748,11 +1780,11 @@ class Client(object):
Examples:
```python
enabled_variant = posthog.get_feature_flag('flag-key', 'distinct_id_of_your_user')
enabled_variant = hanzo_insights.get_feature_flag('flag-key', 'distinct_id_of_your_user')
if enabled_variant == 'variant-key': # replace 'variant-key' with the key of your variant
# Do something differently for this user
# Optional: fetch the payload
matched_flag_payload = posthog.get_feature_flag_payload('flag-key', 'distinct_id_of_your_user')
matched_flag_payload = hanzo_insights.get_feature_flag_payload('flag-key', 'distinct_id_of_your_user')
```
Category:
@@ -1778,6 +1810,7 @@ class Client(object):
groups: dict[str, str],
person_properties: dict[str, str],
group_properties: dict[str, str],
device_id: Optional[str] = None,
) -> Optional[FlagValue]:
if self.feature_flags is None and self.personal_api_key:
self.load_feature_flags()
@@ -1797,6 +1830,7 @@ class Client(object):
groups=groups,
person_properties=person_properties,
group_properties=group_properties,
device_id=device_id,
)
self.log.debug(
f"Successfully computed flag locally: {key} -> {response}"
@@ -1840,12 +1874,12 @@ class Client(object):
Examples:
```python
is_my_flag_enabled = posthog.feature_enabled('flag-key', 'distinct_id_of_your_user')
is_my_flag_enabled = hanzo_insights.feature_enabled('flag-key', 'distinct_id_of_your_user')
if is_my_flag_enabled:
# Do something differently for this user
# Optional: fetch the payload
matched_flag_payload = posthog.get_feature_flag_payload('flag-key', 'distinct_id_of_your_user')
matched_flag_payload = hanzo_insights.get_feature_flag_payload('flag-key', 'distinct_id_of_your_user')
```
Category:
@@ -1921,10 +1955,12 @@ class Client(object):
f"{key}_{'::null::' if response is None else str(response)}"
)
if (
feature_flag_reported_key
not in self.distinct_ids_feature_flags_reported[distinct_id]
):
reported_flags = self.distinct_ids_feature_flags_reported.get(distinct_id)
if reported_flags is None:
reported_flags = set()
self.distinct_ids_feature_flags_reported[distinct_id] = reported_flags
if feature_flag_reported_key not in reported_flags:
properties: dict[str, Any] = {
"$feature_flag": key,
"$feature_flag_response": response,
@@ -1960,9 +1996,7 @@ class Client(object):
groups=groups,
disable_geoip=disable_geoip,
)
self.distinct_ids_feature_flags_reported[distinct_id].add(
feature_flag_reported_key
)
reported_flags.add(feature_flag_reported_key)
def get_remote_config_payload(self, key: str):
if self.disabled:
@@ -2037,7 +2071,7 @@ class Client(object):
Examples:
```python
posthog.get_all_flags('distinct_id_of_your_user')
hanzo_insights.get_all_flags('distinct_id_of_your_user')
```
Category:
@@ -2084,7 +2118,7 @@ class Client(object):
Examples:
```python
posthog.get_all_flags_and_payloads('distinct_id_of_your_user')
hanzo_insights.get_all_flags_and_payloads('distinct_id_of_your_user')
```
Category:
@@ -2099,12 +2133,17 @@ class Client(object):
)
)
# Resolve device_id from context if not provided
if device_id is None:
device_id = get_context_device_id()
response, fallback_to_flags = self._get_all_flags_and_payloads_locally(
distinct_id,
groups=groups,
person_properties=person_properties,
group_properties=group_properties,
flag_keys_to_evaluate=flag_keys_to_evaluate,
device_id=device_id,
)
if fallback_to_flags and not only_evaluate_locally:
@@ -2135,6 +2174,7 @@ class Client(object):
group_properties=None,
warn_on_unknown_groups=False,
flag_keys_to_evaluate: Optional[list[str]] = None,
device_id: Optional[str] = None,
) -> tuple[FlagsAndPayloads, bool]:
person_properties = person_properties or {}
group_properties = group_properties or {}
@@ -2164,6 +2204,7 @@ class Client(object):
person_properties=person_properties,
group_properties=group_properties,
warn_on_unknown_groups=warn_on_unknown_groups,
device_id=device_id,
)
matched_payload = self._compute_payload_locally(
flag["key"], flags[flag["key"]]
@@ -2190,7 +2231,7 @@ class Client(object):
"""Initialize feature flag cache for graceful degradation during service outages.
When enabled, the cache stores flag evaluation results and serves them as fallback
when the PostHog API is unavailable. This ensures your application continues to
when the Insights API is unavailable. This ensures your application continues to
receive flag values even during outages.
Args:
@@ -3,9 +3,7 @@ import logging
import time
from threading import Thread
import backoff
from posthog.request import APIError, DatetimeSerializer, batch_post
from hanzo_insights.request import APIError, DatetimeSerializer, batch_post
try:
from queue import Empty
@@ -23,7 +21,7 @@ BATCH_SIZE_LIMIT = 5 * 1024 * 1024
class Consumer(Thread):
"""Consumes the messages from the client's queue."""
log = logging.getLogger("posthog")
log = logging.getLogger("hanzo_insights")
def __init__(
self,
@@ -128,29 +126,41 @@ class Consumer(Thread):
def request(self, batch):
"""Attempt to upload the batch and retry before raising an error"""
def fatal_exception(exc):
def is_retryable(exc):
if isinstance(exc, APIError):
# retry on server errors and client errors
# with 429 status code (rate limited),
# with 408 (request timeout) or 429 (rate limited),
# don't retry on other client errors
if exc.status == "N/A":
return False
return (400 <= exc.status < 500) and exc.status != 429
return not ((400 <= exc.status < 500) and exc.status not in (408, 429))
else:
# retry on all other errors (eg. network)
return False
return True
@backoff.on_exception(
backoff.expo, Exception, max_tries=self.retries + 1, giveup=fatal_exception
)
def send_request():
batch_post(
self.api_key,
self.host,
gzip=self.gzip,
timeout=self.timeout,
batch=batch,
historical_migration=self.historical_migration,
)
last_exc = None
for attempt in range(self.retries + 1):
try:
batch_post(
self.api_key,
self.host,
gzip=self.gzip,
timeout=self.timeout,
batch=batch,
historical_migration=self.historical_migration,
)
return
except Exception as e:
last_exc = e
if not is_retryable(e):
raise
if attempt < self.retries:
# Respect Retry-After header if present, otherwise use exponential backoff
retry_after = getattr(e, "retry_after", None)
if retry_after and retry_after > 0:
time.sleep(retry_after)
else:
time.sleep(min(2**attempt, 30))
send_request()
if last_exc:
raise last_exc
@@ -4,7 +4,7 @@ from typing import Optional, Any, Callable, Dict, TypeVar, cast, TYPE_CHECKING
if TYPE_CHECKING:
# To avoid circular imports
from posthog.client import Client
from hanzo_insights.client import Client
class ContextScope:
@@ -104,7 +104,7 @@ class ContextScope:
_context_stack: contextvars.ContextVar[Optional[ContextScope]] = contextvars.ContextVar(
"posthog_context_stack", default=None
"insights_context_stack", default=None
)
@@ -134,32 +134,32 @@ def new_context(
If provided, the client will be used to capture exceptions within the context.
If not provided, the default (global) client will be used. Note that the passed
client is only used to capture exceptions within the context - other events captured
within the context via `Client.capture` or `posthog.capture` will still carry the context
within the context via `Client.capture` or `hanzo_insights.capture` will still carry the context
state (tags, identity, session id), but will be captured by the client directly used (or
the global one, in the case of `posthog.capture`)
the global one, in the case of `hanzo_insights.capture`)
Examples:
```python
# Inherit parent context tags
with posthog.new_context():
posthog.tag("request_id", "123")
with hanzo_insights.new_context():
hanzo_insights.tag("request_id", "123")
# Both this event and the exception will be tagged with the context tags
posthog.capture("event_name", {"property": "value"})
hanzo_insights.capture("event_name", {"property": "value"})
raise ValueError("Something went wrong")
```
```python
# Start with fresh context (no inherited tags)
with posthog.new_context(fresh=True):
posthog.tag("request_id", "123")
with hanzo_insights.new_context(fresh=True):
hanzo_insights.tag("request_id", "123")
# Both this event and the exception will be tagged with the context tags
posthog.capture("event_name", {"property": "value"})
hanzo_insights.capture("event_name", {"property": "value"})
raise ValueError("Something went wrong")
```
Category:
Contexts
"""
from posthog import capture_exception
from hanzo_insights import capture_exception
current_context = _get_current_context()
new_context = ContextScope(current_context, fresh, capture_exceptions, client)
@@ -189,7 +189,7 @@ def tag(key: str, value: Any) -> None:
Example:
```python
posthog.tag("user_id", "123")
hanzo_insights.tag("user_id", "123")
```
Category:
@@ -221,7 +221,7 @@ def identify_context(distinct_id: str) -> None:
"""
Identify the current context with a distinct ID, associating all events captured in this or
child contexts with the given distinct ID (unless identify_context is called again). This is overridden by
distinct id's passed directly to posthog.capture and related methods (identify, set etc). Entering a
distinct id's passed directly to hanzo_insights.capture and related methods (identify, set etc). Entering a
fresh context will clear the context-level distinct ID. The distinct-id passed should be uniquely associated
with one of your users. Events captured outside of a context, or in a context with no associated distinct
ID, will be assigned a random UUID, and captured as "personless".
@@ -244,7 +244,7 @@ def set_context_session(session_id: str) -> None:
Entering a fresh context will clear the context-level session ID.
Args:
session_id: The session ID to associate with the current context and its children. See https://posthog.com/docs/data/sessions
session_id: The session ID to associate with the current context and its children. See https://insights.hanzo.ai/docs/data/sessions
Category:
Contexts
@@ -373,20 +373,20 @@ F = TypeVar("F", bound=Callable[..., Any])
def scoped(fresh: bool = False, capture_exceptions: bool = True):
"""
Decorator that creates a new context for the function. Simply wraps
the function in a with posthog.new_context(): block.
the function in a with hanzo_insights.new_context(): block.
Args:
fresh: Whether to start with a fresh context (default: False)
capture_exceptions: Whether to capture and track exceptions with posthog error tracking (default: True)
capture_exceptions: Whether to capture and track exceptions with Insights error tracking (default: True)
Example:
@posthog.scoped()
@hanzo_insights.scoped()
def process_payment(payment_id):
posthog.tag("payment_id", payment_id)
posthog.tag("payment_method", "credit_card")
hanzo_insights.tag("payment_id", payment_id)
hanzo_insights.tag("payment_method", "credit_card")
# This event will be captured with tags
posthog.capture("payment_started")
hanzo_insights.capture("payment_started")
# If this raises an exception, it will be captured with tags
# and then re-raised
some_risky_function()
@@ -9,13 +9,13 @@ import threading
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from posthog.client import Client
from hanzo_insights.client import Client
class ExceptionCapture:
# TODO: Add client side rate limiting to prevent spamming the server with exceptions
log = logging.getLogger("posthog")
log = logging.getLogger("hanzo_insights")
def __init__(self, client: "Client"):
self.client = client
@@ -30,7 +30,7 @@ from typing import ( # noqa: F401
cast,
)
from posthog.args import ExceptionArg, ExcInfo # noqa: F401
from hanzo_insights.args import ExceptionArg, ExcInfo # noqa: F401
try:
# Python 3.11
@@ -43,26 +43,31 @@ except ImportError:
DEFAULT_MAX_VALUE_LENGTH = 1024
DEFAULT_CODE_VARIABLES_MASK_PATTERNS = [
r"(?i).*password.*",
r"(?i).*secret.*",
r"(?i).*passwd.*",
r"(?i).*pwd.*",
r"(?i).*api_key.*",
r"(?i).*apikey.*",
r"(?i).*auth.*",
r"(?i).*credentials.*",
r"(?i).*privatekey.*",
r"(?i).*private_key.*",
r"(?i).*token.*",
r"(?i).*aws_access_key_id.*",
r"(?i).*_pass",
r"(?i)sk_.*",
r"(?i).*jwt.*",
r"(?i)password",
r"(?i)secret",
r"(?i)passwd",
r"(?i)pwd",
r"(?i)api_key",
r"(?i)apikey",
r"(?i)auth",
r"(?i)credentials",
r"(?i)privatekey",
r"(?i)private_key",
r"(?i)token",
r"(?i)aws_access_key_id",
r"(?i)_pass",
r"(?i)sk_",
r"(?i)jwt",
]
DEFAULT_CODE_VARIABLES_IGNORE_PATTERNS = [r"^__.*"]
CODE_VARIABLES_REDACTED_VALUE = "$$_posthog_redacted_based_on_masking_rules_$$"
CODE_VARIABLES_REDACTED_VALUE = "$$_insights_redacted_based_on_masking_rules_$$"
CODE_VARIABLES_TOO_LONG_VALUE = "$$_insights_value_too_long_$$"
_MAX_VALUE_LENGTH_FOR_PATTERN_MATCH = 5_000
_MAX_COLLECTION_ITEMS_TO_SCAN = 100
_REGEX_METACHARACTERS = frozenset(r"\.^$*+?{}[]|()")
DEFAULT_TOTAL_VARIABLES_SIZE_LIMIT = 20 * 1024
@@ -763,12 +768,12 @@ def set_in_app_in_frames(frames, in_app_exclude, in_app_include, project_root=No
def exception_is_already_captured(error):
# type: (ExceptionArg) -> bool
if isinstance(error, BaseException):
return hasattr(error, "__posthog_exception_captured")
return hasattr(error, "__insights_exception_captured")
# Autocaptured exceptions are passed as a tuple from our system hooks,
# the second item is the exception value (the first is the exception type)
elif isinstance(error, tuple) and len(error) > 1:
return error[1] is not None and hasattr(
error[1], "__posthog_exception_captured"
error[1], "__insights_exception_captured"
)
else:
return False # type: ignore[unreachable]
@@ -777,14 +782,14 @@ def exception_is_already_captured(error):
def mark_exception_as_captured(error, uuid):
# type: (ExceptionArg, str) -> None
if isinstance(error, BaseException):
setattr(error, "__posthog_exception_captured", True)
setattr(error, "__posthog_exception_uuid", uuid)
setattr(error, "__insights_exception_captured", True)
setattr(error, "__insights_exception_uuid", uuid)
# Autocaptured exceptions are passed as a tuple from our system hooks,
# the second item is the exception value (the first is the exception type)
elif isinstance(error, tuple) and len(error) > 1:
if error[1] is not None:
setattr(error[1], "__posthog_exception_captured", True)
setattr(error[1], "__posthog_exception_uuid", uuid)
setattr(error[1], "__insights_exception_captured", True)
setattr(error[1], "__insights_exception_uuid", uuid)
def exc_info_from_error(error):
@@ -928,40 +933,87 @@ def strip_string(value, max_length=None):
)
def _extract_plain_substring(pattern):
# Matches inline flag groups like (?i), (?ai), (?ims), etc. that include the 'i' flag.
# Python regex flags: a=ASCII, i=IGNORECASE, L=LOCALE, m=MULTILINE, s=DOTALL, u=UNICODE, x=VERBOSE
inline_flags = re.match(r"^\(\?[aiLmsux]*i[aiLmsux]*\)", pattern)
if not inline_flags:
return None
remainder = pattern[inline_flags.end() :]
if not remainder or any(c in _REGEX_METACHARACTERS for c in remainder):
return None
return remainder.lower()
def _compile_patterns(patterns):
compiled = []
if not patterns:
return None
substrings = []
regexes = []
for pattern in patterns:
try:
compiled.append(re.compile(pattern))
except Exception:
pass
return compiled
simple = _extract_plain_substring(pattern)
if simple is not None:
substrings.append(simple)
else:
try:
regexes.append(re.compile(pattern))
except Exception:
pass
if not substrings and not regexes:
return None
return (substrings, regexes)
def _pattern_matches(name, patterns):
for pattern in patterns:
if patterns is None:
return False
substrings, regexes = patterns
if substrings:
name_lower = name.lower()
for s in substrings:
if s in name_lower:
return True
for pattern in regexes:
if pattern.search(name):
return True
return False
def _mask_sensitive_data(value, compiled_mask):
def _mask_sensitive_data(value, compiled_mask, _seen=None):
if not compiled_mask:
return value
if isinstance(value, (dict, list, tuple)):
if _seen is None:
_seen = set()
obj_id = id(value)
if obj_id in _seen:
return "<circular ref>"
_seen.add(obj_id)
if isinstance(value, dict):
if len(value) > _MAX_COLLECTION_ITEMS_TO_SCAN:
return CODE_VARIABLES_TOO_LONG_VALUE
result = {}
for k, v in value.items():
key_str = str(k) if not isinstance(k, str) else k
if _pattern_matches(key_str, compiled_mask):
if len(key_str) > _MAX_VALUE_LENGTH_FOR_PATTERN_MATCH:
result[k] = CODE_VARIABLES_TOO_LONG_VALUE
elif _pattern_matches(key_str, compiled_mask):
result[k] = CODE_VARIABLES_REDACTED_VALUE
else:
result[k] = _mask_sensitive_data(v, compiled_mask)
result[k] = _mask_sensitive_data(v, compiled_mask, _seen)
return result
elif isinstance(value, (list, tuple)):
masked_items = [_mask_sensitive_data(item, compiled_mask) for item in value]
if len(value) > _MAX_COLLECTION_ITEMS_TO_SCAN:
return CODE_VARIABLES_TOO_LONG_VALUE
masked_items = [
_mask_sensitive_data(item, compiled_mask, _seen) for item in value
]
return type(value)(masked_items)
elif isinstance(value, str):
if len(value) > _MAX_VALUE_LENGTH_FOR_PATTERN_MATCH:
return CODE_VARIABLES_TOO_LONG_VALUE
if _pattern_matches(value, compiled_mask):
return CODE_VARIABLES_REDACTED_VALUE
return value
@@ -982,7 +1034,9 @@ def _serialize_variable_value(value, limiter, max_length=1024, compiled_mask=Non
limiter.add(result_size)
return value
elif isinstance(value, str):
if compiled_mask and _pattern_matches(value, compiled_mask):
if len(value) > _MAX_VALUE_LENGTH_FOR_PATTERN_MATCH:
result = CODE_VARIABLES_TOO_LONG_VALUE
elif compiled_mask and _pattern_matches(value, compiled_mask):
result = CODE_VARIABLES_REDACTED_VALUE
else:
result = value
@@ -2,21 +2,46 @@ import datetime
import hashlib
import logging
import re
import warnings
from typing import Optional
from dateutil import parser
from dateutil.relativedelta import relativedelta
from posthog import utils
from posthog.types import FlagValue
from posthog.utils import convert_to_datetime_aware, is_valid_regex
from hanzo_insights import utils
from hanzo_insights.types import FlagValue
from hanzo_insights.utils import convert_to_datetime_aware, is_valid_regex
__LONG_SCALE__ = float(0xFFFFFFFFFFFFFFF)
log = logging.getLogger("posthog")
log = logging.getLogger("hanzo_insights")
NONE_VALUES_ALLOWED_OPERATORS = ["is_not"]
# All operators supported by match_property, grouped by category.
EQUALITY_OPERATORS = ("exact", "is_not", "is_set", "is_not_set")
STRING_OPERATORS = ("icontains", "not_icontains", "regex", "not_regex")
NUMERIC_OPERATORS = ("gt", "gte", "lt", "lte")
DATE_OPERATORS = ("is_date_before", "is_date_after")
SEMVER_COMPARISON_OPERATORS = (
"semver_eq",
"semver_neq",
"semver_gt",
"semver_gte",
"semver_lt",
"semver_lte",
)
SEMVER_RANGE_OPERATORS = ("semver_tilde", "semver_caret", "semver_wildcard")
SEMVER_OPERATORS = SEMVER_COMPARISON_OPERATORS + SEMVER_RANGE_OPERATORS
PROPERTY_OPERATORS = (
EQUALITY_OPERATORS
+ STRING_OPERATORS
+ NUMERIC_OPERATORS
+ DATE_OPERATORS
+ SEMVER_OPERATORS
)
class InconclusiveMatchError(Exception):
pass
@@ -34,18 +59,18 @@ class RequiresServerEvaluation(Exception):
pass
# This function takes a distinct_id and a feature flag key and returns a float between 0 and 1.
# Given the same distinct_id and key, it'll always return the same float. These floats are
# This function takes a bucketing value and a feature flag key and returns a float between 0 and 1.
# Given the same bucketing value and key, it'll always return the same float. These floats are
# uniformly distributed between 0 and 1, so if we want to show this feature to 20% of traffic
# we can do _hash(key, distinct_id) < 0.2
def _hash(key: str, distinct_id: str, salt: str = "") -> float:
hash_key = f"{key}.{distinct_id}{salt}"
# we can do _hash(key, bucketing_value) < 0.2
def _hash(key: str, bucketing_value: str, salt: str = "") -> float:
hash_key = f"{key}.{bucketing_value}{salt}"
hash_val = int(hashlib.sha1(hash_key.encode("utf-8")).hexdigest()[:15], 16)
return hash_val / __LONG_SCALE__
def get_matching_variant(flag, distinct_id):
hash_value = _hash(flag["key"], distinct_id, salt="variant")
def get_matching_variant(flag, bucketing_value):
hash_value = _hash(flag["key"], bucketing_value, salt="variant")
for variant in variant_lookup_table(flag):
if hash_value >= variant["value_min"] and hash_value < variant["value_max"]:
return variant["key"]
@@ -68,7 +93,13 @@ def variant_lookup_table(feature_flag):
def evaluate_flag_dependency(
property, flags_by_key, evaluation_cache, distinct_id, properties, cohort_properties
property,
flags_by_key,
evaluation_cache,
distinct_id,
properties,
cohort_properties,
device_id=None,
):
"""
Evaluate a flag dependency property according to the dependency chain algorithm.
@@ -80,6 +111,7 @@ def evaluate_flag_dependency(
distinct_id: The distinct ID being evaluated
properties: Person properties for evaluation
cohort_properties: Cohort properties for evaluation
device_id: The device ID for bucketing (optional)
Returns:
bool: True if all dependencies in the chain evaluate to True, False otherwise
@@ -124,13 +156,27 @@ def evaluate_flag_dependency(
else:
# Recursively evaluate the dependency
try:
dep_flag_filters = dep_flag.get("filters") or {}
dep_aggregation_group_type_index = dep_flag_filters.get(
"aggregation_group_type_index"
)
if dep_aggregation_group_type_index is not None:
# Group flags should continue bucketing by the group key
# from the current evaluation context.
dep_bucketing_value = distinct_id
else:
dep_bucketing_value = resolve_bucketing_value(
dep_flag, distinct_id, device_id
)
dep_result = match_feature_flag_properties(
dep_flag,
distinct_id,
properties,
cohort_properties,
flags_by_key,
evaluation_cache,
cohort_properties=cohort_properties,
flags_by_key=flags_by_key,
evaluation_cache=evaluation_cache,
device_id=device_id,
bucketing_value=dep_bucketing_value,
)
evaluation_cache[dep_flag_key] = dep_result
except InconclusiveMatchError as e:
@@ -215,21 +261,54 @@ def matches_dependency_value(expected_value, actual_value):
return False
def resolve_bucketing_value(flag, distinct_id, device_id=None):
"""Resolve the bucketing value for a flag based on its bucketing_identifier setting.
Returns:
The appropriate identifier string to use for hashing/bucketing.
Raises:
InconclusiveMatchError: If the flag requires device_id but none was provided.
"""
flag_filters = flag.get("filters") or {}
bucketing_identifier = flag.get("bucketing_identifier") or flag_filters.get(
"bucketing_identifier"
)
if bucketing_identifier == "device_id":
if not device_id:
raise InconclusiveMatchError(
"Flag requires device_id for bucketing but none was provided"
)
return device_id
return distinct_id
def match_feature_flag_properties(
flag,
distinct_id,
properties,
*,
cohort_properties=None,
flags_by_key=None,
evaluation_cache=None,
device_id=None,
bucketing_value=None,
) -> FlagValue:
flag_conditions = (flag.get("filters") or {}).get("groups") or []
if bucketing_value is None:
warnings.warn(
"Calling match_feature_flag_properties() without bucketing_value is deprecated. "
"Pass bucketing_value explicitly. This fallback will be removed in a future major release.",
DeprecationWarning,
stacklevel=2,
)
bucketing_value = resolve_bucketing_value(flag, distinct_id, device_id)
flag_filters = flag.get("filters") or {}
flag_conditions = flag_filters.get("groups") or []
is_inconclusive = False
cohort_properties = cohort_properties or {}
# Some filters can be explicitly set to null, which require accessing variants like so
flag_variants = ((flag.get("filters") or {}).get("multivariate") or {}).get(
"variants"
) or []
flag_variants = (flag_filters.get("multivariate") or {}).get("variants") or []
valid_variant_keys = [variant["key"] for variant in flag_variants]
for condition in flag_conditions:
@@ -244,12 +323,14 @@ def match_feature_flag_properties(
cohort_properties,
flags_by_key,
evaluation_cache,
bucketing_value=bucketing_value,
device_id=device_id,
):
variant_override = condition.get("variant")
if variant_override and variant_override in valid_variant_keys:
variant = variant_override
else:
variant = get_matching_variant(flag, distinct_id)
variant = get_matching_variant(flag, bucketing_value)
return variant or True
except RequiresServerEvaluation:
# Static cohort or other missing server-side data - must fallback to API
@@ -277,6 +358,9 @@ def is_condition_match(
cohort_properties,
flags_by_key=None,
evaluation_cache=None,
*,
bucketing_value,
device_id=None,
) -> bool:
rollout_percentage = condition.get("rollout_percentage")
if len(condition.get("properties") or []) > 0:
@@ -290,6 +374,7 @@ def is_condition_match(
flags_by_key,
evaluation_cache,
distinct_id,
device_id=device_id,
)
elif property_type == "flag":
matches = evaluate_flag_dependency(
@@ -299,6 +384,7 @@ def is_condition_match(
distinct_id,
properties,
cohort_properties,
device_id=device_id,
)
else:
matches = match_property(prop, properties)
@@ -308,9 +394,9 @@ def is_condition_match(
if rollout_percentage is None:
return True
if rollout_percentage is not None and _hash(feature_flag["key"], distinct_id) > (
rollout_percentage / 100
):
if rollout_percentage is not None and _hash(
feature_flag["key"], bucketing_value
) > (rollout_percentage / 100):
return False
return True
@@ -323,6 +409,9 @@ def match_property(property, property_values) -> bool:
operator = property.get("operator") or "exact"
value = property.get("value")
if operator not in PROPERTY_OPERATORS:
raise InconclusiveMatchError(f"Unknown operator {operator}")
if key not in property_values:
raise InconclusiveMatchError(
"can't match properties without a given property value"
@@ -443,7 +532,64 @@ def match_property(property, property_values) -> bool:
"The date provided must be a string or date object"
)
# if we get here, we don't know how to handle the operator
if operator in SEMVER_OPERATORS:
try:
override_parsed = parse_semver(override_value)
except (ValueError, TypeError):
raise InconclusiveMatchError(
f"Person property value '{override_value}' is not a valid semver"
)
if operator in SEMVER_COMPARISON_OPERATORS:
try:
flag_parsed = parse_semver(value)
except (ValueError, TypeError):
raise InconclusiveMatchError(
f"Flag semver value '{value}' is not a valid semver"
)
if operator == "semver_eq":
return override_parsed == flag_parsed
elif operator == "semver_neq":
return override_parsed != flag_parsed
elif operator == "semver_gt":
return override_parsed > flag_parsed
elif operator == "semver_gte":
return override_parsed >= flag_parsed
elif operator == "semver_lt":
return override_parsed < flag_parsed
elif operator == "semver_lte":
return override_parsed <= flag_parsed
elif operator == "semver_tilde":
try:
lower, upper = _tilde_bounds(str(value))
except (ValueError, TypeError):
raise InconclusiveMatchError(
f"Flag semver value '{value}' is not valid for tilde operator"
)
return lower <= override_parsed < upper
elif operator == "semver_caret":
try:
lower, upper = _caret_bounds(str(value))
except (ValueError, TypeError):
raise InconclusiveMatchError(
f"Flag semver value '{value}' is not valid for caret operator"
)
return lower <= override_parsed < upper
elif operator == "semver_wildcard":
try:
lower, upper = _wildcard_bounds(str(value))
except (ValueError, TypeError):
raise InconclusiveMatchError(
f"Flag semver value '{value}' is not valid for wildcard operator"
)
return lower <= override_parsed < upper
# Unreachable: all operators in PROPERTY_OPERATORS are handled above,
# and unknown operators are rejected at the top of this function.
raise InconclusiveMatchError(f"Unknown operator {operator}")
@@ -454,6 +600,7 @@ def match_cohort(
flags_by_key=None,
evaluation_cache=None,
distinct_id=None,
device_id=None,
) -> bool:
# Cohort properties are in the form of property groups like this:
# {
@@ -478,6 +625,7 @@ def match_cohort(
flags_by_key,
evaluation_cache,
distinct_id,
device_id=device_id,
)
@@ -488,6 +636,7 @@ def match_property_group(
flags_by_key=None,
evaluation_cache=None,
distinct_id=None,
device_id=None,
) -> bool:
if not property_group:
return True
@@ -512,6 +661,7 @@ def match_property_group(
flags_by_key,
evaluation_cache,
distinct_id,
device_id=device_id,
)
if property_group_type == "AND":
if not matches:
@@ -545,6 +695,7 @@ def match_property_group(
flags_by_key,
evaluation_cache,
distinct_id,
device_id=device_id,
)
elif prop.get("type") == "flag":
matches = evaluate_flag_dependency(
@@ -554,6 +705,7 @@ def match_property_group(
distinct_id,
property_values,
cohort_properties,
device_id=device_id,
)
else:
matches = match_property(prop, property_values)
@@ -618,3 +770,75 @@ def relative_date_parse_for_feature_flag_matching(
return parsed_dt
else:
return None
def parse_semver(value: str) -> tuple:
"""Parse a semver string into a comparable (major, minor, patch) integer tuple.
Matches the behavior of the sortableSemver HogQL function:
- Handles v-prefix, whitespace, pre-release suffixes
- Defaults missing components to 0 (e.g., 1.2 -> 1.2.0)
Raises ValueError if parsing fails.
"""
text = str(value).strip().lstrip("vV")
# Strip pre-release/build metadata suffix
text = text.split("-")[0].split("+")[0]
parts = text.split(".")
if not parts or not parts[0]:
raise ValueError("Invalid semver format")
major = int(parts[0])
minor = int(parts[1]) if len(parts) > 1 and parts[1] else 0
patch = int(parts[2]) if len(parts) > 2 and parts[2] else 0
return (major, minor, patch)
def _tilde_bounds(value: str) -> tuple:
"""~1.2.3 means >=1.2.3 <1.3.0 (allows patch-level changes)."""
major, minor, patch = parse_semver(value)
return (major, minor, patch), (major, minor + 1, 0)
def _caret_bounds(value: str) -> tuple:
"""Caret follows semver spec:
^1.2.3 means >=1.2.3 <2.0.0
^0.2.3 means >=0.2.3 <0.3.0
^0.0.3 means >=0.0.3 <0.0.4
"""
major, minor, patch = parse_semver(value)
lower = (major, minor, patch)
if major > 0:
upper = (major + 1, 0, 0)
elif minor > 0:
upper = (0, minor + 1, 0)
else:
upper = (0, 0, patch + 1)
return lower, upper
def _wildcard_bounds(value: str) -> tuple:
"""Wildcard matching:
1.* means >=1.0.0 <2.0.0
1.2.* means >=1.2.0 <1.3.0
"""
cleaned = str(value).strip().lstrip("vV").replace("*", "").rstrip(".")
if not cleaned:
raise ValueError("Invalid wildcard pattern")
parts = [p for p in cleaned.split(".") if p]
if not parts:
raise ValueError("Invalid wildcard pattern")
if len(parts) == 1:
major = int(parts[0])
return (major, 0, 0), (major + 1, 0, 0)
elif len(parts) == 2:
major, minor = int(parts[0]), int(parts[1])
return (major, minor, 0), (major, minor + 1, 0)
else:
major, minor, patch = int(parts[0]), int(parts[1]), int(parts[2])
return (major, minor, patch), (major, minor, patch + 1)
@@ -9,11 +9,11 @@ functions) to share flag definitions and reduce API calls.
Usage:
from posthog import Posthog
from posthog.flag_definition_cache import FlagDefinitionCacheProvider
from hanzo_insights import Insights
from hanzo_insights.flag_definition_cache import FlagDefinitionCacheProvider
cache = RedisFlagDefinitionCache(redis_client, "my-team")
posthog = Posthog(
client = Insights(
"<project_api_key>",
personal_api_key="<personal_api_key>",
flag_definition_cache_provider=cache,
@@ -63,7 +63,7 @@ class FlagDefinitionCacheProvider(Protocol):
new definitions from the API. Store the data in your external cache
and release any locks.
4. `shutdown()` - Called when the PostHog client shuts down. Release any
4. `shutdown()` - Called when the Insights client shuts down. Release any
distributed locks and clean up resources.
Error Handling:
@@ -104,7 +104,7 @@ class FlagDefinitionCacheProvider(Protocol):
def on_flag_definitions_received(self, data: FlagDefinitionCacheData) -> None:
"""
Called after successfully receiving new flag definitions from PostHog.
Called after successfully receiving new flag definitions from Insights.
Use this to store the data in your external cache and release any
distributed locks acquired in `should_fetch_flag_definitions()`.
@@ -117,7 +117,7 @@ class FlagDefinitionCacheProvider(Protocol):
def shutdown(self) -> None:
"""
Called when the PostHog client shuts down.
Called when the Insights client shuts down.
Use this to release any distributed locks and clean up resources.
This method is called even if `should_fetch_flag_definitions()`
@@ -1,6 +1,6 @@
from typing import TYPE_CHECKING, cast
from posthog import contexts
from posthog.client import Client
from hanzo_insights import contexts
from hanzo_insights.client import Client
try:
from asgiref.sync import iscoroutinefunction, markcoroutinefunction
@@ -21,25 +21,26 @@ if TYPE_CHECKING:
from typing import Callable, Dict, Any, Optional, Union, Awaitable # noqa: F401
class PosthogContextMiddleware:
class InsightsContextMiddleware:
"""Middleware to automatically track Django requests.
This middleware wraps all calls with a posthog context. It attempts to extract the following from the request headers:
- Session ID, (extracted from `X-POSTHOG-SESSION-ID`)
- Distinct ID, (extracted from `X-POSTHOG-DISTINCT-ID`)
This middleware wraps all calls with an Insights context. It attempts to extract the following from the request headers:
- Session ID, (extracted from `X-INSIGHTS-SESSION-ID`)
- Distinct ID, (extracted from `X-INSIGHTS-DISTINCT-ID`)
- Request URL as $current_url
- Request Method as $request_method
The context will also auto-capture exceptions and send them to PostHog, unless you disable it by setting
`POSTHOG_MW_CAPTURE_EXCEPTIONS` to `False` in your Django settings. The exceptions are captured using the
global client, unless the setting `POSTHOG_MW_CLIENT` is set to a custom client instance
The context will also auto-capture exceptions and send them to Insights, unless you disable it by setting
`INSIGHTS_MW_CAPTURE_EXCEPTIONS` to `False` in your Django settings.
The exceptions are captured using the global client, unless the setting `INSIGHTS_MW_CLIENT`
is set to a custom client instance.
The middleware behaviour is customisable through 3 additional functions:
- `POSTHOG_MW_EXTRA_TAGS`, which is a Callable[[HttpRequest], Dict[str, Any]] expected to return a dictionary of additional tags to be added to the context.
- `POSTHOG_MW_REQUEST_FILTER`, which is a Callable[[HttpRequest], bool] expected to return `False` if the request should not be tracked.
- `POSTHOG_MW_TAG_MAP`, which is a Callable[[Dict[str, Any]], Dict[str, Any]], which you can use to modify the tags before they're added to the context.
- `INSIGHTS_MW_EXTRA_TAGS`, which is a Callable[[HttpRequest], Dict[str, Any]] expected to return a dictionary of additional tags to be added to the context.
- `INSIGHTS_MW_REQUEST_FILTER`, which is a Callable[[HttpRequest], bool] expected to return `False` if the request should not be tracked.
- `INSIGHTS_MW_TAG_MAP`, which is a Callable[[Dict[str, Any]], Dict[str, Any]], which you can use to modify the tags before they're added to the context.
You can use the `POSTHOG_MW_TAG_MAP` function to remove any default tags you don't want to capture, or override them with your own values.
You can use the `INSIGHTS_MW_TAG_MAP` function to remove any default tags you don't want to capture, or override them with your own values.
Context tags are automatically included as properties on all events captured within a context, including exceptions.
See the context documentation for more information. The extracted distinct ID and session ID, if found, are used to
@@ -66,47 +67,48 @@ class PosthogContextMiddleware:
from django.conf import settings
if hasattr(settings, "POSTHOG_MW_EXTRA_TAGS") and callable(
settings.POSTHOG_MW_EXTRA_TAGS
):
def _get_setting(name):
insights_name = f"INSIGHTS_MW_{name}"
if hasattr(settings, insights_name):
return getattr(settings, insights_name)
return None
extra_tags = _get_setting("EXTRA_TAGS")
if extra_tags and callable(extra_tags):
self.extra_tags = cast(
"Optional[Callable[[HttpRequest], Dict[str, Any]]]",
settings.POSTHOG_MW_EXTRA_TAGS,
extra_tags,
)
else:
self.extra_tags = None
if hasattr(settings, "POSTHOG_MW_REQUEST_FILTER") and callable(
settings.POSTHOG_MW_REQUEST_FILTER
):
request_filter = _get_setting("REQUEST_FILTER")
if request_filter and callable(request_filter):
self.request_filter = cast(
"Optional[Callable[[HttpRequest], bool]]",
settings.POSTHOG_MW_REQUEST_FILTER,
request_filter,
)
else:
self.request_filter = None
if hasattr(settings, "POSTHOG_MW_TAG_MAP") and callable(
settings.POSTHOG_MW_TAG_MAP
):
tag_map = _get_setting("TAG_MAP")
if tag_map and callable(tag_map):
self.tag_map = cast(
"Optional[Callable[[Dict[str, Any]], Dict[str, Any]]]",
settings.POSTHOG_MW_TAG_MAP,
tag_map,
)
else:
self.tag_map = None
if hasattr(settings, "POSTHOG_MW_CAPTURE_EXCEPTIONS") and isinstance(
settings.POSTHOG_MW_CAPTURE_EXCEPTIONS, bool
):
self.capture_exceptions = settings.POSTHOG_MW_CAPTURE_EXCEPTIONS
capture_exceptions = _get_setting("CAPTURE_EXCEPTIONS")
if isinstance(capture_exceptions, bool):
self.capture_exceptions = capture_exceptions
else:
self.capture_exceptions = True
if hasattr(settings, "POSTHOG_MW_CLIENT") and isinstance(
settings.POSTHOG_MW_CLIENT, Client
):
self.client = cast("Optional[Client]", settings.POSTHOG_MW_CLIENT)
mw_client = _get_setting("CLIENT")
if isinstance(mw_client, Client):
self.client = cast("Optional[Client]", mw_client)
else:
self.client = None
@@ -125,13 +127,13 @@ class PosthogContextMiddleware:
"""
tags = {}
# Extract session ID from X-POSTHOG-SESSION-ID header
session_id = request.headers.get("X-POSTHOG-SESSION-ID")
# Extract session ID from X-INSIGHTS-SESSION-ID header
session_id = request.headers.get("X-INSIGHTS-SESSION-ID")
if session_id:
contexts.set_context_session(session_id)
# Extract distinct ID from X-POSTHOG-DISTINCT-ID header or request user id
distinct_id = request.headers.get("X-POSTHOG-DISTINCT-ID") or user_id
# Extract distinct ID from X-INSIGHTS-DISTINCT-ID header or request user id
distinct_id = request.headers.get("X-INSIGHTS-DISTINCT-ID") or user_id
if distinct_id:
contexts.identify_context(distinct_id)
@@ -155,7 +157,7 @@ class PosthogContextMiddleware:
# Extract IP address
ip_address = request.headers.get("X-Forwarded-For")
if ip_address:
tags["$ip_address"] = ip_address
tags["$ip"] = ip_address
# Extract user agent
user_agent = request.headers.get("User-Agent")
@@ -314,6 +316,6 @@ class PosthogContextMiddleware:
if self.client:
self.client.capture_exception(exception)
else:
from posthog import capture_exception
from hanzo_insights import capture_exception
capture_exception(exception)
@@ -3,7 +3,7 @@ import logging
import re
import socket
from dataclasses import dataclass
from datetime import date, datetime
from datetime import date, datetime, timezone
from gzip import GzipFile
from io import BytesIO
from typing import Any, List, Optional, Tuple, Union
@@ -14,8 +14,8 @@ from requests.adapters import HTTPAdapter # type: ignore[import-untyped]
from urllib3.connection import HTTPConnection
from urllib3.util.retry import Retry
from posthog.utils import remove_trailing_slash
from posthog.version import VERSION
from hanzo_insights.utils import remove_trailing_slash
from hanzo_insights.version import VERSION
SocketOptions = List[Tuple[int, int, Union[int, bytes]]]
@@ -137,7 +137,7 @@ def set_socket_options(socket_options: Optional[SocketOptions]) -> None:
Configure socket options for all HTTP connections.
Example:
from posthog import set_socket_options
from hanzo_insights import set_socket_options
set_socket_options([(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)])
"""
global _session, _flags_session, _socket_options
@@ -159,19 +159,19 @@ def disable_connection_reuse() -> None:
_pooling_enabled = False
US_INGESTION_ENDPOINT = "https://us.i.posthog.com"
EU_INGESTION_ENDPOINT = "https://eu.i.posthog.com"
US_INGESTION_ENDPOINT = "https://us.i.insights.hanzo.ai"
EU_INGESTION_ENDPOINT = "https://eu.i.insights.hanzo.ai"
DEFAULT_HOST = US_INGESTION_ENDPOINT
USER_AGENT = "posthog-python/" + VERSION
USER_AGENT = "hanzo-insights-python/" + VERSION
def determine_server_host(host: Optional[str]) -> str:
"""Determines the server host to use."""
host_or_default = host or DEFAULT_HOST
trimmed_host = remove_trailing_slash(host_or_default)
if trimmed_host in ("https://app.posthog.com", "https://us.posthog.com"):
if trimmed_host in ("https://app.posthog.com", "https://us.posthog.com", "https://insights.hanzo.ai", "https://us.insights.hanzo.ai"):
return US_INGESTION_ENDPOINT
elif trimmed_host == "https://eu.posthog.com":
elif trimmed_host in ("https://eu.posthog.com", "https://eu.insights.hanzo.ai"):
return EU_INGESTION_ENDPOINT
else:
return host_or_default
@@ -187,7 +187,7 @@ def post(
**kwargs,
) -> requests.Response:
"""Post the `kwargs` to the API"""
log = logging.getLogger("posthog")
log = logging.getLogger("hanzo_insights")
body = kwargs
body["sentAt"] = datetime.now(tz=tzutc()).isoformat()
url = remove_trailing_slash(host or DEFAULT_HOST) + path
@@ -217,7 +217,7 @@ def post(
def _process_response(
res: requests.Response, success_message: str, *, return_json: bool = True
) -> Union[requests.Response, Any]:
log = logging.getLogger("posthog")
log = logging.getLogger("hanzo_insights")
if res.status_code == 200:
log.debug(success_message)
response = res.json() if return_json else res
@@ -231,16 +231,35 @@ def _process_response(
and "feature_flags" in response["quotaLimited"]
):
log.warning(
"[FEATURE FLAGS] PostHog feature flags quota limited, resetting feature flag data. Learn more about billing limits at https://posthog.com/docs/billing/limits-alerts"
"[FEATURE FLAGS] Feature flags quota limited, resetting feature flag data. Learn more about billing limits at https://insights.hanzo.ai/docs/billing/limits-alerts"
)
raise QuotaLimitError(res.status_code, "Feature flags quota limited")
return response
retry_after = None
retry_after_header = res.headers.get("Retry-After")
if retry_after_header:
try:
retry_after = float(retry_after_header)
except (ValueError, TypeError):
try:
from email.utils import parsedate_to_datetime
retry_after = max(
0.0,
(
parsedate_to_datetime(retry_after_header)
- datetime.now(timezone.utc)
).total_seconds(),
)
except (ValueError, TypeError):
pass
try:
payload = res.json()
log.debug("received response: %s", payload)
raise APIError(res.status_code, payload["detail"])
raise APIError(res.status_code, payload["detail"], retry_after=retry_after)
except (KeyError, ValueError):
raise APIError(res.status_code, res.text)
raise APIError(res.status_code, res.text, retry_after=retry_after)
def decide(
@@ -322,7 +341,7 @@ def get(
- not_modified=True and data=None if server returns 304
- not_modified=False and data=response if server returns 200
"""
log = logging.getLogger("posthog")
log = logging.getLogger("hanzo_insights")
full_url = remove_trailing_slash(host or DEFAULT_HOST) + url
headers = {"Authorization": "Bearer %s" % api_key, "User-Agent": USER_AGENT}
@@ -348,12 +367,15 @@ def get(
class APIError(Exception):
def __init__(self, status: Union[int, str], message: str):
def __init__(
self, status: Union[int, str], message: str, retry_after: Optional[float] = None
):
self.message = message
self.status = status
self.retry_after = retry_after
def __str__(self):
msg = "[PostHog] {0} ({1})"
msg = "[Insights] {0} ({1})"
return msg.format(self.message, self.status)
@@ -6,7 +6,7 @@ import unittest
def all_names():
for _, modname, _ in pkgutil.iter_modules(__path__):
yield "posthog.test." + modname
yield "hanzo_insights.test." + modname
def all():
@@ -3,10 +3,12 @@ from unittest.mock import patch
import pytest
from hanzo_insights import identify_context, new_context
try:
from anthropic.types import Message, Usage
from posthog.ai.anthropic import Anthropic, AsyncAnthropic
from hanzo_insights.ai.anthropic import Anthropic, AsyncAnthropic
ANTHROPIC_AVAILABLE = True
except ImportError:
@@ -103,7 +105,7 @@ class MockDelta:
@pytest.fixture
def mock_client():
with patch("posthog.client.Client") as mock_client:
with patch("hanzo_insights.client.Client") as mock_client:
mock_client.privacy_mode = False
yield mock_client
@@ -277,12 +279,12 @@ def test_basic_completion(mock_client, mock_anthropic_response):
with patch(
"anthropic.resources.Messages.create", return_value=mock_anthropic_response
):
client = Anthropic(api_key="test-key", posthog_client=mock_client)
client = Anthropic(api_key="test-key", insights_client=mock_client)
response = client.messages.create(
model="claude-3-opus-20240229",
messages=[{"role": "user", "content": "Hello"}],
posthog_distinct_id="test-id",
posthog_properties={"foo": "bar"},
insights_distinct_id="test-id",
insights_properties={"foo": "bar"},
)
assert response == mock_anthropic_response
@@ -306,6 +308,7 @@ def test_basic_completion(mock_client, mock_anthropic_response):
assert props["$ai_output_tokens"] == 10
assert props["$ai_http_status"] == 200
assert props["foo"] == "bar"
assert props["$ai_tokens_source"] == "sdk"
assert isinstance(props["$ai_latency"], float)
# Verify raw usage metadata is passed for backend processing
assert "$ai_usage" in props
@@ -318,16 +321,33 @@ def test_basic_completion(mock_client, mock_anthropic_response):
assert "output_tokens" in props["$ai_usage"]
def test_tokens_source_passthrough(mock_client, mock_anthropic_response):
with patch(
"anthropic.resources.Messages.create", return_value=mock_anthropic_response
):
client = Anthropic(api_key="test-key", insights_client=mock_client)
client.messages.create(
model="claude-3-opus-20240229",
messages=[{"role": "user", "content": "Hello"}],
insights_distinct_id="test-id",
insights_properties={"$ai_input_tokens": 99999},
)
props = mock_client.capture.call_args[1]["properties"]
assert props["$ai_tokens_source"] == "passthrough"
assert props["$ai_input_tokens"] == 99999
def test_groups(mock_client, mock_anthropic_response):
with patch(
"anthropic.resources.Messages.create", return_value=mock_anthropic_response
):
client = Anthropic(api_key="test-key", posthog_client=mock_client)
client = Anthropic(api_key="test-key", insights_client=mock_client)
response = client.messages.create(
model="claude-3-opus-20240229",
messages=[{"role": "user", "content": "Hello"}],
posthog_distinct_id="test-id",
posthog_groups={"company": "test_company"},
insights_distinct_id="test-id",
insights_groups={"company": "test_company"},
)
assert response == mock_anthropic_response
@@ -341,12 +361,12 @@ def test_privacy_mode_local(mock_client, mock_anthropic_response):
with patch(
"anthropic.resources.Messages.create", return_value=mock_anthropic_response
):
client = Anthropic(api_key="test-key", posthog_client=mock_client)
client = Anthropic(api_key="test-key", insights_client=mock_client)
response = client.messages.create(
model="claude-3-opus-20240229",
messages=[{"role": "user", "content": "Hello"}],
posthog_distinct_id="test-id",
posthog_privacy_mode=True,
insights_distinct_id="test-id",
insights_privacy_mode=True,
)
assert response == mock_anthropic_response
@@ -363,12 +383,12 @@ def test_privacy_mode_global(mock_client, mock_anthropic_response):
"anthropic.resources.Messages.create", return_value=mock_anthropic_response
):
mock_client.privacy_mode = True
client = Anthropic(api_key="test-key", posthog_client=mock_client)
client = Anthropic(api_key="test-key", insights_client=mock_client)
response = client.messages.create(
model="claude-3-opus-20240229",
messages=[{"role": "user", "content": "Hello"}],
posthog_distinct_id="test-id",
posthog_privacy_mode=False,
insights_distinct_id="test-id",
insights_privacy_mode=False,
)
assert response == mock_anthropic_response
@@ -387,14 +407,14 @@ def test_basic_integration(mock_client):
"anthropic.resources.Messages.create",
return_value=create_mock_response(),
):
client = Anthropic(posthog_client=mock_client)
client = Anthropic(insights_client=mock_client)
client.messages.create(
model="claude-3-opus-20240229",
messages=[{"role": "user", "content": "Foo"}],
max_tokens=1,
temperature=0,
posthog_distinct_id="test-id",
posthog_properties={"foo": "bar"},
insights_distinct_id="test-id",
insights_properties={"foo": "bar"},
system="You must always answer with 'Bar'.",
)
@@ -432,7 +452,7 @@ async def test_basic_async_integration(mock_client):
"anthropic.resources.messages.AsyncMessages.create",
side_effect=mock_async_create,
):
client = AsyncAnthropic(posthog_client=mock_client)
client = AsyncAnthropic(insights_client=mock_client)
await client.messages.create(
model="claude-3-opus-20240229",
messages=[
@@ -440,8 +460,8 @@ async def test_basic_async_integration(mock_client):
],
max_tokens=1,
temperature=0,
posthog_distinct_id="test-id",
posthog_properties={"foo": "bar"},
insights_distinct_id="test-id",
insights_properties={"foo": "bar"},
)
assert mock_client.capture.call_count == 1
@@ -493,7 +513,7 @@ async def test_async_streaming_system_prompt(mock_client):
"anthropic.resources.messages.AsyncMessages.create",
side_effect=async_create_wrapper,
):
client = AsyncAnthropic(posthog_client=mock_client)
client = AsyncAnthropic(insights_client=mock_client)
response = await client.messages.create(
model="claude-3-opus-20240229",
system="You must always answer with 'Bar'.",
@@ -521,7 +541,7 @@ def test_error(mock_client, mock_anthropic_response):
with patch(
"anthropic.resources.Messages.create", side_effect=Exception("Test error")
):
client = Anthropic(api_key="test-key", posthog_client=mock_client)
client = Anthropic(api_key="test-key", insights_client=mock_client)
with pytest.raises(Exception):
client.messages.create(
model="claude-3-opus-20240229",
@@ -541,12 +561,12 @@ def test_cached_tokens(mock_client, mock_anthropic_response_with_cached_tokens):
"anthropic.resources.Messages.create",
return_value=mock_anthropic_response_with_cached_tokens,
):
client = Anthropic(api_key="test-key", posthog_client=mock_client)
client = Anthropic(api_key="test-key", insights_client=mock_client)
response = client.messages.create(
model="claude-3-opus-20240229",
messages=[{"role": "user", "content": "Hello"}],
posthog_distinct_id="test-id",
posthog_properties={"foo": "bar"},
insights_distinct_id="test-id",
insights_properties={"foo": "bar"},
)
assert response == mock_anthropic_response_with_cached_tokens
@@ -580,7 +600,7 @@ def test_tool_definition(mock_client, mock_anthropic_response):
"anthropic.resources.Messages.create",
return_value=mock_anthropic_response,
):
client = Anthropic(api_key="test-key", posthog_client=mock_client)
client = Anthropic(api_key="test-key", insights_client=mock_client)
tools = [
{
@@ -605,8 +625,8 @@ def test_tool_definition(mock_client, mock_anthropic_response):
temperature=0.7,
tools=tools,
messages=[{"role": "user", "content": "hey"}],
posthog_distinct_id="test-id",
posthog_properties={"foo": "bar"},
insights_distinct_id="test-id",
insights_properties={"foo": "bar"},
)
assert response == mock_anthropic_response
@@ -642,7 +662,7 @@ def test_tool_calls_in_output_choices(
"anthropic.resources.Messages.create",
return_value=mock_anthropic_response_with_tool_calls,
):
client = Anthropic(api_key="test-key", posthog_client=mock_client)
client = Anthropic(api_key="test-key", insights_client=mock_client)
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=200,
@@ -660,7 +680,7 @@ def test_tool_calls_in_output_choices(
},
}
],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
assert response == mock_anthropic_response_with_tool_calls
@@ -704,7 +724,7 @@ def test_tool_calls_only_no_content(
"anthropic.resources.Messages.create",
return_value=mock_anthropic_response_tool_calls_only,
):
client = Anthropic(api_key="test-key", posthog_client=mock_client)
client = Anthropic(api_key="test-key", insights_client=mock_client)
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=200,
@@ -723,7 +743,7 @@ def test_tool_calls_only_no_content(
},
}
],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
assert response == mock_anthropic_response_tool_calls_only
@@ -770,7 +790,7 @@ def test_async_tool_calls_in_output_choices(
"anthropic.resources.AsyncMessages.create",
side_effect=mock_async_create,
):
async_client = AsyncAnthropic(api_key="test-key", posthog_client=mock_client)
async_client = AsyncAnthropic(api_key="test-key", insights_client=mock_client)
async def run_test():
return await async_client.messages.create(
@@ -790,7 +810,7 @@ def test_async_tool_calls_in_output_choices(
},
}
],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
response = asyncio.run(run_test())
@@ -835,7 +855,7 @@ def test_streaming_with_tool_calls(mock_client, mock_anthropic_stream_with_tools
"anthropic.resources.Messages.create",
return_value=mock_anthropic_stream_with_tools,
):
client = Anthropic(api_key="test-key", posthog_client=mock_client)
client = Anthropic(api_key="test-key", insights_client=mock_client)
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
system="You are a helpful weather assistant.",
@@ -857,7 +877,7 @@ def test_streaming_with_tool_calls(mock_client, mock_anthropic_stream_with_tools
}
],
stream=True,
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
# Consume the stream - this triggers the finally block synchronously
@@ -927,6 +947,7 @@ def test_streaming_with_tool_calls(mock_client, mock_anthropic_stream_with_tools
assert props["$ai_output_tokens"] == 25
assert props["$ai_cache_read_input_tokens"] == 5
assert props["$ai_cache_creation_input_tokens"] == 0
assert props["$ai_tokens_source"] == "sdk"
# Verify raw usage is captured in streaming mode (merged from events)
assert "$ai_usage" in props
@@ -956,7 +977,7 @@ def test_async_streaming_with_tool_calls(mock_client, mock_anthropic_stream_with
"anthropic.resources.AsyncMessages.create",
side_effect=mock_async_create,
):
async_client = AsyncAnthropic(api_key="test-key", posthog_client=mock_client)
async_client = AsyncAnthropic(api_key="test-key", insights_client=mock_client)
async def run_test():
response = await async_client.messages.create(
@@ -980,7 +1001,7 @@ def test_async_streaming_with_tool_calls(mock_client, mock_anthropic_stream_with
}
],
stream=True,
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
# Consume the async stream
@@ -1080,11 +1101,11 @@ def test_web_search_count(mock_client):
mock_response = MockResponseWithWebSearch()
with patch("anthropic.resources.Messages.create", return_value=mock_response):
client = Anthropic(api_key="test-key", posthog_client=mock_client)
client = Anthropic(api_key="test-key", insights_client=mock_client)
response = client.messages.create(
model="claude-3-opus-20240229",
messages=[{"role": "user", "content": "Search for recent news"}],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
assert response == mock_response
@@ -1158,12 +1179,12 @@ def test_streaming_with_web_search(mock_client, mock_anthropic_stream_with_web_s
"anthropic.resources.Messages.create",
return_value=mock_anthropic_stream_with_web_search,
):
client = Anthropic(api_key="test-key", posthog_client=mock_client)
client = Anthropic(api_key="test-key", insights_client=mock_client)
response = client.messages.create(
model="claude-3-opus-20240229",
messages=[{"role": "user", "content": "Search for recent news"}],
stream=True,
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
# Consume the stream - this triggers the finally block synchronously
@@ -1213,13 +1234,13 @@ def test_async_with_web_search(mock_client):
"anthropic.resources.AsyncMessages.create",
side_effect=mock_async_create,
):
async_client = AsyncAnthropic(api_key="test-key", posthog_client=mock_client)
async_client = AsyncAnthropic(api_key="test-key", insights_client=mock_client)
async def run_test():
response = await async_client.messages.create(
model="claude-3-opus-20240229",
messages=[{"role": "user", "content": "Search for recent news"}],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
return response
@@ -1257,14 +1278,14 @@ def test_async_streaming_with_web_search(
"anthropic.resources.AsyncMessages.create",
side_effect=mock_async_create,
):
async_client = AsyncAnthropic(api_key="test-key", posthog_client=mock_client)
async_client = AsyncAnthropic(api_key="test-key", insights_client=mock_client)
async def run_test():
response = await async_client.messages.create(
model="claude-3-opus-20240229",
messages=[{"role": "user", "content": "Search for recent news"}],
stream=True,
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
# Consume the async stream
@@ -1283,3 +1304,99 @@ def test_async_streaming_with_web_search(
assert props["$ai_web_search_count"] == 2
assert props["$ai_input_tokens"] == 50
assert props["$ai_output_tokens"] == 25
# =======================
# Distinct ID Context Tests
# =======================
def test_no_distinct_id_uses_trace_id_and_personless(
mock_client, mock_anthropic_response
):
"""When no distinct_id is provided and no outer context, trace_id is used and event is personless."""
with patch(
"anthropic.resources.Messages.create", return_value=mock_anthropic_response
):
client = Anthropic(api_key="test-key", insights_client=mock_client)
client.messages.create(
model="claude-3-opus-20240229",
messages=[{"role": "user", "content": "Hello"}],
insights_trace_id="trace-123",
)
call_args = mock_client.capture.call_args[1]
props = call_args["properties"]
assert call_args["distinct_id"] == "trace-123"
assert props["$process_person_profile"] is False
def test_explicit_distinct_id_creates_person_profile(
mock_client, mock_anthropic_response
):
"""When insights_distinct_id is explicitly passed, it is used and event is not personless."""
with patch(
"anthropic.resources.Messages.create", return_value=mock_anthropic_response
):
client = Anthropic(api_key="test-key", insights_client=mock_client)
client.messages.create(
model="claude-3-opus-20240229",
messages=[{"role": "user", "content": "Hello"}],
insights_distinct_id="user-123",
insights_trace_id="trace-123",
)
call_args = mock_client.capture.call_args[1]
props = call_args["properties"]
assert call_args["distinct_id"] == "user-123"
assert (
"$process_person_profile" not in props
or props["$process_person_profile"] is not False
)
def test_outer_context_distinct_id_is_used(mock_client, mock_anthropic_response):
"""When an outer context has a distinct_id, it should be used instead of trace_id."""
with patch(
"anthropic.resources.Messages.create", return_value=mock_anthropic_response
):
client = Anthropic(api_key="test-key", insights_client=mock_client)
with new_context():
identify_context("outer-user-456")
client.messages.create(
model="claude-3-opus-20240229",
messages=[{"role": "user", "content": "Hello"}],
insights_trace_id="trace-123",
)
call_args = mock_client.capture.call_args[1]
props = call_args["properties"]
assert call_args["distinct_id"] == "outer-user-456"
assert (
"$process_person_profile" not in props
or props["$process_person_profile"] is not False
)
def test_explicit_distinct_id_overrides_outer_context(
mock_client, mock_anthropic_response
):
"""When both outer context and explicit insights_distinct_id are set, explicit wins."""
with patch(
"anthropic.resources.Messages.create", return_value=mock_anthropic_response
):
client = Anthropic(api_key="test-key", insights_client=mock_client)
with new_context():
identify_context("outer-user-456")
client.messages.create(
model="claude-3-opus-20240229",
messages=[{"role": "user", "content": "Hello"}],
insights_distinct_id="explicit-user-789",
insights_trace_id="trace-123",
)
call_args = mock_client.capture.call_args[1]
assert call_args["distinct_id"] == "explicit-user-789"
@@ -6,7 +6,7 @@ import pytest
try:
from google import genai as google_genai
from posthog.ai.gemini import Client
from hanzo_insights.ai.gemini import Client
GEMINI_AVAILABLE = True
except ImportError:
@@ -19,7 +19,7 @@ pytestmark = pytest.mark.skipif(
@pytest.fixture
def mock_client():
with patch("posthog.client.Client") as mock_client:
with patch("hanzo_insights.client.Client") as mock_client:
mock_client.privacy_mode = False
yield mock_client
@@ -172,13 +172,13 @@ def test_new_client_basic_generation(
"""Test the new Client/Models API structure"""
mock_google_genai_client.models.generate_content.return_value = mock_gemini_response
client = Client(api_key="test-key", posthog_client=mock_client)
client = Client(api_key="test-key", insights_client=mock_client)
response = client.models.generate_content(
model="gemini-2.0-flash",
contents=["Tell me a fun fact about hedgehogs"],
posthog_distinct_id="test-id",
posthog_properties={"foo": "bar"},
insights_distinct_id="test-id",
insights_properties={"foo": "bar"},
)
assert response == mock_gemini_response
@@ -239,13 +239,13 @@ def test_new_client_streaming_with_generate_content_stream(
mock_streaming_response()
)
client = Client(api_key="test-key", posthog_client=mock_client)
client = Client(api_key="test-key", insights_client=mock_client)
response = client.models.generate_content_stream(
model="gemini-2.0-flash",
contents=["Write a short story"],
posthog_distinct_id="test-id",
posthog_properties={"feature": "streaming"},
insights_distinct_id="test-id",
insights_properties={"feature": "streaming"},
)
chunks = list(response)
@@ -298,7 +298,7 @@ def test_new_client_streaming_with_tools(mock_client, mock_google_genai_client):
mock_streaming_response()
)
client = Client(api_key="test-key", posthog_client=mock_client)
client = Client(api_key="test-key", insights_client=mock_client)
# Create mock tools configuration
mock_tool = MagicMock()
@@ -326,8 +326,8 @@ def test_new_client_streaming_with_tools(mock_client, mock_google_genai_client):
model="gemini-2.0-flash",
contents=["What's the weather in SF?"],
config=mock_config,
posthog_distinct_id="test-id",
posthog_properties={"feature": "streaming_with_tools"},
insights_distinct_id="test-id",
insights_properties={"feature": "streaming_with_tools"},
)
chunks = list(response)
@@ -357,13 +357,13 @@ def test_new_client_groups(mock_client, mock_google_genai_client, mock_gemini_re
"""Test groups functionality with new Client API"""
mock_google_genai_client.models.generate_content.return_value = mock_gemini_response
client = Client(api_key="test-key", posthog_client=mock_client)
client = Client(api_key="test-key", insights_client=mock_client)
client.models.generate_content(
model="gemini-2.0-flash",
contents=["Hello"],
posthog_distinct_id="test-id",
posthog_groups={"company": "company_123"},
insights_distinct_id="test-id",
insights_groups={"company": "company_123"},
)
call_args = mock_client.capture.call_args[1]
@@ -376,13 +376,13 @@ def test_new_client_privacy_mode_local(
"""Test local privacy mode with new Client API"""
mock_google_genai_client.models.generate_content.return_value = mock_gemini_response
client = Client(api_key="test-key", posthog_client=mock_client)
client = Client(api_key="test-key", insights_client=mock_client)
client.models.generate_content(
model="gemini-2.0-flash",
contents=["Hello"],
posthog_distinct_id="test-id",
posthog_privacy_mode=True,
insights_distinct_id="test-id",
insights_privacy_mode=True,
)
call_args = mock_client.capture.call_args[1]
@@ -399,12 +399,12 @@ def test_new_client_privacy_mode_global(
mock_google_genai_client.models.generate_content.return_value = mock_gemini_response
client = Client(api_key="test-key", posthog_client=mock_client)
client = Client(api_key="test-key", insights_client=mock_client)
client.models.generate_content(
model="gemini-2.0-flash",
contents=["Hello"],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
call_args = mock_client.capture.call_args[1]
@@ -419,11 +419,11 @@ def test_new_client_different_input_formats(
"""Test different input formats with new Client API"""
mock_google_genai_client.models.generate_content.return_value = mock_gemini_response
client = Client(api_key="test-key", posthog_client=mock_client)
client = Client(api_key="test-key", insights_client=mock_client)
# Test string input
client.models.generate_content(
model="gemini-2.0-flash", contents="Hello", posthog_distinct_id="test-id"
model="gemini-2.0-flash", contents="Hello", insights_distinct_id="test-id"
)
call_args = mock_client.capture.call_args[1]
props = call_args["properties"]
@@ -434,7 +434,7 @@ def test_new_client_different_input_formats(
client.models.generate_content(
model="gemini-2.0-flash",
contents=[{"role": "user", "parts": [{"text": "hey"}]}],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
call_args = mock_client.capture.call_args[1]
props = call_args["properties"]
@@ -447,7 +447,7 @@ def test_new_client_different_input_formats(
client.models.generate_content(
model="gemini-2.0-flash",
contents=[{"role": "user", "parts": [{"text": "Hello "}, {"text": "world"}]}],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
call_args = mock_client.capture.call_args[1]
props = call_args["properties"]
@@ -464,7 +464,7 @@ def test_new_client_different_input_formats(
# Test list input with string
mock_client.capture.reset_mock()
client.models.generate_content(
model="gemini-2.0-flash", contents=["List item"], posthog_distinct_id="test-id"
model="gemini-2.0-flash", contents=["List item"], insights_distinct_id="test-id"
)
call_args = mock_client.capture.call_args[1]
props = call_args["properties"]
@@ -477,12 +477,12 @@ def test_new_client_model_parameters(
"""Test model parameters with new Client API"""
mock_google_genai_client.models.generate_content.return_value = mock_gemini_response
client = Client(api_key="test-key", posthog_client=mock_client)
client = Client(api_key="test-key", insights_client=mock_client)
client.models.generate_content(
model="gemini-2.0-flash",
contents=["Hello"],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
temperature=0.7,
max_tokens=100,
)
@@ -496,16 +496,16 @@ def test_new_client_model_parameters(
def test_new_client_default_settings(
mock_client, mock_google_genai_client, mock_gemini_response
):
"""Test client with default PostHog settings"""
"""Test client with default Insights settings"""
mock_google_genai_client.models.generate_content.return_value = mock_gemini_response
client = Client(
api_key="test-key",
posthog_client=mock_client,
posthog_distinct_id="default_user",
posthog_properties={"team": "ai"},
posthog_privacy_mode=False,
posthog_groups={"company": "acme_corp"},
insights_client=mock_client,
insights_distinct_id="default_user",
insights_properties={"team": "ai"},
insights_privacy_mode=False,
insights_groups={"company": "acme_corp"},
)
# Call without overriding defaults
@@ -527,21 +527,21 @@ def test_new_client_override_defaults(
client = Client(
api_key="test-key",
posthog_client=mock_client,
posthog_distinct_id="default_user",
posthog_properties={"team": "ai"},
posthog_privacy_mode=False,
posthog_groups={"company": "acme_corp"},
insights_client=mock_client,
insights_distinct_id="default_user",
insights_properties={"team": "ai"},
insights_privacy_mode=False,
insights_groups={"company": "acme_corp"},
)
# Override defaults in call
client.models.generate_content(
model="gemini-2.0-flash",
contents=["Hello"],
posthog_distinct_id="specific_user",
posthog_properties={"feature": "chat", "urgent": True},
posthog_privacy_mode=True,
posthog_groups={"organization": "special_org"},
insights_distinct_id="specific_user",
insights_properties={"feature": "chat", "urgent": True},
insights_privacy_mode=True,
insights_groups={"organization": "special_org"},
)
call_args = mock_client.capture.call_args[1]
@@ -577,7 +577,7 @@ def test_vertex_ai_parameters_passed_through(
location="us-central1",
debug_config=mock_debug_config,
http_options=mock_http_options,
posthog_client=mock_client,
insights_client=mock_client,
)
# Verify genai.Client was called with correct parameters
@@ -597,7 +597,7 @@ def test_api_key_mode(mock_client, mock_google_genai_client):
# Create client with just API key (traditional mode)
Client(
api_key="test-api-key",
posthog_client=mock_client,
insights_client=mock_client,
)
# Verify genai.Client was called with only api_key
@@ -618,7 +618,7 @@ def test_vertex_ai_mode_with_optional_api_key(
api_key="test-api-key",
credentials=mock_credentials,
project="test-project",
posthog_client=mock_client,
insights_client=mock_client,
)
# Verify genai.Client was called with both Vertex AI params and API key
@@ -634,7 +634,7 @@ def test_tool_use_response(mock_client, mock_google_genai_client, mock_gemini_re
"""Test that tools defined in config are captured in $ai_tools property"""
mock_google_genai_client.models.generate_content.return_value = mock_gemini_response
client = Client(api_key="test-key", posthog_client=mock_client)
client = Client(api_key="test-key", insights_client=mock_client)
# Create mock tools configuration
mock_tool = MagicMock()
@@ -664,8 +664,8 @@ def test_tool_use_response(mock_client, mock_google_genai_client, mock_gemini_re
model="gemini-2.5-flash",
contents=["hey"],
config=mock_config,
posthog_distinct_id="test-id",
posthog_properties={"foo": "bar"},
insights_distinct_id="test-id",
insights_properties={"foo": "bar"},
)
assert response == mock_gemini_response
@@ -702,12 +702,12 @@ def test_function_calls_in_output_choices(
mock_gemini_response_with_function_calls
)
client = Client(api_key="test-key", posthog_client=mock_client)
client = Client(api_key="test-key", insights_client=mock_client)
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=["What's the weather in San Francisco?"],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
assert response == mock_gemini_response_with_function_calls
@@ -751,12 +751,12 @@ def test_function_calls_only_no_content(
mock_gemini_response_function_calls_only
)
client = Client(api_key="test-key", posthog_client=mock_client)
client = Client(api_key="test-key", insights_client=mock_client)
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=["Get weather for New York"],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
assert response == mock_gemini_response_function_calls_only
@@ -810,12 +810,12 @@ def test_cache_and_reasoning_tokens(mock_client, mock_google_genai_client):
mock_google_genai_client.models.generate_content.return_value = mock_response
client = Client(api_key="test-key", posthog_client=mock_client)
client = Client(api_key="test-key", insights_client=mock_client)
response = client.models.generate_content(
model="gemini-2.5-pro",
contents="Test with cache",
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
assert response == mock_response
@@ -869,19 +869,19 @@ def test_streaming_cache_and_reasoning_tokens(mock_client, mock_google_genai_cli
mock_stream = iter([chunk1, chunk2])
mock_google_genai_client.models.generate_content_stream.return_value = mock_stream
client = Client(api_key="test-key", posthog_client=mock_client)
client = Client(api_key="test-key", insights_client=mock_client)
response = client.models.generate_content_stream(
model="gemini-2.5-pro",
contents="Test streaming with cache",
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
# Consume the stream
result = list(response)
assert len(result) == 2
# Check PostHog capture was called
# Check Insights capture was called
assert mock_client.capture.call_count == 1
call_args = mock_client.capture.call_args[1]
@@ -946,11 +946,11 @@ def test_web_search_grounding(mock_client, mock_google_genai_client):
# Mock the generate_content method
mock_google_genai_client.models.generate_content.return_value = mock_response
client = Client(api_key="test-key", posthog_client=mock_client)
client = Client(api_key="test-key", insights_client=mock_client)
response = client.models.generate_content(
model="gemini-2.5-flash",
contents="What's the latest news?",
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
assert response == mock_response
@@ -1015,12 +1015,12 @@ def test_streaming_with_web_search(mock_client, mock_google_genai_client):
mock_streaming_response()
)
client = Client(api_key="test-key", posthog_client=mock_client)
client = Client(api_key="test-key", insights_client=mock_client)
response = client.models.generate_content_stream(
model="gemini-2.5-flash",
contents="What's the latest news?",
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
chunks = list(response)
@@ -1080,12 +1080,12 @@ def test_empty_grounding_metadata_no_web_search(mock_client, mock_google_genai_c
# Mock the generate_content method
mock_google_genai_client.models.generate_content.return_value = mock_response
client = Client(api_key="test-key", posthog_client=mock_client)
client = Client(api_key="test-key", insights_client=mock_client)
response = client.models.generate_content(
model="gemini-2.5-flash",
contents="Hello",
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
assert response == mock_response
@@ -1143,12 +1143,12 @@ def test_empty_array_grounding_metadata_no_web_search(
# Mock the generate_content method
mock_google_genai_client.models.generate_content.return_value = mock_response
client = Client(api_key="test-key", posthog_client=mock_client)
client = Client(api_key="test-key", insights_client=mock_client)
response = client.models.generate_content(
model="gemini-2.5-flash",
contents="What can you do?",
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
assert response == mock_response
@@ -5,7 +5,7 @@ import pytest
try:
from google import genai as google_genai
from posthog.ai.gemini import AsyncClient
from hanzo_insights.ai.gemini import AsyncClient
GEMINI_AVAILABLE = True
except ImportError:
@@ -21,7 +21,7 @@ pytestmark = [
@pytest.fixture
def mock_client():
with patch("posthog.client.Client") as mock_client:
with patch("hanzo_insights.client.Client") as mock_client:
mock_client.privacy_mode = False
yield mock_client
@@ -121,13 +121,13 @@ async def test_async_client_basic_generation(
return_value=mock_gemini_response
)
client = AsyncClient(api_key="test-key", posthog_client=mock_client)
client = AsyncClient(api_key="test-key", insights_client=mock_client)
response = await client.models.generate_content(
model="gemini-2.0-flash",
contents=["Tell me a fun fact about hedgehogs"],
posthog_distinct_id="test-id",
posthog_properties={"foo": "bar"},
insights_distinct_id="test-id",
insights_properties={"foo": "bar"},
)
assert response == mock_gemini_response
@@ -178,13 +178,13 @@ async def test_async_client_streaming_with_generate_content_stream(
return_value=mock_streaming_response()
)
client = AsyncClient(api_key="test-key", posthog_client=mock_client)
client = AsyncClient(api_key="test-key", insights_client=mock_client)
response = await client.models.generate_content_stream(
model="gemini-2.0-flash",
contents=["Write a short story"],
posthog_distinct_id="test-id",
posthog_properties={"feature": "streaming"},
insights_distinct_id="test-id",
insights_properties={"feature": "streaming"},
)
chunks = []
@@ -239,7 +239,7 @@ async def test_async_client_streaming_with_tools(mock_client, mock_google_genai_
return_value=mock_streaming_response()
)
client = AsyncClient(api_key="test-key", posthog_client=mock_client)
client = AsyncClient(api_key="test-key", insights_client=mock_client)
# Create mock tools configuration
mock_tool = MagicMock()
@@ -267,8 +267,8 @@ async def test_async_client_streaming_with_tools(mock_client, mock_google_genai_
model="gemini-2.0-flash",
contents=["What's the weather in SF?"],
config=mock_config,
posthog_distinct_id="test-id",
posthog_properties={"feature": "streaming_with_tools"},
insights_distinct_id="test-id",
insights_properties={"feature": "streaming_with_tools"},
)
chunks = []
@@ -305,13 +305,13 @@ async def test_async_client_groups(
return_value=mock_gemini_response
)
client = AsyncClient(api_key="test-key", posthog_client=mock_client)
client = AsyncClient(api_key="test-key", insights_client=mock_client)
await client.models.generate_content(
model="gemini-2.0-flash",
contents=["Hello"],
posthog_distinct_id="test-id",
posthog_groups={"company": "company_123"},
insights_distinct_id="test-id",
insights_groups={"company": "company_123"},
)
call_args = mock_client.capture.call_args[1]
@@ -326,13 +326,13 @@ async def test_async_client_privacy_mode_local(
return_value=mock_gemini_response
)
client = AsyncClient(api_key="test-key", posthog_client=mock_client)
client = AsyncClient(api_key="test-key", insights_client=mock_client)
await client.models.generate_content(
model="gemini-2.0-flash",
contents=["Hello"],
posthog_distinct_id="test-id",
posthog_privacy_mode=True,
insights_distinct_id="test-id",
insights_privacy_mode=True,
)
call_args = mock_client.capture.call_args[1]
@@ -351,12 +351,12 @@ async def test_async_client_privacy_mode_global(
return_value=mock_gemini_response
)
client = AsyncClient(api_key="test-key", posthog_client=mock_client)
client = AsyncClient(api_key="test-key", insights_client=mock_client)
await client.models.generate_content(
model="gemini-2.0-flash",
contents=["Hello"],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
call_args = mock_client.capture.call_args[1]
@@ -373,11 +373,11 @@ async def test_async_client_different_input_formats(
return_value=mock_gemini_response
)
client = AsyncClient(api_key="test-key", posthog_client=mock_client)
client = AsyncClient(api_key="test-key", insights_client=mock_client)
# Test string input
await client.models.generate_content(
model="gemini-2.0-flash", contents="Hello", posthog_distinct_id="test-id"
model="gemini-2.0-flash", contents="Hello", insights_distinct_id="test-id"
)
call_args = mock_client.capture.call_args[1]
props = call_args["properties"]
@@ -388,7 +388,7 @@ async def test_async_client_different_input_formats(
await client.models.generate_content(
model="gemini-2.0-flash",
contents=[{"role": "user", "parts": [{"text": "hey"}]}],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
call_args = mock_client.capture.call_args[1]
props = call_args["properties"]
@@ -401,7 +401,7 @@ async def test_async_client_different_input_formats(
await client.models.generate_content(
model="gemini-2.0-flash",
contents=[{"role": "user", "parts": [{"text": "Hello "}, {"text": "world"}]}],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
call_args = mock_client.capture.call_args[1]
props = call_args["properties"]
@@ -418,7 +418,7 @@ async def test_async_client_different_input_formats(
# Test list input with string
mock_client.capture.reset_mock()
await client.models.generate_content(
model="gemini-2.0-flash", contents=["List item"], posthog_distinct_id="test-id"
model="gemini-2.0-flash", contents=["List item"], insights_distinct_id="test-id"
)
call_args = mock_client.capture.call_args[1]
props = call_args["properties"]
@@ -433,12 +433,12 @@ async def test_async_client_model_parameters(
return_value=mock_gemini_response
)
client = AsyncClient(api_key="test-key", posthog_client=mock_client)
client = AsyncClient(api_key="test-key", insights_client=mock_client)
await client.models.generate_content(
model="gemini-2.0-flash",
contents=["Hello"],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
temperature=0.7,
max_tokens=100,
)
@@ -452,18 +452,18 @@ async def test_async_client_model_parameters(
async def test_async_client_default_settings(
mock_client, mock_google_genai_client, mock_gemini_response
):
"""Test async client with default PostHog settings"""
"""Test async client with default Insights settings"""
mock_google_genai_client.aio.models.generate_content = AsyncMock(
return_value=mock_gemini_response
)
client = AsyncClient(
api_key="test-key",
posthog_client=mock_client,
posthog_distinct_id="default_user",
posthog_properties={"team": "ai"},
posthog_privacy_mode=False,
posthog_groups={"company": "acme_corp"},
insights_client=mock_client,
insights_distinct_id="default_user",
insights_properties={"team": "ai"},
insights_privacy_mode=False,
insights_groups={"company": "acme_corp"},
)
# Call without overriding defaults
@@ -487,21 +487,21 @@ async def test_async_client_override_defaults(
client = AsyncClient(
api_key="test-key",
posthog_client=mock_client,
posthog_distinct_id="default_user",
posthog_properties={"team": "ai"},
posthog_privacy_mode=False,
posthog_groups={"company": "acme_corp"},
insights_client=mock_client,
insights_distinct_id="default_user",
insights_properties={"team": "ai"},
insights_privacy_mode=False,
insights_groups={"company": "acme_corp"},
)
# Override defaults in call
await client.models.generate_content(
model="gemini-2.0-flash",
contents=["Hello"],
posthog_distinct_id="specific_user",
posthog_properties={"feature": "chat", "urgent": True},
posthog_privacy_mode=True,
posthog_groups={"organization": "special_org"},
insights_distinct_id="specific_user",
insights_properties={"feature": "chat", "urgent": True},
insights_privacy_mode=True,
insights_groups={"organization": "special_org"},
)
call_args = mock_client.capture.call_args[1]
@@ -539,7 +539,7 @@ async def test_async_vertex_ai_parameters_passed_through(
location="us-central1",
debug_config=mock_debug_config,
http_options=mock_http_options,
posthog_client=mock_client,
insights_client=mock_client,
)
# Verify genai.Client was called with correct parameters
@@ -559,7 +559,7 @@ async def test_async_api_key_mode(mock_client, mock_google_genai_client):
# Create async client with just API key (traditional mode)
AsyncClient(
api_key="test-api-key",
posthog_client=mock_client,
insights_client=mock_client,
)
# Verify genai.Client was called with only api_key
@@ -574,12 +574,12 @@ async def test_async_function_calls_in_output_choices(
return_value=mock_gemini_response_with_function_calls
)
client = AsyncClient(api_key="test-key", posthog_client=mock_client)
client = AsyncClient(api_key="test-key", insights_client=mock_client)
response = await client.models.generate_content(
model="gemini-2.5-flash",
contents=["What's the weather in San Francisco?"],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
assert response == mock_gemini_response_with_function_calls
@@ -637,12 +637,12 @@ async def test_async_cache_and_reasoning_tokens(mock_client, mock_google_genai_c
return_value=mock_response
)
client = AsyncClient(api_key="test-key", posthog_client=mock_client)
client = AsyncClient(api_key="test-key", insights_client=mock_client)
response = await client.models.generate_content(
model="gemini-2.5-pro",
contents="Test with cache",
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
assert response == mock_response
@@ -689,12 +689,12 @@ async def test_async_streaming_cache_and_reasoning_tokens(
return_value=mock_streaming_response()
)
client = AsyncClient(api_key="test-key", posthog_client=mock_client)
client = AsyncClient(api_key="test-key", insights_client=mock_client)
response = await client.models.generate_content_stream(
model="gemini-2.5-pro",
contents="Test streaming with cache",
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
# Consume the stream
@@ -704,7 +704,7 @@ async def test_async_streaming_cache_and_reasoning_tokens(
assert len(result) == 2
# Check PostHog capture was called
# Check Insights capture was called
assert mock_client.capture.call_count == 1
call_args = mock_client.capture.call_args[1]
@@ -761,11 +761,11 @@ async def test_async_web_search_grounding(mock_client, mock_google_genai_client)
return_value=mock_response
)
client = AsyncClient(api_key="test-key", posthog_client=mock_client)
client = AsyncClient(api_key="test-key", insights_client=mock_client)
response = await client.models.generate_content(
model="gemini-2.5-flash",
contents="What's the latest news?",
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
assert response == mock_response
@@ -829,12 +829,12 @@ async def test_async_streaming_with_web_search(mock_client, mock_google_genai_cl
return_value=mock_streaming_response()
)
client = AsyncClient(api_key="test-key", posthog_client=mock_client)
client = AsyncClient(api_key="test-key", insights_client=mock_client)
response = await client.models.generate_content_stream(
model="gemini-2.5-flash",
contents="What's the latest news?",
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
chunks = []
@@ -21,8 +21,8 @@ try:
from langgraph.graph.state import END, START, StateGraph
from langgraph.prebuilt import create_react_agent
from posthog.ai.langchain import CallbackHandler
from posthog.ai.langchain.callbacks import GenerationMetadata, SpanMetadata
from hanzo_insights.ai.langchain import CallbackHandler
from hanzo_insights.ai.langchain.callbacks import GenerationMetadata, SpanMetadata
LANGCHAIN_AVAILABLE = True
except ImportError:
@@ -53,9 +53,9 @@ ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")
@pytest.fixture(scope="function")
def mock_client():
with patch("posthog.client.Client") as mock_client:
with patch("hanzo_insights.client.Client") as mock_client:
mock_client.privacy_mode = False
logging.getLogger("posthog").setLevel(logging.DEBUG)
logging.getLogger("hanzo_insights").setLevel(logging.DEBUG)
yield mock_client
@@ -101,7 +101,7 @@ def test_metadata_capture(mock_client):
run_id,
messages=[{"role": "user", "content": "Who won the world series in 2020?"}],
invocation_params={"temperature": 0.5},
metadata={"ls_model_name": "hog-mini", "ls_provider": "posthog"},
metadata={"ls_model_name": "hog-mini", "ls_provider": "hanzo_insights"},
name="test",
)
expected = GenerationMetadata(
@@ -109,11 +109,11 @@ def test_metadata_capture(mock_client):
input=[{"role": "user", "content": "Who won the world series in 2020?"}],
start_time=1234567890,
model_params={"temperature": 0.5},
provider="posthog",
provider="hanzo_insights",
base_url="https://us.posthog.com",
name="test",
end_time=None,
posthog_properties=None,
insights_properties=None,
)
assert callbacks._runs[run_id] == expected
with patch("time.time", return_value=1234567891):
@@ -1049,7 +1049,7 @@ def test_base_url_retrieval(mock_client):
prompt = ChatPromptTemplate.from_messages([("user", "Foo")])
chain = prompt | ChatOpenAI(
api_key="test",
model="posthog-mini",
model="insights-mini",
base_url="https://test.posthog.com",
)
callbacks = CallbackHandler(mock_client)
@@ -1257,7 +1257,7 @@ def test_metadata_tools(mock_client):
run_id,
messages=[{"role": "user", "content": "What's the weather like in SF?"}],
invocation_params={"temperature": 0.5, "tools": tools},
metadata={"ls_model_name": "hog-mini", "ls_provider": "posthog"},
metadata={"ls_model_name": "hog-mini", "ls_provider": "hanzo_insights"},
name="test",
)
expected = GenerationMetadata(
@@ -1265,12 +1265,12 @@ def test_metadata_tools(mock_client):
input=[{"role": "user", "content": "What's the weather like in SF?"}],
start_time=1234567890,
model_params={"temperature": 0.5},
provider="posthog",
provider="hanzo_insights",
base_url="https://us.posthog.com",
name="test",
tools=tools,
end_time=None,
posthog_properties=None,
insights_properties=None,
)
assert callbacks._runs[run_id] == expected
with patch("time.time", return_value=1234567891):
@@ -1867,8 +1867,8 @@ def test_openai_reasoning_tokens_o4_mini(mock_client):
def test_callback_handler_without_client():
"""Test that CallbackHandler works properly when no PostHog client is passed."""
with patch("posthog.ai.langchain.callbacks.setup") as mock_setup:
"""Test that CallbackHandler works properly when no Insights client is passed."""
with patch("hanzo_insights.ai.langchain.callbacks.setup") as mock_setup:
mock_client = mock_setup.return_value
callbacks = CallbackHandler()
@@ -1894,7 +1894,7 @@ def test_callback_handler_without_client():
def test_convert_message_to_dict_tool_calls():
"""Test that _convert_message_to_dict properly converts tool calls in AIMessage."""
from posthog.ai.langchain.callbacks import _convert_message_to_dict
from hanzo_insights.ai.langchain.callbacks import _convert_message_to_dict
from langchain_core.messages import AIMessage
from langchain_core.messages.tool import ToolCall
@@ -1984,7 +1984,7 @@ def test_tool_definition(mock_client):
assert run == expected
assert callbacks._runs == {}
# Now test that the tools are properly captured in the PostHog event
# Now test that the tools are properly captured in the Insights event
mock_response = MagicMock()
mock_response.generations = [[MagicMock()]]
@@ -2236,7 +2236,7 @@ def test_agent_action_and_finish_imports():
from langchain.schema.agent import AgentAction, AgentFinish # type: ignore
# Verify they're available in the callbacks module
from posthog.ai.langchain.callbacks import CallbackHandler
from hanzo_insights.ai.langchain.callbacks import CallbackHandler
# Test on_agent_action with mock data
mock_client = MagicMock()
@@ -2266,8 +2266,8 @@ def test_agent_action_and_finish_imports():
assert call_args["event"] == "$ai_span"
def test_posthog_properties_field_in_generation_metadata(mock_client):
"""Test that posthog_properties is properly stored in GenerationMetadata."""
def test_insights_properties_field_in_generation_metadata(mock_client):
"""Test that insights_properties is properly stored in GenerationMetadata."""
callbacks = CallbackHandler(mock_client)
run_id = uuid.uuid4()
@@ -2281,7 +2281,7 @@ def test_posthog_properties_field_in_generation_metadata(mock_client):
metadata={
"ls_model_name": "gpt-4o",
"ls_provider": "openai",
"posthog_properties": {"$ai_billable": True},
"insights_properties": {"$ai_billable": True},
},
name="test",
)
@@ -2294,11 +2294,11 @@ def test_posthog_properties_field_in_generation_metadata(mock_client):
provider="openai",
base_url="https://api.openai.com",
name="test",
posthog_properties={"$ai_billable": True},
insights_properties={"$ai_billable": True},
end_time=None,
)
assert callbacks._runs[run_id] == expected
assert callbacks._runs[run_id].posthog_properties == {"$ai_billable": True}
assert callbacks._runs[run_id].insights_properties == {"$ai_billable": True}
callbacks._pop_run_metadata(run_id)
@@ -2313,15 +2313,15 @@ def test_posthog_properties_field_in_generation_metadata(mock_client):
metadata={
"ls_model_name": "gpt-4o",
"ls_provider": "openai",
"posthog_properties": {"$ai_billable": False},
"insights_properties": {"$ai_billable": False},
},
name="test",
)
assert callbacks._runs[run_id2].posthog_properties == {"$ai_billable": False}
assert callbacks._runs[run_id2].insights_properties == {"$ai_billable": False}
callbacks._pop_run_metadata(run_id2)
# Test when posthog_properties not provided
# Test when insights_properties not provided
run_id3 = uuid.uuid4()
with patch("time.time", return_value=1234567890):
callbacks._set_llm_metadata(
@@ -2333,7 +2333,7 @@ def test_posthog_properties_field_in_generation_metadata(mock_client):
name="test",
)
assert callbacks._runs[run_id3].posthog_properties is None
assert callbacks._runs[run_id3].insights_properties is None
def test_billable_property_in_generation_event(mock_client):
@@ -2349,7 +2349,7 @@ def test_billable_property_in_generation_event(mock_client):
run_id,
messages=[{"role": "user", "content": "Test"}],
metadata={
"posthog_properties": {"$ai_billable": True},
"insights_properties": {"$ai_billable": True},
"ls_model_name": "test-model",
},
invocation_params={},
@@ -2412,12 +2412,12 @@ def test_billable_with_real_chain(mock_client):
metadata={
"ls_model_name": "fake-model",
"ls_provider": "fake",
"posthog_properties": {"$ai_billable": True},
"insights_properties": {"$ai_billable": True},
},
invocation_params={"temperature": 0.7},
)
assert callbacks._runs[run_id].posthog_properties == {"$ai_billable": True}
assert callbacks._runs[run_id].insights_properties == {"$ai_billable": True}
mock_response = MagicMock()
mock_response.generations = [[MagicMock()]]
@@ -34,8 +34,8 @@ try:
ParsedResponseOutputText,
)
from posthog.ai.openai import OpenAI
from posthog.ai.openai.openai_async import AsyncOpenAI
from hanzo_insights.ai.openai import OpenAI
from hanzo_insights.ai.openai.openai_async import AsyncOpenAI
OPENAI_AVAILABLE = True
except ImportError:
@@ -49,7 +49,7 @@ pytestmark = pytest.mark.skipif(
@pytest.fixture
def mock_client():
with patch("posthog.client.Client") as mock_client:
with patch("hanzo_insights.client.Client") as mock_client:
mock_client.privacy_mode = False
yield mock_client
@@ -467,12 +467,12 @@ def test_basic_completion(mock_client, mock_openai_response):
"openai.resources.chat.completions.Completions.create",
return_value=mock_openai_response,
):
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello"}],
posthog_distinct_id="test-id",
posthog_properties={"foo": "bar"},
insights_distinct_id="test-id",
insights_properties={"foo": "bar"},
)
assert response == mock_openai_response
@@ -513,12 +513,12 @@ def test_embeddings(mock_client, mock_embedding_response):
"openai.resources.embeddings.Embeddings.create",
return_value=mock_embedding_response,
):
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
response = client.embeddings.create(
model="text-embedding-3-small",
input="Hello world",
posthog_distinct_id="test-id",
posthog_properties={"foo": "bar"},
insights_distinct_id="test-id",
insights_properties={"foo": "bar"},
)
assert response == mock_embedding_response
@@ -543,12 +543,12 @@ def test_groups(mock_client, mock_openai_response):
"openai.resources.chat.completions.Completions.create",
return_value=mock_openai_response,
):
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello"}],
posthog_distinct_id="test-id",
posthog_groups={"company": "test_company"},
insights_distinct_id="test-id",
insights_groups={"company": "test_company"},
)
assert response == mock_openai_response
@@ -564,12 +564,12 @@ def test_privacy_mode_local(mock_client, mock_openai_response):
"openai.resources.chat.completions.Completions.create",
return_value=mock_openai_response,
):
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello"}],
posthog_distinct_id="test-id",
posthog_privacy_mode=True,
insights_distinct_id="test-id",
insights_privacy_mode=True,
)
assert response == mock_openai_response
@@ -587,12 +587,12 @@ def test_privacy_mode_global(mock_client, mock_openai_response):
return_value=mock_openai_response,
):
mock_client.privacy_mode = True
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello"}],
posthog_distinct_id="test-id",
posthog_privacy_mode=False,
insights_distinct_id="test-id",
insights_privacy_mode=False,
)
assert response == mock_openai_response
@@ -609,7 +609,7 @@ def test_error(mock_client, mock_openai_response):
"openai.resources.chat.completions.Completions.create",
side_effect=Exception("Test error"),
):
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
with pytest.raises(Exception):
client.chat.completions.create(
model="gpt-4", messages=[{"role": "user", "content": "Hello"}]
@@ -628,12 +628,12 @@ def test_cached_tokens(mock_client, mock_openai_response_with_cached_tokens):
"openai.resources.chat.completions.Completions.create",
return_value=mock_openai_response_with_cached_tokens,
):
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello"}],
posthog_distinct_id="test-id",
posthog_properties={"foo": "bar"},
insights_distinct_id="test-id",
insights_properties={"foo": "bar"},
)
assert response == mock_openai_response_with_cached_tokens
@@ -666,7 +666,7 @@ def test_tool_calls(mock_client, mock_openai_response_with_tool_calls):
"openai.resources.chat.completions.Completions.create",
return_value=mock_openai_response_with_tool_calls,
):
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
response = client.chat.completions.create(
model="gpt-4",
messages=[
@@ -682,7 +682,7 @@ def test_tool_calls(mock_client, mock_openai_response_with_tool_calls):
},
}
],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
assert response == mock_openai_response_with_tool_calls
@@ -739,7 +739,7 @@ def test_tool_calls_only_no_content(mock_client, mock_openai_response_tool_calls
"openai.resources.chat.completions.Completions.create",
return_value=mock_openai_response_tool_calls_only,
):
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Get weather for New York"}],
@@ -753,7 +753,7 @@ def test_tool_calls_only_no_content(mock_client, mock_openai_response_tool_calls
},
}
],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
assert response == mock_openai_response_tool_calls_only
@@ -793,7 +793,7 @@ def test_responses_api_tool_calls(mock_client, mock_responses_api_with_tool_call
"openai.resources.responses.Responses.create",
return_value=mock_responses_api_with_tool_calls,
):
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
response = client.responses.create(
model="gpt-4o-mini",
input=[{"role": "user", "content": "What's the weather in Chicago?"}],
@@ -808,7 +808,7 @@ def test_responses_api_tool_calls(mock_client, mock_responses_api_with_tool_call
},
}
],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
assert response == mock_responses_api_with_tool_calls
@@ -851,7 +851,7 @@ def test_streaming_with_tool_calls(mock_client, streaming_tool_call_chunks):
# Set up the mock to return our chunks when iterated
mock_create.return_value = streaming_tool_call_chunks
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
# Call the streaming method
response_generator = client.chat.completions.create(
@@ -870,7 +870,7 @@ def test_streaming_with_tool_calls(mock_client, streaming_tool_call_chunks):
}
],
stream=True,
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
# Consume the generator to trigger the event capture
@@ -949,12 +949,12 @@ def test_responses_api(mock_client, mock_openai_response_with_responses_api):
"openai.resources.responses.Responses.create",
return_value=mock_openai_response_with_responses_api,
):
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
response = client.responses.create(
model="gpt-4o-mini",
input="Hello",
posthog_distinct_id="test-id",
posthog_properties={"foo": "bar"},
insights_distinct_id="test-id",
insights_properties={"foo": "bar"},
)
assert response == mock_openai_response_with_responses_api
assert mock_client.capture.call_count == 1
@@ -986,7 +986,7 @@ def test_responses_parse(mock_client, mock_parsed_response):
"openai.resources.responses.Responses.parse",
return_value=mock_parsed_response,
):
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
response = client.responses.parse(
model="gpt-4o-2024-08-06",
input=[
@@ -1016,8 +1016,8 @@ def test_responses_parse(mock_client, mock_parsed_response):
},
}
},
posthog_distinct_id="test-id",
posthog_properties={"foo": "bar"},
insights_distinct_id="test-id",
insights_properties={"foo": "bar"},
)
assert response == mock_parsed_response
@@ -1101,15 +1101,15 @@ def test_responses_api_streaming_with_tokens(mock_client):
"openai.resources.responses.Responses.create",
side_effect=mock_streaming_response,
):
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
# Consume the streaming response
response = client.responses.create(
model="gpt-4o-mini",
input=[{"role": "user", "content": "Test message"}],
stream=True,
posthog_distinct_id="test-id",
posthog_properties={"test": "streaming"},
insights_distinct_id="test-id",
insights_properties={"test": "streaming"},
)
# Consume all chunks
@@ -1153,7 +1153,7 @@ async def test_async_chat_streaming_with_tool_calls(
with patch(
"openai.resources.chat.completions.AsyncCompletions.create", new=mock_create
):
client = AsyncOpenAI(api_key="test-key", posthog_client=mock_client)
client = AsyncOpenAI(api_key="test-key", insights_client=mock_client)
response_stream = await client.chat.completions.create(
model="gpt-4",
@@ -1171,7 +1171,7 @@ async def test_async_chat_streaming_with_tool_calls(
}
],
stream=True,
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
chunks = []
@@ -1239,14 +1239,14 @@ async def test_async_responses_streaming_with_tokens(mock_client):
return chunk_iterable()
with patch("openai.resources.responses.AsyncResponses.create", new=mock_create):
client = AsyncOpenAI(api_key="test-key", posthog_client=mock_client)
client = AsyncOpenAI(api_key="test-key", insights_client=mock_client)
response_stream = await client.responses.create(
model="gpt-4o-mini",
input=[{"role": "user", "content": "Test message"}],
stream=True,
posthog_distinct_id="test-id",
posthog_properties={"test": "streaming"},
insights_distinct_id="test-id",
insights_properties={"test": "streaming"},
)
async for _ in response_stream:
@@ -1274,13 +1274,13 @@ async def test_async_embeddings_create(mock_client, mock_embedding_response):
mock_create = AsyncMock(return_value=mock_embedding_response)
with patch("openai.resources.embeddings.AsyncEmbeddings.create", new=mock_create):
client = AsyncOpenAI(api_key="test-key", posthog_client=mock_client)
client = AsyncOpenAI(api_key="test-key", insights_client=mock_client)
response = await client.embeddings.create(
model="text-embedding-3-small",
input="Hello world",
posthog_distinct_id="test-id",
posthog_properties={"foo": "bar"},
insights_distinct_id="test-id",
insights_properties={"foo": "bar"},
)
assert response == mock_embedding_response
@@ -1304,7 +1304,7 @@ def test_tool_definition(mock_client, mock_openai_response):
"openai.resources.chat.completions.Completions.create",
return_value=mock_openai_response,
):
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
# Define tools to be passed to the create function
tools = [
@@ -1331,8 +1331,8 @@ def test_tool_definition(mock_client, mock_openai_response):
model="gpt-4o-mini",
messages=[{"role": "user", "content": "hey"}],
tools=tools,
posthog_distinct_id="test-id",
posthog_properties={"foo": "bar"},
insights_distinct_id="test-id",
insights_properties={"foo": "bar"},
)
assert response == mock_openai_response
@@ -1392,11 +1392,11 @@ def test_web_search_perplexity_style(mock_client):
mock_response = MockResponseWithAnnotations()
with patch("openai.resources.chat.Completions.create", return_value=mock_response):
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
response = client.chat.completions.create(
model="gpt-4-turbo",
messages=[{"role": "user", "content": "What's happening in tech?"}],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
assert response == mock_response
@@ -1439,19 +1439,19 @@ def test_web_search_responses_api(mock_client):
"openai.resources.responses.Responses.create", return_value=mock_response
):
# Manually call the tracking since we're testing the converter logic
from posthog.ai.utils import call_llm_and_track_usage
from hanzo_insights.ai.utils import call_llm_and_track_usage
def mock_create_call(**kwargs):
return mock_response
result = call_llm_and_track_usage(
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
ph_client=mock_client,
provider="openai",
posthog_trace_id=None,
posthog_properties=None,
posthog_privacy_mode=False,
posthog_groups=None,
insights_trace_id=None,
insights_properties=None,
insights_privacy_mode=False,
insights_groups=None,
base_url="https://api.openai.com/v1",
call_method=mock_create_call,
model="gpt-4o",
@@ -1533,12 +1533,12 @@ def test_streaming_with_web_search(mock_client, streaming_web_search_chunks):
with patch("openai.resources.chat.completions.Completions.create") as mock_create:
mock_create.return_value = streaming_web_search_chunks
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
response_generator = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Search for recent news"}],
stream=True,
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
# Consume the generator to trigger the event capture
@@ -1569,12 +1569,12 @@ def test_streaming_with_web_search_on_non_usage_chunk(
with patch("openai.resources.chat.completions.Completions.create") as mock_create:
mock_create.return_value = streaming_web_search_chunks
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
response_generator = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Search for recent news"}],
stream=True,
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
# Consume the generator to trigger the event capture
@@ -1629,12 +1629,12 @@ async def test_async_chat_with_web_search(mock_client):
with patch(
"openai.resources.chat.completions.AsyncCompletions.create", new=mock_create
):
client = AsyncOpenAI(api_key="test-key", posthog_client=mock_client)
client = AsyncOpenAI(api_key="test-key", insights_client=mock_client)
response = await client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Search for recent news"}],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
assert response == mock_response
@@ -1672,13 +1672,13 @@ async def test_async_chat_streaming_with_web_search(
with patch(
"openai.resources.chat.completions.AsyncCompletions.create", new=mock_create
):
client = AsyncOpenAI(api_key="test-key", posthog_client=mock_client)
client = AsyncOpenAI(api_key="test-key", insights_client=mock_client)
response_stream = await client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Search for recent news"}],
stream=True,
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
chunks = []
@@ -1742,13 +1742,13 @@ def test_streaming_chat_extracts_model_from_chunk_when_not_in_kwargs(mock_client
with patch("openai.resources.chat.completions.Completions.create") as mock_create:
mock_create.return_value = chunks
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
# Note: NOT passing model in kwargs - simulates stored prompt usage
response_generator = client.chat.completions.create(
messages=[{"role": "user", "content": "Hello"}],
stream=True,
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
# Consume the generator
@@ -1788,13 +1788,13 @@ def test_streaming_chat_prefers_kwargs_model_over_chunk_model(mock_client):
with patch("openai.resources.chat.completions.Completions.create") as mock_create:
mock_create.return_value = chunks
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
response_generator = client.chat.completions.create(
model="gpt-4o-from-kwargs", # Explicitly passed model
messages=[{"role": "user", "content": "Hello"}],
stream=True,
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
list(response_generator)
@@ -1839,13 +1839,13 @@ def test_streaming_responses_api_extracts_model_from_response_object(mock_client
with patch("openai.resources.responses.Responses.create") as mock_create:
mock_create.return_value = iter(chunks)
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
# Note: NOT passing model - simulates stored prompt
response_generator = client.responses.create(
input=[{"role": "user", "content": "Hello"}],
stream=True,
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
list(response_generator)
@@ -1886,12 +1886,12 @@ def test_non_streaming_extracts_model_from_response(mock_client):
"openai.resources.chat.completions.Completions.create",
return_value=mock_response,
):
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
# Note: NOT passing model in kwargs
response = client.chat.completions.create(
messages=[{"role": "user", "content": "Hello"}],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
assert response == mock_response
@@ -1948,12 +1948,12 @@ def test_non_streaming_responses_api_extracts_model_from_response(mock_client):
"openai.resources.responses.Responses.create",
return_value=mock_response,
):
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
# Note: NOT passing model in kwargs
response = client.responses.create(
input="Hello",
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
assert response == mock_response
@@ -1995,12 +1995,12 @@ def test_non_streaming_returns_none_when_no_model(mock_client):
"openai.resources.chat.completions.Completions.create",
return_value=mock_response,
):
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
# Note: NOT passing model in kwargs and response has no model
client.chat.completions.create(
messages=[{"role": "user", "content": "Hello"}],
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
call_args = mock_client.capture.call_args[1]
@@ -2032,12 +2032,12 @@ def test_streaming_falls_back_to_unknown_when_no_model(mock_client):
with patch("openai.resources.chat.completions.Completions.create") as mock_create:
mock_create.return_value = [chunk]
client = OpenAI(api_key="test-key", posthog_client=mock_client)
client = OpenAI(api_key="test-key", insights_client=mock_client)
response_generator = client.chat.completions.create(
messages=[{"role": "user", "content": "Hello"}],
stream=True,
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
list(response_generator)
@@ -2083,13 +2083,13 @@ async def test_async_streaming_chat_extracts_model_from_chunk(mock_client):
with patch(
"openai.resources.chat.completions.AsyncCompletions.create", new=mock_create
):
client = AsyncOpenAI(api_key="test-key", posthog_client=mock_client)
client = AsyncOpenAI(api_key="test-key", insights_client=mock_client)
# Note: NOT passing model
response_stream = await client.chat.completions.create(
messages=[{"role": "user", "content": "Hello"}],
stream=True,
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
async for _ in response_stream:
@@ -2137,12 +2137,12 @@ async def test_async_streaming_responses_extracts_model_from_response(mock_clien
return chunk_iterable()
with patch("openai.resources.responses.AsyncResponses.create", new=mock_create):
client = AsyncOpenAI(api_key="test-key", posthog_client=mock_client)
client = AsyncOpenAI(api_key="test-key", insights_client=mock_client)
response_stream = await client.responses.create(
input=[{"role": "user", "content": "Hello"}],
stream=True,
posthog_distinct_id="test-id",
insights_distinct_id="test-id",
)
async for _ in response_stream:
@@ -16,7 +16,7 @@ try:
TranscriptionSpanData,
)
from posthog.ai.openai_agents import PostHogTracingProcessor, instrument
from hanzo_insights.ai.openai_agents import InsightsTracingProcessor, instrument
OPENAI_AGENTS_AVAILABLE = True
except ImportError:
@@ -33,13 +33,13 @@ pytestmark = pytest.mark.skipif(
def mock_client():
client = MagicMock()
client.privacy_mode = False
logging.getLogger("posthog").setLevel(logging.DEBUG)
logging.getLogger("hanzo_insights").setLevel(logging.DEBUG)
return client
@pytest.fixture(scope="function")
def processor(mock_client):
return PostHogTracingProcessor(
return InsightsTracingProcessor(
client=mock_client,
distinct_id="test-user",
privacy_mode=False,
@@ -68,12 +68,12 @@ def mock_span():
return span
class TestPostHogTracingProcessor:
"""Tests for the PostHogTracingProcessor class."""
class TestInsightsTracingProcessor:
"""Tests for the InsightsTracingProcessor class."""
def test_initialization(self, mock_client):
"""Test processor initializes correctly."""
processor = PostHogTracingProcessor(
processor = InsightsTracingProcessor(
client=mock_client,
distinct_id="user@example.com",
privacy_mode=True,
@@ -93,7 +93,7 @@ class TestPostHogTracingProcessor:
def resolver(trace):
return trace.metadata.get("user_id", "default")
processor = PostHogTracingProcessor(
processor = InsightsTracingProcessor(
client=mock_client,
distinct_id=resolver,
)
@@ -127,7 +127,7 @@ class TestPostHogTracingProcessor:
def test_personless_mode_when_no_distinct_id(self, mock_client, mock_trace):
"""Test that trace events use personless mode when no distinct_id is provided."""
processor = PostHogTracingProcessor(
processor = InsightsTracingProcessor(
client=mock_client,
)
@@ -143,7 +143,7 @@ class TestPostHogTracingProcessor:
self, mock_client, mock_trace, mock_span
):
"""Test that span events use personless mode when no distinct_id is provided."""
processor = PostHogTracingProcessor(
processor = InsightsTracingProcessor(
client=mock_client,
)
@@ -168,7 +168,7 @@ class TestPostHogTracingProcessor:
def resolver(trace):
return None # Simulate no user ID available
processor = PostHogTracingProcessor(
processor = InsightsTracingProcessor(
client=mock_client,
distinct_id=resolver,
)
@@ -188,7 +188,7 @@ class TestPostHogTracingProcessor:
def test_person_profile_when_distinct_id_provided(self, mock_client, mock_trace):
"""Test that events create person profiles when distinct_id is provided."""
processor = PostHogTracingProcessor(
processor = InsightsTracingProcessor(
client=mock_client,
distinct_id="real-user",
)
@@ -375,7 +375,7 @@ class TestPostHogTracingProcessor:
def test_privacy_mode_redacts_content(self, mock_client, mock_span):
"""Test that privacy_mode redacts input/output content."""
processor = PostHogTracingProcessor(
processor = InsightsTracingProcessor(
client=mock_client,
distinct_id="test-user",
privacy_mode=True,
@@ -636,7 +636,7 @@ class TestPostHogTracingProcessor:
def test_groups_included_in_events(self, mock_client, mock_trace, mock_span):
"""Test that groups are included in captured events."""
processor = PostHogTracingProcessor(
processor = InsightsTracingProcessor(
client=mock_client,
distinct_id="test-user",
groups={"company": "acme", "team": "engineering"},
@@ -650,7 +650,7 @@ class TestPostHogTracingProcessor:
def test_additional_properties_included(self, mock_client, mock_trace):
"""Test that additional properties are included in events."""
processor = PostHogTracingProcessor(
processor = InsightsTracingProcessor(
client=mock_client,
distinct_id="test-user",
properties={"environment": "production", "version": "1.0"},
@@ -734,7 +734,7 @@ class TestPostHogTracingProcessor:
def resolver(trace):
return f"user-{trace.name}"
processor = PostHogTracingProcessor(
processor = InsightsTracingProcessor(
client=mock_client,
distinct_id=resolver,
)
@@ -755,7 +755,7 @@ class TestPostHogTracingProcessor:
def test_eviction_of_stale_entries(self, mock_client):
"""Test that stale entries are evicted when max is exceeded."""
processor = PostHogTracingProcessor(
processor = InsightsTracingProcessor(
client=mock_client,
distinct_id="test-user",
)
@@ -785,7 +785,7 @@ class TestInstrumentHelper:
)
mock_add.assert_called_once_with(processor)
assert isinstance(processor, PostHogTracingProcessor)
assert isinstance(processor, InsightsTracingProcessor)
def test_instrument_with_privacy_mode(self, mock_client):
"""Test instrument() respects privacy_mode."""
@@ -1,7 +1,7 @@
import unittest
from unittest.mock import MagicMock, patch
from posthog.ai.prompts import Prompts
from hanzo_insights.ai.prompts import Prompts
class MockResponse:
@@ -32,12 +32,16 @@ class TestPrompts(unittest.TestCase):
"deleted": False,
}
def create_mock_posthog(
self, personal_api_key="phx_test_key", host="https://us.posthog.com"
def create_mock_client(
self,
personal_api_key="phx_test_key",
project_api_key="phc_test_key",
host="https://us.insights.hanzo.ai",
):
"""Create a mock PostHog client."""
"""Create a mock Insights client."""
mock = MagicMock()
mock.personal_api_key = personal_api_key
mock.api_key = project_api_key
mock.raw_host = host
return mock
@@ -45,14 +49,14 @@ class TestPrompts(unittest.TestCase):
class TestPromptsGet(TestPrompts):
"""Tests for the Prompts.get() method."""
@patch("posthog.ai.prompts._get_session")
@patch("hanzo_insights.ai.prompts._get_session")
def test_successfully_fetch_a_prompt(self, mock_get_session):
"""Should successfully fetch a prompt."""
mock_get = mock_get_session.return_value.get
mock_get.return_value = MockResponse(json_data=self.mock_prompt_response)
posthog = self.create_mock_posthog()
prompts = Prompts(posthog)
client = self.create_mock_client()
prompts = Prompts(client)
result = prompts.get("test-prompt")
@@ -61,23 +65,47 @@ class TestPromptsGet(TestPrompts):
call_args = mock_get.call_args
self.assertEqual(
call_args[0][0],
"https://us.posthog.com/api/environments/@current/llm_prompts/name/test-prompt/",
"https://us.insights.hanzo.ai/api/environments/@current/llm_prompts/name/test-prompt/?token=phc_test_key",
)
self.assertIn("Authorization", call_args[1]["headers"])
self.assertEqual(
call_args[1]["headers"]["Authorization"], "Bearer phx_test_key"
)
@patch("posthog.ai.prompts._get_session")
@patch("posthog.ai.prompts.time.time")
@patch("hanzo_insights.ai.prompts._get_session")
def test_successfully_fetch_a_specific_prompt_version(self, mock_get_session):
"""Should successfully fetch a specific prompt version."""
mock_get = mock_get_session.return_value.get
versioned_prompt_response = {
**self.mock_prompt_response,
"prompt": "Prompt version 1",
"version": 1,
}
mock_get.return_value = MockResponse(json_data=versioned_prompt_response)
client = self.create_mock_client()
prompts = Prompts(client)
result = prompts.get("test-prompt", version=1)
self.assertEqual(result, versioned_prompt_response["prompt"])
mock_get.assert_called_once()
call_args = mock_get.call_args
self.assertEqual(
call_args[0][0],
"https://us.insights.hanzo.ai/api/environments/@current/llm_prompts/name/test-prompt/?token=phc_test_key&version=1",
)
@patch("hanzo_insights.ai.prompts._get_session")
@patch("hanzo_insights.ai.prompts.time.time")
def test_return_cached_prompt_when_fresh(self, mock_time, mock_get_session):
"""Should return cached prompt when fresh (no API call)."""
mock_get = mock_get_session.return_value.get
mock_get.return_value = MockResponse(json_data=self.mock_prompt_response)
mock_time.return_value = 1000.0
posthog = self.create_mock_posthog()
prompts = Prompts(posthog)
client = self.create_mock_client()
prompts = Prompts(client)
# First call - fetches from API
result1 = prompts.get("test-prompt", cache_ttl_seconds=300)
@@ -92,8 +120,43 @@ class TestPromptsGet(TestPrompts):
self.assertEqual(result2, self.mock_prompt_response["prompt"])
self.assertEqual(mock_get.call_count, 1) # No additional fetch
@patch("posthog.ai.prompts._get_session")
@patch("posthog.ai.prompts.time.time")
@patch("hanzo_insights.ai.prompts._get_session")
def test_cache_latest_and_versioned_prompts_separately(self, mock_get_session):
"""Should cache latest and historical prompt versions separately."""
mock_get = mock_get_session.return_value.get
latest_prompt_response = {
**self.mock_prompt_response,
"prompt": "Latest prompt",
"version": 2,
}
versioned_prompt_response = {
**self.mock_prompt_response,
"prompt": "Prompt version 1",
"version": 1,
}
mock_get.side_effect = [
MockResponse(json_data=latest_prompt_response),
MockResponse(json_data=versioned_prompt_response),
]
client = self.create_mock_client()
prompts = Prompts(client)
self.assertEqual(prompts.get("test-prompt"), latest_prompt_response["prompt"])
self.assertEqual(
prompts.get("test-prompt", version=1),
versioned_prompt_response["prompt"],
)
self.assertEqual(prompts.get("test-prompt"), latest_prompt_response["prompt"])
self.assertEqual(
prompts.get("test-prompt", version=1),
versioned_prompt_response["prompt"],
)
self.assertEqual(mock_get.call_count, 2)
@patch("hanzo_insights.ai.prompts._get_session")
@patch("hanzo_insights.ai.prompts.time.time")
def test_refetch_when_cache_is_stale(self, mock_time, mock_get_session):
"""Should refetch when cache is stale."""
mock_get = mock_get_session.return_value.get
@@ -108,8 +171,8 @@ class TestPromptsGet(TestPrompts):
]
mock_time.return_value = 1000.0
posthog = self.create_mock_posthog()
prompts = Prompts(posthog)
client = self.create_mock_client()
prompts = Prompts(client)
# First call - fetches from API
result1 = prompts.get("test-prompt", cache_ttl_seconds=60)
@@ -124,9 +187,9 @@ class TestPromptsGet(TestPrompts):
self.assertEqual(result2, updated_prompt_response["prompt"])
self.assertEqual(mock_get.call_count, 2)
@patch("posthog.ai.prompts._get_session")
@patch("posthog.ai.prompts.time.time")
@patch("posthog.ai.prompts.log")
@patch("hanzo_insights.ai.prompts._get_session")
@patch("hanzo_insights.ai.prompts.time.time")
@patch("hanzo_insights.ai.prompts.log")
def test_use_stale_cache_on_fetch_failure_with_warning(
self, mock_log, mock_time, mock_get_session
):
@@ -138,8 +201,8 @@ class TestPromptsGet(TestPrompts):
]
mock_time.return_value = 1000.0
posthog = self.create_mock_posthog()
prompts = Prompts(posthog)
client = self.create_mock_client()
prompts = Prompts(client)
# First call - populates cache
result1 = prompts.get("test-prompt", cache_ttl_seconds=60)
@@ -157,8 +220,8 @@ class TestPromptsGet(TestPrompts):
warning_call = mock_log.warning.call_args
self.assertIn("using stale cache", warning_call[0][0])
@patch("posthog.ai.prompts._get_session")
@patch("posthog.ai.prompts.log")
@patch("hanzo_insights.ai.prompts._get_session")
@patch("hanzo_insights.ai.prompts.log")
def test_use_fallback_when_no_cache_and_fetch_fails_with_warning(
self, mock_log, mock_get_session
):
@@ -166,8 +229,8 @@ class TestPromptsGet(TestPrompts):
mock_get = mock_get_session.return_value.get
mock_get.side_effect = Exception("Network error")
posthog = self.create_mock_posthog()
prompts = Prompts(posthog)
client = self.create_mock_client()
prompts = Prompts(client)
fallback = "Default system prompt."
result = prompts.get("test-prompt", fallback=fallback)
@@ -179,42 +242,59 @@ class TestPromptsGet(TestPrompts):
warning_call = mock_log.warning.call_args
self.assertIn("using fallback", warning_call[0][0])
@patch("posthog.ai.prompts._get_session")
@patch("hanzo_insights.ai.prompts._get_session")
def test_throw_when_no_cache_no_fallback_and_fetch_fails(self, mock_get_session):
"""Should throw when no cache, no fallback, and fetch fails."""
mock_get = mock_get_session.return_value.get
mock_get.side_effect = Exception("Network error")
posthog = self.create_mock_posthog()
prompts = Prompts(posthog)
client = self.create_mock_client()
prompts = Prompts(client)
with self.assertRaises(Exception) as context:
prompts.get("test-prompt")
self.assertIn("Network error", str(context.exception))
@patch("posthog.ai.prompts._get_session")
@patch("hanzo_insights.ai.prompts._get_session")
def test_handle_404_response(self, mock_get_session):
"""Should handle 404 response."""
mock_get = mock_get_session.return_value.get
mock_get.return_value = MockResponse(status_code=404, ok=False)
posthog = self.create_mock_posthog()
prompts = Prompts(posthog)
client = self.create_mock_client()
prompts = Prompts(client)
with self.assertRaises(Exception) as context:
prompts.get("nonexistent-prompt")
self.assertIn('Prompt "nonexistent-prompt" not found', str(context.exception))
@patch("posthog.ai.prompts._get_session")
@patch("hanzo_insights.ai.prompts._get_session")
def test_handle_404_response_for_specific_prompt_version(self, mock_get_session):
"""Should handle 404 response for a specific prompt version."""
mock_get = mock_get_session.return_value.get
mock_get.return_value = MockResponse(status_code=404, ok=False)
client = self.create_mock_client()
prompts = Prompts(client)
with self.assertRaises(Exception) as context:
prompts.get("nonexistent-prompt", version=3)
self.assertIn(
'Prompt "nonexistent-prompt" version 3 not found',
str(context.exception),
)
@patch("hanzo_insights.ai.prompts._get_session")
def test_handle_403_response(self, mock_get_session):
"""Should handle 403 response."""
mock_get = mock_get_session.return_value.get
mock_get.return_value = MockResponse(status_code=403, ok=False)
posthog = self.create_mock_posthog()
prompts = Prompts(posthog)
client = self.create_mock_client()
prompts = Prompts(client)
with self.assertRaises(Exception) as context:
prompts.get("restricted-prompt")
@@ -225,8 +305,8 @@ class TestPromptsGet(TestPrompts):
def test_throw_when_no_personal_api_key_configured(self):
"""Should throw when no personal_api_key is configured."""
posthog = self.create_mock_posthog(personal_api_key=None)
prompts = Prompts(posthog)
client = self.create_mock_client(personal_api_key=None)
prompts = Prompts(client)
with self.assertRaises(Exception) as context:
prompts.get("test-prompt")
@@ -235,47 +315,61 @@ class TestPromptsGet(TestPrompts):
"personal_api_key is required to fetch prompts", str(context.exception)
)
@patch("posthog.ai.prompts._get_session")
def test_throw_when_no_project_api_key_configured(self):
"""Should throw when no project_api_key is configured."""
client = self.create_mock_client(project_api_key=None)
prompts = Prompts(client)
with self.assertRaises(Exception) as context:
prompts.get("test-prompt")
self.assertIn(
"project_api_key is required to fetch prompts", str(context.exception)
)
@patch("hanzo_insights.ai.prompts._get_session")
def test_throw_when_api_returns_invalid_response_format(self, mock_get_session):
"""Should throw when API returns invalid response format."""
mock_get = mock_get_session.return_value.get
mock_get.return_value = MockResponse(json_data={"invalid": "response"})
posthog = self.create_mock_posthog()
prompts = Prompts(posthog)
client = self.create_mock_client()
prompts = Prompts(client)
with self.assertRaises(Exception) as context:
prompts.get("test-prompt")
self.assertIn("Invalid response format", str(context.exception))
@patch("posthog.ai.prompts._get_session")
def test_use_custom_host_from_posthog_options(self, mock_get_session):
"""Should use custom host from PostHog options."""
@patch("hanzo_insights.ai.prompts._get_session")
def test_use_custom_host_from_insights_options(self, mock_get_session):
"""Should use custom host from Insights options."""
mock_get = mock_get_session.return_value.get
mock_get.return_value = MockResponse(json_data=self.mock_prompt_response)
posthog = self.create_mock_posthog(host="https://eu.i.posthog.com")
prompts = Prompts(posthog)
client = self.create_mock_client(host="https://eu.insights.hanzo.ai")
prompts = Prompts(client)
prompts.get("test-prompt")
call_args = mock_get.call_args
self.assertTrue(
call_args[0][0].startswith("https://eu.i.posthog.com/"),
f"Expected URL to start with 'https://eu.i.posthog.com/', got {call_args[0][0]}",
call_args[0][0].startswith(
"https://eu.insights.hanzo.ai/api/environments/@current/llm_prompts/name/test-prompt/?token=phc_test_key"
),
f"Expected URL to start with 'https://eu.insights.hanzo.ai/api/environments/@current/llm_prompts/name/test-prompt/?token=phc_test_key', got {call_args[0][0]}",
)
@patch("posthog.ai.prompts._get_session")
@patch("posthog.ai.prompts.time.time")
@patch("hanzo_insights.ai.prompts._get_session")
@patch("hanzo_insights.ai.prompts.time.time")
def test_use_default_cache_ttl_5_minutes(self, mock_time, mock_get_session):
"""Should use default cache TTL (5 minutes) when not specified."""
mock_get = mock_get_session.return_value.get
mock_get.return_value = MockResponse(json_data=self.mock_prompt_response)
mock_time.return_value = 1000.0
posthog = self.create_mock_posthog()
prompts = Prompts(posthog)
client = self.create_mock_client()
prompts = Prompts(client)
# First call
prompts.get("test-prompt")
@@ -295,8 +389,8 @@ class TestPromptsGet(TestPrompts):
prompts.get("test-prompt")
self.assertEqual(mock_get.call_count, 2)
@patch("posthog.ai.prompts._get_session")
@patch("posthog.ai.prompts.time.time")
@patch("hanzo_insights.ai.prompts._get_session")
@patch("hanzo_insights.ai.prompts.time.time")
def test_use_custom_default_cache_ttl_from_constructor(
self, mock_time, mock_get_session
):
@@ -305,8 +399,8 @@ class TestPromptsGet(TestPrompts):
mock_get.return_value = MockResponse(json_data=self.mock_prompt_response)
mock_time.return_value = 1000.0
posthog = self.create_mock_posthog()
prompts = Prompts(posthog, default_cache_ttl_seconds=60)
client = self.create_mock_client()
prompts = Prompts(client, default_cache_ttl_seconds=60)
# First call
prompts.get("test-prompt")
@@ -319,30 +413,32 @@ class TestPromptsGet(TestPrompts):
prompts.get("test-prompt")
self.assertEqual(mock_get.call_count, 2)
@patch("posthog.ai.prompts._get_session")
@patch("hanzo_insights.ai.prompts._get_session")
def test_url_encode_prompt_names_with_special_characters(self, mock_get_session):
"""Should URL-encode prompt names with special characters."""
mock_get = mock_get_session.return_value.get
mock_get.return_value = MockResponse(json_data=self.mock_prompt_response)
posthog = self.create_mock_posthog()
prompts = Prompts(posthog)
client = self.create_mock_client()
prompts = Prompts(client)
prompts.get("prompt with spaces/and/slashes")
call_args = mock_get.call_args
self.assertEqual(
call_args[0][0],
"https://us.posthog.com/api/environments/@current/llm_prompts/name/prompt%20with%20spaces%2Fand%2Fslashes/",
"https://us.insights.hanzo.ai/api/environments/@current/llm_prompts/name/prompt%20with%20spaces%2Fand%2Fslashes/?token=phc_test_key",
)
@patch("posthog.ai.prompts._get_session")
def test_work_with_direct_options_no_posthog_client(self, mock_get_session):
"""Should work with direct options (no PostHog client)."""
@patch("hanzo_insights.ai.prompts._get_session")
def test_work_with_direct_options_no_insights_client(self, mock_get_session):
"""Should work with direct options (no Insights client)."""
mock_get = mock_get_session.return_value.get
mock_get.return_value = MockResponse(json_data=self.mock_prompt_response)
prompts = Prompts(personal_api_key="phx_direct_key")
prompts = Prompts(
personal_api_key="phx_direct_key", project_api_key="phc_direct_key"
)
result = prompts.get("test-prompt")
@@ -350,20 +446,22 @@ class TestPromptsGet(TestPrompts):
call_args = mock_get.call_args
self.assertEqual(
call_args[0][0],
"https://us.posthog.com/api/environments/@current/llm_prompts/name/test-prompt/",
"https://us.insights.hanzo.ai/api/environments/@current/llm_prompts/name/test-prompt/?token=phc_direct_key",
)
self.assertEqual(
call_args[1]["headers"]["Authorization"], "Bearer phx_direct_key"
)
@patch("posthog.ai.prompts._get_session")
@patch("hanzo_insights.ai.prompts._get_session")
def test_use_custom_host_from_direct_options(self, mock_get_session):
"""Should use custom host from direct options."""
mock_get = mock_get_session.return_value.get
mock_get.return_value = MockResponse(json_data=self.mock_prompt_response)
prompts = Prompts(
personal_api_key="phx_direct_key", host="https://eu.posthog.com"
personal_api_key="phx_direct_key",
project_api_key="phc_direct_key",
host="https://eu.insights.hanzo.ai",
)
prompts.get("test-prompt")
@@ -371,11 +469,11 @@ class TestPromptsGet(TestPrompts):
call_args = mock_get.call_args
self.assertEqual(
call_args[0][0],
"https://eu.posthog.com/api/environments/@current/llm_prompts/name/test-prompt/",
"https://eu.insights.hanzo.ai/api/environments/@current/llm_prompts/name/test-prompt/?token=phc_direct_key",
)
@patch("posthog.ai.prompts._get_session")
@patch("posthog.ai.prompts.time.time")
@patch("hanzo_insights.ai.prompts._get_session")
@patch("hanzo_insights.ai.prompts.time.time")
def test_use_custom_default_cache_ttl_from_direct_options(
self, mock_time, mock_get_session
):
@@ -385,7 +483,9 @@ class TestPromptsGet(TestPrompts):
mock_time.return_value = 1000.0
prompts = Prompts(
personal_api_key="phx_direct_key", default_cache_ttl_seconds=60
personal_api_key="phx_direct_key",
project_api_key="phc_direct_key",
default_cache_ttl_seconds=60,
)
# First call
@@ -405,8 +505,8 @@ class TestPromptsCompile(TestPrompts):
def test_replace_a_single_variable(self):
"""Should replace a single variable."""
posthog = self.create_mock_posthog()
prompts = Prompts(posthog)
client = self.create_mock_client()
prompts = Prompts(client)
result = prompts.compile("Hello, {{name}}!", {"name": "World"})
@@ -414,8 +514,8 @@ class TestPromptsCompile(TestPrompts):
def test_replace_multiple_variables(self):
"""Should replace multiple variables."""
posthog = self.create_mock_posthog()
prompts = Prompts(posthog)
client = self.create_mock_client()
prompts = Prompts(client)
result = prompts.compile(
"Hello, {{name}}! Welcome to {{company}}. Your tier is {{tier}}.",
@@ -428,8 +528,8 @@ class TestPromptsCompile(TestPrompts):
def test_handle_numbers(self):
"""Should handle numbers."""
posthog = self.create_mock_posthog()
prompts = Prompts(posthog)
client = self.create_mock_client()
prompts = Prompts(client)
result = prompts.compile("You have {{count}} items.", {"count": 42})
@@ -437,8 +537,8 @@ class TestPromptsCompile(TestPrompts):
def test_handle_booleans(self):
"""Should handle booleans."""
posthog = self.create_mock_posthog()
prompts = Prompts(posthog)
client = self.create_mock_client()
prompts = Prompts(client)
result = prompts.compile("Feature enabled: {{enabled}}", {"enabled": True})
@@ -446,8 +546,8 @@ class TestPromptsCompile(TestPrompts):
def test_leave_unmatched_variables_unchanged(self):
"""Should leave unmatched variables unchanged."""
posthog = self.create_mock_posthog()
prompts = Prompts(posthog)
client = self.create_mock_client()
prompts = Prompts(client)
result = prompts.compile(
"Hello, {{name}}! Your {{unknown}} is ready.", {"name": "World"}
@@ -457,8 +557,8 @@ class TestPromptsCompile(TestPrompts):
def test_handle_prompts_with_no_variables(self):
"""Should handle prompts with no variables."""
posthog = self.create_mock_posthog()
prompts = Prompts(posthog)
client = self.create_mock_client()
prompts = Prompts(client)
result = prompts.compile("You are a helpful assistant.", {})
@@ -466,8 +566,8 @@ class TestPromptsCompile(TestPrompts):
def test_handle_empty_variables_dict(self):
"""Should handle empty variables dict."""
posthog = self.create_mock_posthog()
prompts = Prompts(posthog)
client = self.create_mock_client()
prompts = Prompts(client)
result = prompts.compile("Hello, {{name}}!", {})
@@ -475,8 +575,8 @@ class TestPromptsCompile(TestPrompts):
def test_handle_multiple_occurrences_of_same_variable(self):
"""Should handle multiple occurrences of the same variable."""
posthog = self.create_mock_posthog()
prompts = Prompts(posthog)
client = self.create_mock_client()
prompts = Prompts(client)
result = prompts.compile(
"Hello, {{name}}! Goodbye, {{name}}!", {"name": "World"}
@@ -486,7 +586,9 @@ class TestPromptsCompile(TestPrompts):
def test_work_with_direct_options_initialization(self):
"""Should work with direct options initialization."""
prompts = Prompts(personal_api_key="phx_test_key")
prompts = Prompts(
personal_api_key="phx_test_key", project_api_key="phc_test_key"
)
result = prompts.compile("Hello, {{name}}!", {"name": "World"})
@@ -494,7 +596,9 @@ class TestPromptsCompile(TestPrompts):
def test_handle_variables_with_hyphens(self):
"""Should handle variables with hyphens."""
prompts = Prompts(personal_api_key="phx_test_key")
prompts = Prompts(
personal_api_key="phx_test_key", project_api_key="phc_test_key"
)
result = prompts.compile("User ID: {{user-id}}", {"user-id": "12345"})
@@ -502,7 +606,9 @@ class TestPromptsCompile(TestPrompts):
def test_handle_variables_with_dots(self):
"""Should handle variables with dots."""
prompts = Prompts(personal_api_key="phx_test_key")
prompts = Prompts(
personal_api_key="phx_test_key", project_api_key="phc_test_key"
)
result = prompts.compile("Company: {{company.name}}", {"company.name": "Acme"})
@@ -512,7 +618,17 @@ class TestPromptsCompile(TestPrompts):
class TestPromptsClearCache(TestPrompts):
"""Tests for the Prompts.clear_cache() method."""
@patch("posthog.ai.prompts._get_session")
def test_clear_cache_with_version_and_no_name_raises_value_error(self):
"""Should enforce that versioned cache clearing requires a prompt name."""
client = self.create_mock_client()
prompts = Prompts(client)
with self.assertRaises(ValueError) as context:
prompts.clear_cache(version=1)
self.assertIn("requires 'name'", str(context.exception))
@patch("hanzo_insights.ai.prompts._get_session")
def test_clear_a_specific_prompt_from_cache(self, mock_get_session):
"""Should clear a specific prompt from cache."""
mock_get = mock_get_session.return_value.get
@@ -524,8 +640,8 @@ class TestPromptsClearCache(TestPrompts):
MockResponse(json_data=self.mock_prompt_response),
]
posthog = self.create_mock_posthog()
prompts = Prompts(posthog)
client = self.create_mock_client()
prompts = Prompts(client)
# Populate cache with two prompts
prompts.get("test-prompt")
@@ -543,7 +659,78 @@ class TestPromptsClearCache(TestPrompts):
prompts.get("other-prompt")
self.assertEqual(mock_get.call_count, 3)
@patch("posthog.ai.prompts._get_session")
@patch("hanzo_insights.ai.prompts._get_session")
def test_clear_a_specific_prompt_version_from_cache(self, mock_get_session):
"""Should clear only the requested prompt version from cache."""
mock_get = mock_get_session.return_value.get
latest_prompt_response = {
**self.mock_prompt_response,
"prompt": "Latest prompt",
"version": 2,
}
versioned_prompt_response = {
**self.mock_prompt_response,
"prompt": "Prompt version 1",
"version": 1,
}
mock_get.side_effect = [
MockResponse(json_data=latest_prompt_response),
MockResponse(json_data=versioned_prompt_response),
MockResponse(json_data=versioned_prompt_response),
]
client = self.create_mock_client()
prompts = Prompts(client)
prompts.get("test-prompt")
prompts.get("test-prompt", version=1)
self.assertEqual(mock_get.call_count, 2)
prompts.clear_cache("test-prompt", version=1)
prompts.get("test-prompt")
self.assertEqual(mock_get.call_count, 2)
prompts.get("test-prompt", version=1)
self.assertEqual(mock_get.call_count, 3)
@patch("hanzo_insights.ai.prompts._get_session")
def test_clear_a_prompt_name_clears_all_cached_versions(self, mock_get_session):
"""Should clear latest and versioned cache entries for the same prompt name."""
mock_get = mock_get_session.return_value.get
latest_prompt_response = {
**self.mock_prompt_response,
"prompt": "Latest prompt",
"version": 2,
}
versioned_prompt_response = {
**self.mock_prompt_response,
"prompt": "Prompt version 1",
"version": 1,
}
mock_get.side_effect = [
MockResponse(json_data=latest_prompt_response),
MockResponse(json_data=versioned_prompt_response),
MockResponse(json_data=latest_prompt_response),
MockResponse(json_data=versioned_prompt_response),
]
client = self.create_mock_client()
prompts = Prompts(client)
prompts.get("test-prompt")
prompts.get("test-prompt", version=1)
self.assertEqual(mock_get.call_count, 2)
prompts.clear_cache("test-prompt")
prompts.get("test-prompt")
prompts.get("test-prompt", version=1)
self.assertEqual(mock_get.call_count, 4)
@patch("hanzo_insights.ai.prompts._get_session")
def test_clear_all_prompts_from_cache(self, mock_get_session):
"""Should clear all prompts from cache when no name is provided."""
mock_get = mock_get_session.return_value.get
@@ -556,8 +743,8 @@ class TestPromptsClearCache(TestPrompts):
MockResponse(json_data=other_prompt_response),
]
posthog = self.create_mock_posthog()
prompts = Prompts(posthog)
client = self.create_mock_client()
prompts = Prompts(client)
# Populate cache with two prompts
prompts.get("test-prompt")
@@ -1,7 +1,7 @@
import os
import unittest
from posthog.ai.sanitization import (
from hanzo_insights.ai.sanitization import (
redact_base64_data_url,
sanitize_openai,
sanitize_openai_response,
@@ -69,6 +69,25 @@ class TestSanitization(unittest.TestCase):
)
self.assertEqual(result[0]["content"][1]["image_url"]["detail"], "high")
def test_sanitize_openai_input_image(self):
input_data = [
{
"role": "user",
"content": [
{
"type": "input_image",
"image_url": self.sample_base64_image,
}
],
}
]
result = sanitize_openai(input_data)
self.assertEqual(
result[0]["content"][0]["image_url"], REDACTED_IMAGE_PLACEHOLDER
)
def test_sanitize_openai_preserves_regular_urls(self):
input_data = [
{
@@ -13,8 +13,8 @@ import time
import unittest
from unittest.mock import MagicMock, patch
from posthog.client import Client
from posthog.test.test_utils import FAKE_TEST_API_KEY
from hanzo_insights.client import Client
from hanzo_insights.test.test_utils import FAKE_TEST_API_KEY
class TestSystemPromptCapture(unittest.TestCase):
@@ -26,7 +26,7 @@ class TestSystemPromptCapture(unittest.TestCase):
self.test_user_message = "Hello, how are you?"
self.test_response = "I'm doing well, thank you!"
# Create mock PostHog client
# Create mock Insights client
self.client = Client(FAKE_TEST_API_KEY)
self.client._enqueue = MagicMock()
self.client.privacy_mode = False
@@ -61,7 +61,7 @@ class TestSystemPromptCapture(unittest.TestCase):
from openai.types.chat.chat_completion import Choice
from openai.types.completion_usage import CompletionUsage
from posthog.ai.openai import OpenAI
from hanzo_insights.ai.openai import OpenAI
except ImportError:
self.skipTest("OpenAI package not available")
@@ -88,7 +88,7 @@ class TestSystemPromptCapture(unittest.TestCase):
"openai.resources.chat.completions.Completions.create",
return_value=mock_response,
):
client = OpenAI(posthog_client=self.client, api_key="test")
client = OpenAI(insights_client=self.client, api_key="test")
messages = [
{"role": "system", "content": self.test_system_prompt},
@@ -96,7 +96,7 @@ class TestSystemPromptCapture(unittest.TestCase):
]
client.chat.completions.create(
model="gpt-4", messages=messages, posthog_distinct_id="test-user"
model="gpt-4", messages=messages, insights_distinct_id="test-user"
)
self.assertEqual(len(self.client._enqueue.call_args_list), 1)
@@ -110,7 +110,7 @@ class TestSystemPromptCapture(unittest.TestCase):
from openai.types.chat.chat_completion import Choice
from openai.types.completion_usage import CompletionUsage
from posthog.ai.openai import OpenAI
from hanzo_insights.ai.openai import OpenAI
except ImportError:
self.skipTest("OpenAI package not available")
@@ -137,7 +137,7 @@ class TestSystemPromptCapture(unittest.TestCase):
"openai.resources.chat.completions.Completions.create",
return_value=mock_response,
):
client = OpenAI(posthog_client=self.client, api_key="test")
client = OpenAI(insights_client=self.client, api_key="test")
messages = [{"role": "user", "content": self.test_user_message}]
@@ -145,7 +145,7 @@ class TestSystemPromptCapture(unittest.TestCase):
model="gpt-4",
messages=messages,
system=self.test_system_prompt,
posthog_distinct_id="test-user",
insights_distinct_id="test-user",
)
self.assertEqual(len(self.client._enqueue.call_args_list), 1)
@@ -162,7 +162,7 @@ class TestSystemPromptCapture(unittest.TestCase):
from openai.types.chat.chat_completion_chunk import Choice as ChoiceChunk
from openai.types.completion_usage import CompletionUsage
from posthog.ai.openai import OpenAI
from hanzo_insights.ai.openai import OpenAI
except ImportError:
self.skipTest("OpenAI package not available")
@@ -201,7 +201,7 @@ class TestSystemPromptCapture(unittest.TestCase):
"openai.resources.chat.completions.Completions.create",
return_value=[chunk1, chunk2],
):
client = OpenAI(posthog_client=self.client, api_key="test")
client = OpenAI(insights_client=self.client, api_key="test")
messages = [{"role": "user", "content": self.test_user_message}]
@@ -210,7 +210,7 @@ class TestSystemPromptCapture(unittest.TestCase):
messages=messages,
system=self.test_system_prompt,
stream=True,
posthog_distinct_id="test-user",
insights_distinct_id="test-user",
)
list(response_generator) # Consume generator
@@ -223,7 +223,7 @@ class TestSystemPromptCapture(unittest.TestCase):
def test_anthropic_messages_array_system_prompt(self):
"""Test Anthropic with system prompt in messages array."""
try:
from posthog.ai.anthropic import Anthropic
from hanzo_insights.ai.anthropic import Anthropic
except ImportError:
self.skipTest("Anthropic package not available")
@@ -235,7 +235,7 @@ class TestSystemPromptCapture(unittest.TestCase):
mock_response.usage.cache_creation_input_tokens = None
mock_create.return_value = mock_response
client = Anthropic(posthog_client=self.client, api_key="test")
client = Anthropic(insights_client=self.client, api_key="test")
messages = [
{"role": "system", "content": self.test_system_prompt},
@@ -245,7 +245,7 @@ class TestSystemPromptCapture(unittest.TestCase):
client.messages.create(
model="claude-3-5-sonnet-20241022",
messages=messages,
posthog_distinct_id="test-user",
insights_distinct_id="test-user",
)
self.assertEqual(len(self.client._enqueue.call_args_list), 1)
@@ -255,7 +255,7 @@ class TestSystemPromptCapture(unittest.TestCase):
def test_anthropic_separate_system_parameter(self):
"""Test Anthropic with system prompt as separate parameter."""
try:
from posthog.ai.anthropic import Anthropic
from hanzo_insights.ai.anthropic import Anthropic
except ImportError:
self.skipTest("Anthropic package not available")
@@ -267,7 +267,7 @@ class TestSystemPromptCapture(unittest.TestCase):
mock_response.usage.cache_creation_input_tokens = None
mock_create.return_value = mock_response
client = Anthropic(posthog_client=self.client, api_key="test")
client = Anthropic(insights_client=self.client, api_key="test")
messages = [{"role": "user", "content": self.test_user_message}]
@@ -275,7 +275,7 @@ class TestSystemPromptCapture(unittest.TestCase):
model="claude-3-5-sonnet-20241022",
messages=messages,
system=self.test_system_prompt,
posthog_distinct_id="test-user",
insights_distinct_id="test-user",
)
self.assertEqual(len(self.client._enqueue.call_args_list), 1)
@@ -286,7 +286,7 @@ class TestSystemPromptCapture(unittest.TestCase):
def test_gemini_contents_array_system_prompt(self):
"""Test Gemini with system prompt in contents array."""
try:
from posthog.ai.gemini import Client
from hanzo_insights.ai.gemini import Client
except ImportError:
self.skipTest("Gemini package not available")
@@ -306,7 +306,7 @@ class TestSystemPromptCapture(unittest.TestCase):
mock_client_instance.models = mock_models_instance
mock_genai_class.return_value = mock_client_instance
client = Client(posthog_client=self.client, api_key="test")
client = Client(insights_client=self.client, api_key="test")
contents = [
{"role": "system", "content": self.test_system_prompt},
@@ -316,7 +316,7 @@ class TestSystemPromptCapture(unittest.TestCase):
client.models.generate_content(
model="gemini-2.0-flash",
contents=contents,
posthog_distinct_id="test-user",
insights_distinct_id="test-user",
)
self.assertEqual(len(self.client._enqueue.call_args_list), 1)
@@ -326,7 +326,7 @@ class TestSystemPromptCapture(unittest.TestCase):
def test_gemini_system_instruction_parameter(self):
"""Test Gemini with system_instruction in config parameter."""
try:
from posthog.ai.gemini import Client
from hanzo_insights.ai.gemini import Client
except ImportError:
self.skipTest("Gemini package not available")
@@ -346,7 +346,7 @@ class TestSystemPromptCapture(unittest.TestCase):
mock_client_instance.models = mock_models_instance
mock_genai_class.return_value = mock_client_instance
client = Client(posthog_client=self.client, api_key="test")
client = Client(insights_client=self.client, api_key="test")
contents = [{"role": "user", "content": self.test_user_message}]
config = {"system_instruction": self.test_system_prompt}
@@ -355,7 +355,7 @@ class TestSystemPromptCapture(unittest.TestCase):
model="gemini-2.0-flash",
contents=contents,
config=config,
posthog_distinct_id="test-user",
insights_distinct_id="test-user",
)
self.assertEqual(len(self.client._enqueue.call_args_list), 1)
@@ -0,0 +1,62 @@
from parameterized import parameterized
from hanzo_insights.ai.utils import _get_tokens_source
@parameterized.expand(
[
("no_insights_properties", {"$ai_input_tokens": 100}, None, "sdk"),
("empty_insights_properties", {"$ai_input_tokens": 100}, {}, "sdk"),
(
"unrelated_insights_properties",
{"$ai_input_tokens": 100},
{"foo": "bar"},
"sdk",
),
(
"override_input_tokens",
{"$ai_input_tokens": 100},
{"$ai_input_tokens": 999},
"passthrough",
),
(
"override_output_tokens",
{"$ai_output_tokens": 50},
{"$ai_output_tokens": 999},
"passthrough",
),
(
"override_total_tokens",
{"$ai_input_tokens": 100},
{"$ai_total_tokens": 999},
"passthrough",
),
(
"override_cache_read",
{"$ai_input_tokens": 100},
{"$ai_cache_read_input_tokens": 500},
"passthrough",
),
(
"override_cache_creation",
{"$ai_input_tokens": 100},
{"$ai_cache_creation_input_tokens": 200},
"passthrough",
),
(
"override_reasoning_tokens",
{"$ai_input_tokens": 100},
{"$ai_reasoning_tokens": 300},
"passthrough",
),
(
"mixed_override_and_custom",
{"$ai_input_tokens": 100},
{"$ai_input_tokens": 999, "custom_key": "value"},
"passthrough",
),
]
)
def test_get_tokens_source(name, sdk_tags, insights_properties, expected):
result = _get_tokens_source(sdk_tags, insights_properties)
assert result == expected
@@ -1,4 +1,4 @@
from posthog.contexts import (
from hanzo_insights.contexts import (
new_context,
get_context_session_id,
get_context_distinct_id,
@@ -20,7 +20,7 @@ if not settings.configured:
)
django.setup()
from posthog.integrations.django import PosthogContextMiddleware
from hanzo_insights.integrations.django import InsightsContextMiddleware
class MockRequest:
@@ -45,7 +45,7 @@ class MockRequest:
return f"{scheme}://{self._host}{self.path}"
class TestPosthogContextMiddleware(unittest.TestCase):
class TestInsightsContextMiddleware(unittest.TestCase):
def create_middleware(
self,
extra_tags=None,
@@ -60,24 +60,24 @@ class TestPosthogContextMiddleware(unittest.TestCase):
with patch("django.conf.settings") as mock_settings:
# Configure mock settings
mock_settings.POSTHOG_MW_EXTRA_TAGS = extra_tags
mock_settings.POSTHOG_MW_REQUEST_FILTER = request_filter
mock_settings.POSTHOG_MW_TAG_MAP = tag_map
mock_settings.POSTHOG_MW_CAPTURE_EXCEPTIONS = capture_exceptions
mock_settings.POSTHOG_MW_CLIENT = None
mock_settings.INSIGHTS_MW_EXTRA_TAGS = extra_tags
mock_settings.INSIGHTS_MW_REQUEST_FILTER = request_filter
mock_settings.INSIGHTS_MW_TAG_MAP = tag_map
mock_settings.INSIGHTS_MW_CAPTURE_EXCEPTIONS = capture_exceptions
mock_settings.INSIGHTS_MW_CLIENT = None
# Make hasattr work correctly
def mock_hasattr(obj, name):
return name in [
"POSTHOG_MW_EXTRA_TAGS",
"POSTHOG_MW_REQUEST_FILTER",
"POSTHOG_MW_TAG_MAP",
"POSTHOG_MW_CAPTURE_EXCEPTIONS",
"POSTHOG_MW_CLIENT",
"INSIGHTS_MW_EXTRA_TAGS",
"INSIGHTS_MW_REQUEST_FILTER",
"INSIGHTS_MW_TAG_MAP",
"INSIGHTS_MW_CAPTURE_EXCEPTIONS",
"INSIGHTS_MW_CLIENT",
]
with patch("builtins.hasattr", side_effect=mock_hasattr):
middleware = PosthogContextMiddleware(get_response)
middleware = InsightsContextMiddleware(get_response)
return middleware
@@ -87,8 +87,8 @@ class TestPosthogContextMiddleware(unittest.TestCase):
middleware = self.create_middleware()
request = MockRequest(
headers={
"X-POSTHOG-SESSION-ID": "session-123",
"X-POSTHOG-DISTINCT-ID": "user-456",
"X-INSIGHTS-SESSION-ID": "session-123",
"X-INSIGHTS-DISTINCT-ID": "user-456",
},
method="POST",
path="/api/test",
@@ -104,7 +104,7 @@ class TestPosthogContextMiddleware(unittest.TestCase):
self.assertEqual(tags["$request_method"], "POST")
def test_extract_tags_missing_headers(self):
"""Test tag extraction when PostHog headers are missing"""
"""Test tag extraction when Insights headers are missing"""
with new_context():
middleware = self.create_middleware()
@@ -118,12 +118,12 @@ class TestPosthogContextMiddleware(unittest.TestCase):
self.assertEqual(tags["$request_method"], "GET")
def test_extract_tags_partial_headers(self):
"""Test tag extraction with only some PostHog headers present"""
"""Test tag extraction with only some Insights headers present"""
with new_context():
middleware = self.create_middleware()
request = MockRequest(
headers={"X-POSTHOG-SESSION-ID": "session-only"}, method="PUT"
headers={"X-INSIGHTS-SESSION-ID": "session-only"}, method="PUT"
)
tags = middleware.extract_tags(request)
@@ -141,7 +141,7 @@ class TestPosthogContextMiddleware(unittest.TestCase):
with new_context():
middleware = self.create_middleware(extra_tags=extra_tags_func)
request = MockRequest(
headers={"X-POSTHOG-SESSION-ID": "session-123"}, method="GET"
headers={"X-INSIGHTS-SESSION-ID": "session-123"}, method="GET"
)
tags = middleware.extract_tags(request)
@@ -167,7 +167,7 @@ class TestPosthogContextMiddleware(unittest.TestCase):
tag_map=tag_map_func, extra_tags=extra_tags_func
)
request = MockRequest(
headers={"X-POSTHOG-SESSION-ID": "session-123"}, method="GET"
headers={"X-INSIGHTS-SESSION-ID": "session-123"}, method="GET"
)
tags = middleware.extract_tags(request)
@@ -230,7 +230,7 @@ class TestPosthogContextMiddleware(unittest.TestCase):
middleware.client = mock_client
request = MockRequest(
headers={"X-POSTHOG-DISTINCT-ID": "test-user"},
headers={"X-INSIGHTS-DISTINCT-ID": "test-user"},
method="POST",
path="/api/endpoint",
)
@@ -282,7 +282,7 @@ class TestPosthogContextMiddleware(unittest.TestCase):
mock_client.capture_exception.assert_not_called()
class TestPosthogContextMiddlewareSync(unittest.TestCase):
class TestInsightsContextMiddlewareSync(unittest.TestCase):
"""Test synchronous middleware behavior"""
def test_sync_middleware_call(self):
@@ -291,13 +291,13 @@ class TestPosthogContextMiddlewareSync(unittest.TestCase):
get_response = Mock(return_value=mock_response)
# Create middleware with sync get_response
middleware = PosthogContextMiddleware(get_response)
middleware = InsightsContextMiddleware(get_response)
# Verify sync mode detected
self.assertFalse(middleware._is_coroutine)
request = MockRequest(
headers={"X-POSTHOG-SESSION-ID": "test-session"},
headers={"X-INSIGHTS-SESSION-ID": "test-session"},
method="GET",
path="/test",
)
@@ -318,7 +318,7 @@ class TestPosthogContextMiddlewareSync(unittest.TestCase):
def request_filter(req):
return False
middleware = PosthogContextMiddleware.__new__(PosthogContextMiddleware)
middleware = InsightsContextMiddleware.__new__(InsightsContextMiddleware)
middleware.get_response = get_response
middleware._is_coroutine = False
middleware.request_filter = request_filter
@@ -351,7 +351,7 @@ class TestPosthogContextMiddlewareSync(unittest.TestCase):
mock_client = Mock()
get_response = Mock(return_value=Mock(status_code=500))
middleware = PosthogContextMiddleware(get_response)
middleware = InsightsContextMiddleware(get_response)
middleware.client = mock_client
def get_response_simulating_django(request):
@@ -380,7 +380,7 @@ class TestPosthogContextMiddlewareSync(unittest.TestCase):
)
class TestPosthogContextMiddlewareAsync(unittest.TestCase):
class TestInsightsContextMiddlewareAsync(unittest.TestCase):
"""Test asynchronous middleware behavior"""
def test_async_middleware_detection(self):
@@ -389,7 +389,7 @@ class TestPosthogContextMiddlewareAsync(unittest.TestCase):
async def async_get_response(request):
return Mock()
middleware = PosthogContextMiddleware(async_get_response)
middleware = InsightsContextMiddleware(async_get_response)
# Verify async mode detected
self.assertTrue(middleware._is_coroutine)
@@ -403,10 +403,10 @@ class TestPosthogContextMiddlewareAsync(unittest.TestCase):
async def async_get_response(request):
return mock_response
middleware = PosthogContextMiddleware(async_get_response)
middleware = InsightsContextMiddleware(async_get_response)
request = MockRequest(
headers={"X-POSTHOG-SESSION-ID": "async-session"},
headers={"X-INSIGHTS-SESSION-ID": "async-session"},
method="POST",
path="/async-test",
)
@@ -434,7 +434,7 @@ class TestPosthogContextMiddlewareAsync(unittest.TestCase):
return mock_response
# Properly initialize middleware
middleware = PosthogContextMiddleware(async_get_response)
middleware = InsightsContextMiddleware(async_get_response)
# Override request filter after initialization
middleware.request_filter = lambda req: False
@@ -459,10 +459,10 @@ class TestPosthogContextMiddlewareAsync(unittest.TestCase):
self.assertEqual(session_id, "async-session-123")
return mock_response
middleware = PosthogContextMiddleware(async_get_response)
middleware = InsightsContextMiddleware(async_get_response)
request = MockRequest(
headers={"X-POSTHOG-SESSION-ID": "async-session-123"},
headers={"X-INSIGHTS-SESSION-ID": "async-session-123"},
method="GET",
)
@@ -483,7 +483,7 @@ class TestPosthogContextMiddlewareAsync(unittest.TestCase):
raise ValueError("Async test exception")
# Properly initialize middleware
middleware = PosthogContextMiddleware(raise_exception)
middleware = InsightsContextMiddleware(raise_exception)
middleware.client = mock_client # Override with mock client
request = MockRequest()
@@ -525,11 +525,11 @@ class TestPosthogContextMiddlewareAsync(unittest.TestCase):
self.assertEqual(distinct_id, "123")
return mock_response
middleware = PosthogContextMiddleware(async_get_response)
middleware = InsightsContextMiddleware(async_get_response)
middleware.client = Mock()
request = MockRequest(
headers={"X-POSTHOG-SESSION-ID": "test-session"}, method="GET"
headers={"X-INSIGHTS-SESSION-ID": "test-session"}, method="GET"
)
# Mock auser() to return authenticated user
@@ -561,11 +561,11 @@ class TestPosthogContextMiddlewareAsync(unittest.TestCase):
self.assertIsNone(distinct_id)
return mock_response
middleware = PosthogContextMiddleware(async_get_response)
middleware = InsightsContextMiddleware(async_get_response)
middleware.client = Mock()
request = MockRequest(
headers={"X-POSTHOG-SESSION-ID": "test-session"}, method="GET"
headers={"X-INSIGHTS-SESSION-ID": "test-session"}, method="GET"
)
async def mock_auser():
@@ -591,12 +591,12 @@ class TestPosthogContextMiddlewareAsync(unittest.TestCase):
async def async_get_response(request):
return mock_response
middleware = PosthogContextMiddleware(async_get_response)
middleware = InsightsContextMiddleware(async_get_response)
middleware.client = Mock()
# Request without auser method (no auth middleware)
request = MockRequest(
headers={"X-POSTHOG-SESSION-ID": "test-session"}, method="GET"
headers={"X-INSIGHTS-SESSION-ID": "test-session"}, method="GET"
)
with new_context():
@@ -621,12 +621,12 @@ class TestPosthogContextMiddlewareAsync(unittest.TestCase):
async def async_get_response(request):
return mock_response
middleware = PosthogContextMiddleware(async_get_response)
middleware = InsightsContextMiddleware(async_get_response)
middleware.extra_tags = extra_tags_callback
middleware.client = Mock()
request = MockRequest(
headers={"X-POSTHOG-SESSION-ID": "test-session"}, method="GET"
headers={"X-INSIGHTS-SESSION-ID": "test-session"}, method="GET"
)
# Mock auser for no user
@@ -658,12 +658,12 @@ class TestPosthogContextMiddlewareAsync(unittest.TestCase):
async def async_get_response(request):
return mock_response
middleware = PosthogContextMiddleware(async_get_response)
middleware = InsightsContextMiddleware(async_get_response)
middleware.tag_map = tag_map_callback
middleware.client = Mock()
request = MockRequest(
headers={"X-POSTHOG-SESSION-ID": "test-session"}, method="GET"
headers={"X-INSIGHTS-SESSION-ID": "test-session"}, method="GET"
)
# Mock auser for no user
@@ -699,12 +699,12 @@ class TestPosthogContextMiddlewareAsync(unittest.TestCase):
self.assertEqual(session_id, "async-sess-123")
return mock_response
middleware = PosthogContextMiddleware(async_get_response)
middleware = InsightsContextMiddleware(async_get_response)
middleware.client = Mock()
request = MockRequest(
headers={
"X-POSTHOG-SESSION-ID": "async-sess-123",
"X-INSIGHTS-SESSION-ID": "async-sess-123",
"X-Forwarded-For": "192.168.1.1",
"User-Agent": "TestAgent/1.0",
},
@@ -725,13 +725,13 @@ class TestPosthogContextMiddlewareAsync(unittest.TestCase):
asyncio.run(run_test())
class TestPosthogContextMiddlewareHybrid(unittest.TestCase):
class TestInsightsContextMiddlewareHybrid(unittest.TestCase):
"""Test hybrid middleware behavior with mixed sync/async chains"""
def test_hybrid_flags_set(self):
"""Test that both capability flags are set"""
self.assertTrue(PosthogContextMiddleware.sync_capable)
self.assertTrue(PosthogContextMiddleware.async_capable)
self.assertTrue(InsightsContextMiddleware.sync_capable)
self.assertTrue(InsightsContextMiddleware.async_capable)
def test_sync_to_async_routing(self):
"""Test that __call__ routes to __acall__ when async"""
@@ -740,7 +740,7 @@ class TestPosthogContextMiddlewareHybrid(unittest.TestCase):
async def async_get_response(request):
return Mock()
middleware = PosthogContextMiddleware(async_get_response)
middleware = InsightsContextMiddleware(async_get_response)
# Verify routing happens
request = MockRequest()
@@ -759,7 +759,7 @@ class TestPosthogContextMiddlewareHybrid(unittest.TestCase):
def sync_get_response(request):
return mock_response
middleware = PosthogContextMiddleware(sync_get_response)
middleware = InsightsContextMiddleware(sync_get_response)
request = MockRequest()
result = middleware(request)
@@ -2,16 +2,16 @@ import unittest
import mock
from posthog.client import Client
from posthog.test.test_utils import FAKE_TEST_API_KEY
from hanzo_insights.client import Client
from hanzo_insights.test.test_utils import FAKE_TEST_API_KEY
class TestClient(unittest.TestCase):
@classmethod
def setUpClass(cls):
# This ensures no real HTTP POST requests are made
cls.client_post_patcher = mock.patch("posthog.client.batch_post")
cls.consumer_post_patcher = mock.patch("posthog.consumer.batch_post")
cls.client_post_patcher = mock.patch("hanzo_insights.client.batch_post")
cls.consumer_post_patcher = mock.patch("hanzo_insights.consumer.batch_post")
cls.client_post_patcher.start()
cls.consumer_post_patcher.start()
@@ -40,7 +40,7 @@ class TestClient(unittest.TestCase):
event["properties"]["processed_by_before_send"] = True
return event
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -73,7 +73,7 @@ class TestClient(unittest.TestCase):
return None
return event
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -101,7 +101,7 @@ class TestClient(unittest.TestCase):
def buggy_before_send(event):
raise ValueError("Oops!")
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -128,7 +128,7 @@ class TestClient(unittest.TestCase):
event["properties"]["marked"] = True
return event
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -153,7 +153,7 @@ class TestClient(unittest.TestCase):
def test_before_send_callback_disabled_when_none(self):
"""Test that client works normally when before_send is None."""
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -189,7 +189,7 @@ class TestClient(unittest.TestCase):
return event
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -7,21 +7,21 @@ import mock
import six
from parameterized import parameterized
from posthog.client import Client
from posthog.contexts import get_context_session_id, new_context, set_context_session
from posthog.request import APIError, GetResponse
from posthog.test.test_utils import FAKE_TEST_API_KEY
from posthog.types import FeatureFlag, LegacyFlagMetadata
from posthog.version import VERSION
from posthog.contexts import tag
from hanzo_insights.client import Client
from hanzo_insights.contexts import get_context_session_id, new_context, set_context_session
from hanzo_insights.request import APIError, GetResponse
from hanzo_insights.test.test_utils import FAKE_TEST_API_KEY
from hanzo_insights.types import FeatureFlag, LegacyFlagMetadata
from hanzo_insights.version import VERSION
from hanzo_insights.contexts import tag
class TestClient(unittest.TestCase):
@classmethod
def setUpClass(cls):
# This ensures no real HTTP POST requests are made
cls.client_post_patcher = mock.patch("posthog.client.batch_post")
cls.consumer_post_patcher = mock.patch("posthog.consumer.batch_post")
cls.client_post_patcher = mock.patch("hanzo_insights.client.batch_post")
cls.consumer_post_patcher = mock.patch("hanzo_insights.consumer.batch_post")
cls.client_post_patcher.start()
cls.consumer_post_patcher.start()
@@ -46,7 +46,7 @@ class TestClient(unittest.TestCase):
self.client.flush()
def test_basic_capture(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
msg_uuid = client.capture("python test event", distinct_id="distinct_id")
self.assertIsNotNone(msg_uuid)
@@ -61,7 +61,7 @@ class TestClient(unittest.TestCase):
self.assertTrue(isinstance(msg["timestamp"], str))
self.assertIsNotNone(msg.get("uuid"))
self.assertEqual(msg["distinct_id"], "distinct_id")
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
self.assertEqual(msg["properties"]["$lib"], "insights-python")
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
# these will change between platforms so just asssert on presence here
assert msg["properties"]["$python_runtime"] == mock.ANY
@@ -70,7 +70,7 @@ class TestClient(unittest.TestCase):
assert msg["properties"]["$os_version"] == mock.ANY
def test_basic_capture_with_uuid(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
uuid = str(uuid4())
msg_uuid = client.capture(
@@ -88,11 +88,11 @@ class TestClient(unittest.TestCase):
self.assertTrue(isinstance(msg["timestamp"], str))
self.assertEqual(msg["uuid"], uuid)
self.assertEqual(msg["distinct_id"], "distinct_id")
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
self.assertEqual(msg["properties"]["$lib"], "insights-python")
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
def test_basic_capture_with_project_api_key(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
project_api_key=FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -111,11 +111,11 @@ class TestClient(unittest.TestCase):
self.assertEqual(msg["event"], "python test event")
self.assertTrue(isinstance(msg["timestamp"], str))
self.assertEqual(msg["distinct_id"], "distinct_id")
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
self.assertEqual(msg["properties"]["$lib"], "insights-python")
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
def test_basic_super_properties(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
super_properties={"source": "repo-name"},
@@ -230,7 +230,7 @@ class TestClient(unittest.TestCase):
capture_call[1]["properties"]["$exception_list"][0]["stacktrace"][
"frames"
][0]["filename"],
"posthog/test/test_client.py",
"hanzo_insights/test/test_client.py",
)
self.assertEqual(
capture_call[1]["properties"]["$exception_list"][0]["stacktrace"][
@@ -242,7 +242,7 @@ class TestClient(unittest.TestCase):
capture_call[1]["properties"]["$exception_list"][0]["stacktrace"][
"frames"
][0]["module"],
"posthog.test.test_client",
"hanzo_insights.test.test_client",
)
self.assertEqual(
capture_call[1]["properties"]["$exception_list"][0]["stacktrace"][
@@ -253,31 +253,31 @@ class TestClient(unittest.TestCase):
def test_basic_capture_exception_with_no_exception_happening(self):
with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
with self.assertLogs("posthog", level="WARNING") as logs:
with self.assertLogs("hanzo_insights", level="WARNING") as logs:
client = self.client
client.capture_exception(None)
self.assertFalse(patch_capture.called)
self.assertEqual(
logs.output[0],
"WARNING:posthog:No exception information available",
"WARNING:hanzo_insights:No exception information available",
)
def test_capture_exception_logs_when_enabled(self):
client = Client(FAKE_TEST_API_KEY, log_captured_exceptions=True)
with self.assertLogs("posthog", level="ERROR") as logs:
with self.assertLogs("hanzo_insights", level="ERROR") as logs:
client.capture_exception(
Exception("test exception"), distinct_id="distinct_id"
)
self.assertEqual(
logs.output[0], "ERROR:posthog:test exception\nNoneType: None"
logs.output[0], "ERROR:hanzo_insights:test exception\nNoneType: None"
)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
def test_basic_capture_with_feature_flags(self, patch_flags):
patch_flags.return_value = {"featureFlags": {"beta-feature": "random-variant"}}
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -299,7 +299,7 @@ class TestClient(unittest.TestCase):
self.assertTrue(isinstance(msg["timestamp"], str))
self.assertIsNotNone(msg.get("uuid"))
self.assertEqual(msg["distinct_id"], "distinct_id")
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
self.assertEqual(msg["properties"]["$lib"], "insights-python")
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
self.assertEqual(
msg["properties"]["$feature/beta-feature"], "random-variant"
@@ -310,7 +310,7 @@ class TestClient(unittest.TestCase):
self.assertEqual(patch_flags.call_count, 1)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
def test_basic_capture_with_locally_evaluated_feature_flags(self, patch_flags):
patch_flags.return_value = {"featureFlags": {"beta-feature": "random-variant"}}
@@ -400,7 +400,7 @@ class TestClient(unittest.TestCase):
},
}
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -424,7 +424,7 @@ class TestClient(unittest.TestCase):
self.assertTrue(isinstance(msg["timestamp"], str))
self.assertIsNotNone(msg.get("uuid"))
self.assertEqual(msg["distinct_id"], "distinct_id")
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
self.assertEqual(msg["properties"]["$lib"], "insights-python")
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
self.assertEqual(
msg["properties"]["$feature/beta-feature-local"], "third-variant"
@@ -438,7 +438,7 @@ class TestClient(unittest.TestCase):
self.assertEqual(patch_flags.call_count, 0)
# test that flags are not evaluated without local evaluation
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -460,7 +460,7 @@ class TestClient(unittest.TestCase):
assert "$feature/false-flag" not in msg["properties"]
assert "$active_feature_flags" not in msg["properties"]
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.get")
def test_load_feature_flags_quota_limited(self, patch_get):
mock_response = {
"type": "quota_limited",
@@ -470,16 +470,30 @@ class TestClient(unittest.TestCase):
patch_get.side_effect = APIError(402, mock_response["detail"])
client = Client(FAKE_TEST_API_KEY, personal_api_key="test")
with self.assertLogs("posthog", level="WARNING") as logs:
with self.assertLogs("hanzo_insights", level="WARNING") as logs:
client._load_feature_flags()
self.assertEqual(client.feature_flags, [])
self.assertEqual(client.feature_flags_by_key, {})
self.assertEqual(client.group_type_mapping, {})
self.assertEqual(client.cohorts, {})
self.assertIn("PostHog feature flags quota limited", logs.output[0])
self.assertIn("Insights feature flags quota limited", logs.output[0])
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.get")
def test_load_feature_flags_unauthorized(self, patch_get):
patch_get.side_effect = APIError(401, "Unauthorized")
client = Client(FAKE_TEST_API_KEY, personal_api_key="test")
with self.assertLogs("hanzo_insights", level="ERROR") as logs:
client._load_feature_flags()
self.assertEqual(client.feature_flags, [])
self.assertEqual(client.feature_flags_by_key, {})
self.assertEqual(client.group_type_mapping, {})
self.assertEqual(client.cohorts, {})
self.assertIn("please set a valid personal_api_key", logs.output[0])
@mock.patch("hanzo_insights.client.flags")
def test_dont_override_capture_with_local_flags(self, patch_flags):
patch_flags.return_value = {"featureFlags": {"beta-feature": "random-variant"}}
@@ -554,7 +568,7 @@ class TestClient(unittest.TestCase):
},
}
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -581,7 +595,7 @@ class TestClient(unittest.TestCase):
self.assertTrue(isinstance(msg["timestamp"], str))
self.assertIsNotNone(msg.get("uuid"))
self.assertEqual(msg["distinct_id"], "distinct_id")
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
self.assertEqual(msg["properties"]["$lib"], "insights-python")
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
self.assertEqual(
msg["properties"]["$feature/beta-feature-local"], "my-custom-variant"
@@ -594,7 +608,7 @@ class TestClient(unittest.TestCase):
self.assertEqual(patch_flags.call_count, 0)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
def test_basic_capture_with_feature_flags_returns_active_only(self, patch_flags):
patch_flags.return_value = {
"featureFlags": {
@@ -604,7 +618,7 @@ class TestClient(unittest.TestCase):
}
}
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -627,7 +641,7 @@ class TestClient(unittest.TestCase):
self.assertIsNotNone(msg.get("uuid"))
self.assertEqual(msg["distinct_id"], "distinct_id")
self.assertTrue(msg["properties"]["$geoip_disable"])
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
self.assertEqual(msg["properties"]["$lib"], "insights-python")
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
self.assertEqual(
msg["properties"]["$feature/beta-feature"], "random-variant"
@@ -641,7 +655,7 @@ class TestClient(unittest.TestCase):
self.assertEqual(patch_flags.call_count, 1)
patch_flags.assert_called_with(
"random_key",
"https://us.i.posthog.com",
"https://us.i.insights.hanzo.ai",
timeout=3,
distinct_id="distinct_id",
groups={},
@@ -651,7 +665,7 @@ class TestClient(unittest.TestCase):
device_id=None,
)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
def test_basic_capture_with_feature_flags_and_disable_geoip_returns_correctly(
self, patch_flags
):
@@ -663,7 +677,7 @@ class TestClient(unittest.TestCase):
}
}
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
host="https://app.posthog.com",
@@ -692,7 +706,7 @@ class TestClient(unittest.TestCase):
self.assertIsNotNone(msg.get("uuid"))
self.assertTrue("$geoip_disable" not in msg["properties"])
self.assertEqual(msg["distinct_id"], "distinct_id")
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
self.assertEqual(msg["properties"]["$lib"], "insights-python")
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
self.assertEqual(
msg["properties"]["$feature/beta-feature"], "random-variant"
@@ -706,7 +720,7 @@ class TestClient(unittest.TestCase):
self.assertEqual(patch_flags.call_count, 1)
patch_flags.assert_called_with(
"random_key",
"https://us.i.posthog.com",
"https://us.i.insights.hanzo.ai",
timeout=12,
distinct_id="distinct_id",
groups={},
@@ -716,13 +730,13 @@ class TestClient(unittest.TestCase):
device_id=None,
)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
def test_basic_capture_with_feature_flags_switched_off_doesnt_send_them(
self, patch_flags
):
patch_flags.return_value = {"featureFlags": {"beta-feature": "random-variant"}}
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -744,14 +758,14 @@ class TestClient(unittest.TestCase):
self.assertTrue(isinstance(msg["timestamp"], str))
self.assertIsNotNone(msg.get("uuid"))
self.assertEqual(msg["distinct_id"], "distinct_id")
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
self.assertEqual(msg["properties"]["$lib"], "insights-python")
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
self.assertTrue("$feature/beta-feature" not in msg["properties"])
self.assertTrue("$active_feature_flags" not in msg["properties"])
self.assertEqual(patch_flags.call_count, 0)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
def test_capture_with_send_feature_flags_false_and_local_evaluation_doesnt_send_flags(
self, patch_flags
):
@@ -800,7 +814,7 @@ class TestClient(unittest.TestCase):
},
}
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -833,7 +847,7 @@ class TestClient(unittest.TestCase):
# CRITICAL: Verify the /flags API was NOT called
self.assertEqual(patch_flags.call_count, 0)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
def test_capture_with_send_feature_flags_true_and_local_evaluation_uses_local_flags(
self, patch_flags
):
@@ -882,7 +896,7 @@ class TestClient(unittest.TestCase):
},
}
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -923,12 +937,12 @@ class TestClient(unittest.TestCase):
# CRITICAL: Verify the /flags API was NOT called
self.assertEqual(patch_flags.call_count, 0)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
def test_capture_with_send_feature_flags_options_only_evaluate_locally_true(
self, patch_flags
):
"""Test that SendFeatureFlagsOptions with only_evaluate_locally=True uses local evaluation"""
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -976,14 +990,14 @@ class TestClient(unittest.TestCase):
self.assertEqual(msg["properties"]["$feature/local-flag"], True)
self.assertEqual(msg["properties"]["$active_feature_flags"], ["local-flag"])
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
def test_capture_with_send_feature_flags_options_only_evaluate_locally_false(
self, patch_flags
):
"""Test that SendFeatureFlagsOptions with only_evaluate_locally=False forces remote evaluation"""
patch_flags.return_value = {"featureFlags": {"remote-flag": "remote-value"}}
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -1022,14 +1036,14 @@ class TestClient(unittest.TestCase):
self.assertEqual(msg["properties"]["$feature/remote-flag"], "remote-value")
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
def test_capture_with_send_feature_flags_options_default_behavior(
self, patch_flags
):
"""Test that SendFeatureFlagsOptions without only_evaluate_locally defaults to remote evaluation"""
patch_flags.return_value = {"featureFlags": {"default-flag": "default-value"}}
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -1062,12 +1076,12 @@ class TestClient(unittest.TestCase):
msg["properties"]["$feature/default-flag"], "default-value"
)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
def test_capture_exception_with_send_feature_flags_options(self, patch_flags):
"""Test that capture_exception also supports SendFeatureFlagsOptions"""
patch_flags.return_value = {"featureFlags": {"exception-flag": True}}
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -1106,7 +1120,7 @@ class TestClient(unittest.TestCase):
def test_stringifies_distinct_id(self):
# A large number that loses precision in node:
# node -e "console.log(157963456373623802 + 1)" > 157963456373623800
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
msg_uuid = client.capture(
"python test event", distinct_id=157963456373623802
@@ -1122,7 +1136,7 @@ class TestClient(unittest.TestCase):
self.assertEqual(msg["distinct_id"], "157963456373623802")
def test_advanced_capture(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
msg_uuid = client.capture(
"python test event",
@@ -1142,14 +1156,14 @@ class TestClient(unittest.TestCase):
self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
self.assertEqual(msg["properties"]["property"], "value")
self.assertEqual(msg["event"], "python test event")
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
self.assertEqual(msg["properties"]["$lib"], "insights-python")
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
self.assertEqual(msg["uuid"], "new-uuid")
self.assertEqual(msg["distinct_id"], "distinct_id")
self.assertTrue("$groups" not in msg["properties"])
def test_groups_capture(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
msg_uuid = client.capture(
"test_event",
@@ -1170,7 +1184,7 @@ class TestClient(unittest.TestCase):
)
def test_basic_set(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
msg_uuid = client.set(
distinct_id="distinct_id", properties={"trait": "value"}
@@ -1189,7 +1203,7 @@ class TestClient(unittest.TestCase):
self.assertEqual(msg["distinct_id"], "distinct_id")
def test_advanced_set(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
msg_uuid = client.set(
distinct_id="distinct_id",
@@ -1207,14 +1221,14 @@ class TestClient(unittest.TestCase):
self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
self.assertEqual(msg["$set"]["trait"], "value")
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
self.assertEqual(msg["properties"]["$lib"], "insights-python")
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
self.assertTrue(isinstance(msg["timestamp"], str))
self.assertEqual(msg["uuid"], "new-uuid")
self.assertEqual(msg["distinct_id"], "distinct_id")
def test_basic_set_once(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
msg_uuid = client.set_once(
distinct_id="distinct_id", properties={"trait": "value"}
@@ -1233,7 +1247,7 @@ class TestClient(unittest.TestCase):
self.assertEqual(msg["distinct_id"], "distinct_id")
def test_advanced_set_once(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
msg_uuid = client.set_once(
distinct_id="distinct_id",
@@ -1251,14 +1265,14 @@ class TestClient(unittest.TestCase):
self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
self.assertEqual(msg["$set_once"]["trait"], "value")
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
self.assertEqual(msg["properties"]["$lib"], "insights-python")
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
self.assertTrue(isinstance(msg["timestamp"], str))
self.assertEqual(msg["uuid"], "new-uuid")
self.assertEqual(msg["distinct_id"], "distinct_id")
def test_basic_group_identify(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
msg_uuid = client.group_identify("organization", "id:5")
@@ -1276,7 +1290,7 @@ class TestClient(unittest.TestCase):
"$group_type": "organization",
"$group_key": "id:5",
"$group_set": {},
"$lib": "posthog-python",
"$lib": "insights-python",
"$lib_version": VERSION,
"$geoip_disable": True,
},
@@ -1285,7 +1299,7 @@ class TestClient(unittest.TestCase):
self.assertIsNotNone(msg.get("uuid"))
def test_basic_group_identify_with_distinct_id(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
msg_uuid = client.group_identify(
"organization", "id:5", distinct_id="distinct_id"
@@ -1305,7 +1319,7 @@ class TestClient(unittest.TestCase):
"$group_type": "organization",
"$group_key": "id:5",
"$group_set": {},
"$lib": "posthog-python",
"$lib": "insights-python",
"$lib_version": VERSION,
"$geoip_disable": True,
},
@@ -1314,7 +1328,7 @@ class TestClient(unittest.TestCase):
self.assertIsNotNone(msg.get("uuid"))
def test_advanced_group_identify(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
msg_uuid = client.group_identify(
"organization",
@@ -1338,7 +1352,7 @@ class TestClient(unittest.TestCase):
"$group_type": "organization",
"$group_key": "id:5",
"$group_set": {"trait": "value"},
"$lib": "posthog-python",
"$lib": "insights-python",
"$lib_version": VERSION,
"$geoip_disable": True,
},
@@ -1346,7 +1360,7 @@ class TestClient(unittest.TestCase):
self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
def test_advanced_group_identify_with_distinct_id(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
msg_uuid = client.group_identify(
"organization",
@@ -1373,7 +1387,7 @@ class TestClient(unittest.TestCase):
"$group_type": "organization",
"$group_key": "id:5",
"$group_set": {"trait": "value"},
"$lib": "posthog-python",
"$lib": "insights-python",
"$lib_version": VERSION,
"$geoip_disable": True,
},
@@ -1381,7 +1395,7 @@ class TestClient(unittest.TestCase):
self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
def test_basic_alias(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
msg_uuid = client.alias("previousId", "distinct_id")
self.assertIsNotNone(msg_uuid)
@@ -1421,7 +1435,7 @@ class TestClient(unittest.TestCase):
def test_capture_with_session_id_variations(
self, test_name, session_id, additional_properties, expected_properties
):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
properties = {"$session_id": session_id, **additional_properties}
@@ -1440,7 +1454,7 @@ class TestClient(unittest.TestCase):
self.assertEqual(msg["event"], "python test event")
self.assertEqual(msg["distinct_id"], "distinct_id")
self.assertEqual(msg["properties"]["$session_id"], session_id)
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
self.assertEqual(msg["properties"]["$lib"], "insights-python")
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
# Check additional expected properties
@@ -1448,7 +1462,7 @@ class TestClient(unittest.TestCase):
self.assertEqual(msg["properties"][key], value)
def test_session_id_preserved_with_groups(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
session_id = "group-session-101"
@@ -1473,7 +1487,7 @@ class TestClient(unittest.TestCase):
)
def test_session_id_with_anonymous_event(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
session_id = "anonymous-session-202"
@@ -1570,7 +1584,7 @@ class TestClient(unittest.TestCase):
additional_properties,
expected_additional_properties,
):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
properties = {"$session_id": session_id, **additional_properties}
@@ -1593,7 +1607,7 @@ class TestClient(unittest.TestCase):
self.assertEqual(msg["properties"][key], value)
# Verify system properties are still added
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
self.assertEqual(msg["properties"]["$lib"], "insights-python")
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
@parameterized.expand(
@@ -1637,7 +1651,7 @@ class TestClient(unittest.TestCase):
expected_session_id,
expected_super_props,
):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY, super_properties=super_properties, sync_mode=True
)
@@ -1690,7 +1704,7 @@ class TestClient(unittest.TestCase):
self.assertFalse(consumer.is_alive())
def test_synchronous(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, sync_mode=True)
msg_uuid = client.capture("test event", distinct_id="distinct_id")
@@ -1744,7 +1758,7 @@ class TestClient(unittest.TestCase):
# the post function should be called 2 times, with a batch size of 10
# each time.
with mock.patch(
"posthog.consumer.batch_post", side_effect=mock_post_fn
"hanzo_insights.consumer.batch_post", side_effect=mock_post_fn
) as mock_post:
for _ in range(20):
client.capture(
@@ -1770,7 +1784,7 @@ class TestClient(unittest.TestCase):
self.assertIsNone(msg_uuid)
self.assertFalse(self.failed)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
def test_disabled_with_feature_flags(self, patch_flags):
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, disabled=True)
@@ -1798,7 +1812,7 @@ class TestClient(unittest.TestCase):
self.assertTrue(client.queue.empty())
def test_enabled_to_disabled(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -1822,7 +1836,7 @@ class TestClient(unittest.TestCase):
self.assertFalse(self.failed)
def test_disable_geoip_default_on_events(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -1839,7 +1853,7 @@ class TestClient(unittest.TestCase):
self.assertEqual(capture_msg["properties"]["$geoip_disable"], True)
def test_disable_geoip_override_on_events(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -1875,7 +1889,7 @@ class TestClient(unittest.TestCase):
self.assertEqual("$geoip_disable" not in identify_msg["properties"], True)
def test_disable_geoip_method_overrides_init_on_events(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -1893,7 +1907,7 @@ class TestClient(unittest.TestCase):
msg = batch_data[0]
self.assertTrue("$geoip_disable" not in msg["properties"])
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
def test_disable_geoip_default_on_decide(self, patch_flags):
patch_flags.return_value = {
"featureFlags": {
@@ -1906,7 +1920,7 @@ class TestClient(unittest.TestCase):
client.get_feature_flag("random_key", "some_id", disable_geoip=True)
patch_flags.assert_called_with(
"random_key",
"https://us.i.posthog.com",
"https://us.i.insights.hanzo.ai",
timeout=3,
distinct_id="some_id",
groups={},
@@ -1922,7 +1936,7 @@ class TestClient(unittest.TestCase):
)
patch_flags.assert_called_with(
"random_key",
"https://us.i.posthog.com",
"https://us.i.insights.hanzo.ai",
timeout=3,
distinct_id="feature_enabled_distinct_id",
groups={},
@@ -1936,7 +1950,7 @@ class TestClient(unittest.TestCase):
client.get_all_flags_and_payloads("all_flags_payloads_id")
patch_flags.assert_called_with(
"random_key",
"https://us.i.posthog.com",
"https://us.i.insights.hanzo.ai",
timeout=3,
distinct_id="all_flags_payloads_id",
groups={},
@@ -1946,8 +1960,8 @@ class TestClient(unittest.TestCase):
device_id=None,
)
@mock.patch("posthog.client.Poller")
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.Poller")
@mock.patch("hanzo_insights.client.get")
def test_call_identify_fails(self, patch_get, patch_poller):
def raise_effect():
raise Exception("http exception")
@@ -1958,7 +1972,7 @@ class TestClient(unittest.TestCase):
self.assertFalse(client.feature_enabled("example", "distinct_id"))
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
def test_default_properties_get_added_properly(self, patch_flags):
patch_flags.return_value = {
"featureFlags": {
@@ -2066,7 +2080,7 @@ class TestClient(unittest.TestCase):
("get_flags_decision", ["some_id"], {}, None),
]
)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
def test_device_id_is_passed_to_flags_request(
self,
method,
@@ -2093,13 +2107,13 @@ class TestClient(unittest.TestCase):
expected_call["flag_keys_to_evaluate"] = expected_flag_keys
patch_flags.assert_called_with(
"random_key", "https://us.i.posthog.com", timeout=3, **expected_call
"random_key", "https://us.i.insights.hanzo.ai", timeout=3, **expected_call
)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
def test_device_id_from_context_is_used_in_flags_request(self, patch_flags):
"""Test that device_id from context is used in flags request when not explicitly provided."""
from posthog.contexts import new_context, set_context_device_id
from hanzo_insights.contexts import new_context, set_context_device_id
patch_flags.return_value = {
"featureFlags": {
@@ -2117,7 +2131,7 @@ class TestClient(unittest.TestCase):
client.get_feature_flag("random_key", "some_id")
patch_flags.assert_called_with(
"random_key",
"https://us.i.posthog.com",
"https://us.i.insights.hanzo.ai",
timeout=3,
distinct_id="some_id",
groups={},
@@ -2137,7 +2151,7 @@ class TestClient(unittest.TestCase):
)
patch_flags.assert_called_with(
"random_key",
"https://us.i.posthog.com",
"https://us.i.insights.hanzo.ai",
timeout=3,
distinct_id="some_id",
groups={},
@@ -2203,8 +2217,8 @@ class TestClient(unittest.TestCase):
distro_info,
):
"""Test that we can mock platform and sys for testing system_context"""
with mock.patch("posthog.utils.platform") as mock_platform:
with mock.patch("posthog.utils.sys") as mock_sys:
with mock.patch("hanzo_insights.utils.platform") as mock_platform:
with mock.patch("hanzo_insights.utils.sys") as mock_sys:
# Set up common mocks
mock_platform.python_implementation.return_value = expected_runtime
mock_sys.version_info = version_info
@@ -2220,15 +2234,15 @@ class TestClient(unittest.TestCase):
if sys_platform == "linux":
# Directly patch the get_os_info function to return our expected values
with mock.patch(
"posthog.utils.get_os_info",
"hanzo_insights.utils.get_os_info",
return_value=(expected_os, expected_os_version),
):
from posthog.utils import system_context
from hanzo_insights.utils import system_context
context = system_context()
else:
# Get system context for non-Linux platforms
from posthog.utils import system_context
from hanzo_insights.utils import system_context
context = system_context()
@@ -2242,7 +2256,7 @@ class TestClient(unittest.TestCase):
assert context == expected_context
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
def test_get_decide_returns_normalized_decide_response(self, patch_flags):
patch_flags.return_value = {
"featureFlags": {
@@ -2297,7 +2311,7 @@ class TestClient(unittest.TestCase):
}
def test_set_context_session_with_capture(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
with new_context():
set_context_session("context-session-123")
@@ -2320,7 +2334,7 @@ class TestClient(unittest.TestCase):
)
def test_set_context_session_with_page_explicit_properties(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
with new_context():
set_context_session("page-explicit-session-789")
@@ -2346,9 +2360,9 @@ class TestClient(unittest.TestCase):
def test_set_context_session_override_in_capture(self):
"""Test that explicit session ID overrides context session ID in capture"""
from posthog.contexts import new_context, set_context_session
from hanzo_insights.contexts import new_context, set_context_session
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
with new_context():
set_context_session("context-session-override")
@@ -2373,8 +2387,8 @@ class TestClient(unittest.TestCase):
msg["properties"]["$session_id"], "explicit-session-override"
)
@mock.patch("posthog.client.Poller")
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.Poller")
@mock.patch("hanzo_insights.client.get")
def test_enable_local_evaluation_false_disables_poller(
self, patch_get, patch_poller
):
@@ -2411,8 +2425,8 @@ class TestClient(unittest.TestCase):
self.assertEqual(len(client.feature_flags), 1)
self.assertEqual(client.feature_flags[0]["key"], "beta-feature")
@mock.patch("posthog.client.Poller")
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.Poller")
@mock.patch("hanzo_insights.client.get")
def test_enable_local_evaluation_true_starts_poller(self, patch_get, patch_poller):
"""Test that when enable_local_evaluation=True (default), the poller is started"""
patch_get.return_value = GetResponse(
@@ -2446,7 +2460,7 @@ class TestClient(unittest.TestCase):
self.assertEqual(len(client.feature_flags), 1)
self.assertEqual(client.feature_flags[0]["key"], "beta-feature")
@mock.patch("posthog.client.remote_config")
@mock.patch("hanzo_insights.client.remote_config")
def test_get_remote_config_payload_works_without_poller(self, patch_remote_config):
"""Test that get_remote_config_payload works without local evaluation enabled"""
patch_remote_config.return_value = {"test": "payload"}
@@ -2558,7 +2572,7 @@ class TestClient(unittest.TestCase):
client._parse_send_feature_flags(None)
self.assertIn("Invalid type for send_feature_flags", str(cm.exception))
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
def test_capture_with_send_feature_flags_flag_keys_filter(self, patch_flags):
"""Test that SendFeatureFlagsOptions with flag_keys_filter only evaluates specified flags"""
# When flag_keys_to_evaluate is provided, the API should only return the requested flags
@@ -2569,7 +2583,7 @@ class TestClient(unittest.TestCase):
}
}
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(
FAKE_TEST_API_KEY,
on_error=self.set_fail,
@@ -2605,7 +2619,7 @@ class TestClient(unittest.TestCase):
# flag2 should not be included since it wasn't requested
self.assertNotIn("$feature/flag2", msg["properties"])
@mock.patch("posthog.client.batch_post")
@mock.patch("hanzo_insights.client.batch_post")
def test_get_feature_flag_result_with_empty_string_payload(self, patch_batch_post):
"""Test that get_feature_flag_result returns a FeatureFlagResult when payload is empty string"""
client = Client(
@@ -2656,7 +2670,7 @@ class TestClient(unittest.TestCase):
self.assertEqual(result.get_value(), "empty-variant")
self.assertEqual(result.payload, "") # Should be empty string, not None
@mock.patch("posthog.client.batch_post")
@mock.patch("hanzo_insights.client.batch_post")
def test_get_all_flags_and_payloads_with_empty_string(self, patch_batch_post):
"""Test that get_all_flags_and_payloads includes flags with empty string payloads"""
client = Client(
@@ -2713,7 +2727,7 @@ class TestClient(unittest.TestCase):
)
def test_context_tags_added(self):
with mock.patch("posthog.client.batch_post") as mock_post:
with mock.patch("hanzo_insights.client.batch_post") as mock_post:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
with new_context():
@@ -2725,7 +2739,7 @@ class TestClient(unittest.TestCase):
self.assertEqual(msg["properties"]["$context_tags"], ["random_tag"])
@mock.patch(
"posthog.client.Client._enqueue", side_effect=Exception("Unexpected error")
"hanzo_insights.client.Client._enqueue", side_effect=Exception("Unexpected error")
)
def test_methods_handle_exceptions(self, mock_enqueue):
"""Test that all decorated methods handle exceptions gracefully."""
@@ -2746,7 +2760,7 @@ class TestClient(unittest.TestCase):
self.assertEqual(result, None)
@mock.patch(
"posthog.client.Client._enqueue", side_effect=Exception("Expected error")
"hanzo_insights.client.Client._enqueue", side_effect=Exception("Expected error")
)
def test_debug_flag_re_raises_exceptions(self, mock_enqueue):
"""Test that methods re-raise exceptions when debug=True."""
@@ -11,9 +11,9 @@ try:
except ImportError:
from Queue import Queue
from posthog.consumer import MAX_MSG_SIZE, Consumer
from posthog.request import APIError
from posthog.test.test_utils import TEST_API_KEY
from hanzo_insights.consumer import MAX_MSG_SIZE, Consumer
from hanzo_insights.request import APIError
from hanzo_insights.test.test_utils import TEST_API_KEY
def _track_event(event_name: str = "python event") -> dict[str, str]:
@@ -60,7 +60,7 @@ class TestConsumer(unittest.TestCase):
q = Queue()
flush_interval = 0.3
consumer = Consumer(q, TEST_API_KEY, flush_at=10, flush_interval=flush_interval)
with mock.patch("posthog.consumer.batch_post") as mock_post:
with mock.patch("hanzo_insights.consumer.batch_post") as mock_post:
consumer.start()
for i in range(3):
q.put(_track_event("python event %d" % i))
@@ -76,7 +76,7 @@ class TestConsumer(unittest.TestCase):
consumer = Consumer(
q, TEST_API_KEY, flush_at=flush_at, flush_interval=flush_interval
)
with mock.patch("posthog.consumer.batch_post") as mock_post:
with mock.patch("hanzo_insights.consumer.batch_post") as mock_post:
consumer.start()
for i in range(flush_at * 2):
q.put(_track_event("python event %d" % i))
@@ -99,7 +99,7 @@ class TestConsumer(unittest.TestCase):
consumer = Consumer(None, TEST_API_KEY, retries=retries)
with mock.patch(
"posthog.consumer.batch_post", mock.Mock(side_effect=mock_post)
"hanzo_insights.consumer.batch_post", mock.Mock(side_effect=mock_post)
):
if exception_count <= retries:
consumer.request([_track_event()])
@@ -159,7 +159,7 @@ class TestConsumer(unittest.TestCase):
return res
with mock.patch(
"posthog.request._session.post", side_effect=mock_post_fn
"hanzo_insights.request._session.post", side_effect=mock_post_fn
) as mock_post:
consumer.start()
for _ in range(0, n_msgs + 2):
@@ -167,6 +167,63 @@ class TestConsumer(unittest.TestCase):
q.join()
self.assertEqual(mock_post.call_count, 2)
def test_request_sleeps_with_retry_after(self) -> None:
error = APIError(429, "Too Many Requests", retry_after=5.0)
call_count = [0]
def mock_post(*args: Any, **kwargs: Any) -> None:
call_count[0] += 1
if call_count[0] <= 1:
raise error
consumer = Consumer(None, TEST_API_KEY, retries=3)
with (
mock.patch("hanzo_insights.consumer.batch_post", side_effect=mock_post),
mock.patch("hanzo_insights.consumer.time.sleep") as mock_sleep,
):
consumer.request([_track_event()])
mock_sleep.assert_called_once_with(5.0)
def test_request_uses_exponential_backoff_without_retry_after(self) -> None:
error = APIError(503, "Service Unavailable")
call_count = [0]
def mock_post(*args: Any, **kwargs: Any) -> None:
call_count[0] += 1
if call_count[0] <= 3:
raise error
consumer = Consumer(None, TEST_API_KEY, retries=3)
with (
mock.patch("hanzo_insights.consumer.batch_post", side_effect=mock_post),
mock.patch("hanzo_insights.consumer.time.sleep") as mock_sleep,
):
consumer.request([_track_event()])
self.assertEqual(
mock_sleep.call_args_list,
[
mock.call(1), # 2^0
mock.call(2), # 2^1
mock.call(4), # 2^2
],
)
def test_request_retries_on_408(self) -> None:
call_count = [0]
def mock_post(*args: Any, **kwargs: Any) -> None:
call_count[0] += 1
if call_count[0] <= 1:
raise APIError(408, "Request Timeout")
consumer = Consumer(None, TEST_API_KEY, retries=3)
with (
mock.patch("hanzo_insights.consumer.batch_post", side_effect=mock_post),
mock.patch("hanzo_insights.consumer.time.sleep"),
):
consumer.request([_track_event()])
self.assertEqual(call_count[0], 2)
@parameterized.expand(
[
("on_error_succeeds", False),
@@ -1,7 +1,7 @@
import unittest
from unittest.mock import patch
from posthog.contexts import (
from hanzo_insights.contexts import (
get_tags,
new_context,
scoped,
@@ -66,7 +66,7 @@ class TestContexts(unittest.TestCase):
# Back to level 1
assert get_tags() == {"level1": "value1"}
@patch("posthog.capture_exception")
@patch("hanzo_insights.capture_exception")
def test_scoped_decorator_success(self, mock_capture):
@scoped()
def successful_function(x, y):
@@ -85,7 +85,7 @@ class TestContexts(unittest.TestCase):
# Context should be cleared after function execution
assert get_tags() == {}
@patch("posthog.capture_exception")
@patch("hanzo_insights.capture_exception")
def test_scoped_decorator_exception(self, mock_capture):
test_exception = ValueError("Test exception")
@@ -111,7 +111,7 @@ class TestContexts(unittest.TestCase):
# Context should be cleared after function execution
assert get_tags() == {}
@patch("posthog.capture_exception")
@patch("hanzo_insights.capture_exception")
def test_new_context_exception_handling(self, mock_capture):
test_exception = RuntimeError("Context exception")
@@ -10,8 +10,8 @@ def test_excepthook(tmpdir):
app.write(
dedent(
"""
from posthog import Posthog
posthog = Posthog('phc_x', host='https://eu.i.posthog.com', enable_exception_autocapture=True, debug=True, on_error=lambda e, batch: print('error handling batch: ', e, batch))
from hanzo_insights import Insights
client = Insights('phc_x', host='https://eu.i.insights.hanzo.ai', enable_exception_autocapture=True, debug=True, on_error=lambda e, batch: print('error handling batch: ', e, batch))
# frame_value = "LOL"
@@ -27,7 +27,7 @@ def test_excepthook(tmpdir):
assert b"ZeroDivisionError" in output
assert b"LOL" in output
assert b"DEBUG:posthog:data uploaded successfully" in output
assert b"DEBUG:hanzo_insights:data uploaded successfully" in output
assert (
b'"$exception_list": [{"mechanism": {"type": "generic", "handled": true}, "module": null, "type": "ZeroDivisionError", "value": "division by zero", "stacktrace": {"frames": [{"platform": "python", "filename": "app.py", "abs_path"'
in output
@@ -40,14 +40,14 @@ def test_code_variables_capture(tmpdir):
dedent(
"""
import os
from posthog import Posthog
from hanzo_insights import Insights
class UnserializableObject:
pass
posthog = Posthog(
client = Insights(
'phc_x',
host='https://eu.i.posthog.com',
host='https://eu.i.insights.hanzo.ai',
debug=True,
enable_exception_autocapture=True,
capture_exception_code_variables=True,
@@ -118,29 +118,29 @@ def test_code_variables_capture(tmpdir):
assert b"'my_bool': 'True'" in output
assert b'"my_dict": "{\\"name\\": \\"test\\", \\"value\\": 123}"' in output
assert (
b'{\\"safe_key\\": \\"safe_value\\", \\"password\\": \\"$$_posthog_redacted_based_on_masking_rules_$$\\", \\"other_key\\": \\"$$_posthog_redacted_based_on_masking_rules_$$\\"}'
b'{\\"safe_key\\": \\"safe_value\\", \\"password\\": \\"$$_insights_redacted_based_on_masking_rules_$$\\", \\"other_key\\": \\"$$_insights_redacted_based_on_masking_rules_$$\\"}'
in output
)
assert (
b'{\\"level1\\": {\\"level2\\": {\\"api_key\\": \\"$$_posthog_redacted_based_on_masking_rules_$$\\", \\"data\\": \\"$$_posthog_redacted_based_on_masking_rules_$$\\", \\"safe\\": \\"visible\\"}}}'
b'{\\"level1\\": {\\"level2\\": {\\"api_key\\": \\"$$_insights_redacted_based_on_masking_rules_$$\\", \\"data\\": \\"$$_insights_redacted_based_on_masking_rules_$$\\", \\"safe\\": \\"visible\\"}}}'
in output
)
assert (
b'[\\"safe_item\\", \\"$$_posthog_redacted_based_on_masking_rules_$$\\", \\"another_safe\\"]'
b'[\\"safe_item\\", \\"$$_insights_redacted_based_on_masking_rules_$$\\", \\"another_safe\\"]'
in output
)
assert (
b'[\\"tuple_safe\\", \\"$$_posthog_redacted_based_on_masking_rules_$$\\", \\"tuple_also_safe\\"]'
b'[\\"tuple_safe\\", \\"$$_insights_redacted_based_on_masking_rules_$$\\", \\"tuple_also_safe\\"]'
in output
)
assert (
b'[{\\"id\\": 1, \\"password\\": \\"$$_posthog_redacted_based_on_masking_rules_$$\\"}, {\\"id\\": 2, \\"value\\": \\"safe_value\\"}]'
b'[{\\"id\\": 1, \\"password\\": \\"$$_insights_redacted_based_on_masking_rules_$$\\"}, {\\"id\\": 2, \\"value\\": \\"safe_value\\"}]'
in output
)
assert b"<__main__.UnserializableObject object at" in output
assert b"'my_password': '$$_posthog_redacted_based_on_masking_rules_$$'" in output
assert b"'my_password': '$$_insights_redacted_based_on_masking_rules_$$'" in output
assert (
b"'my_innocent_var': '$$_posthog_redacted_based_on_masking_rules_$$'" in output
b"'my_innocent_var': '$$_insights_redacted_based_on_masking_rules_$$'" in output
)
assert b"'__should_be_ignored':" not in output
@@ -160,12 +160,12 @@ def test_code_variables_context_override(tmpdir):
dedent(
"""
import os
import posthog
from posthog import Posthog
import hanzo_insights
from hanzo_insights import Insights
posthog_client = Posthog(
insights_client = Insights(
'phc_x',
host='https://eu.i.posthog.com',
host='https://eu.i.insights.hanzo.ai',
debug=True,
enable_exception_autocapture=True,
capture_exception_code_variables=False,
@@ -178,10 +178,10 @@ def test_code_variables_context_override(tmpdir):
1/0
with posthog.new_context(client=posthog_client):
posthog.set_capture_exception_code_variables_context(True)
posthog.set_code_variables_mask_patterns_context([r"(?i).*bank.*"])
posthog.set_code_variables_ignore_patterns_context([])
with hanzo_insights.new_context(client=insights_client):
hanzo_insights.set_capture_exception_code_variables_context(True)
hanzo_insights.set_code_variables_mask_patterns_context([r"(?i).*bank.*"])
hanzo_insights.set_code_variables_ignore_patterns_context([])
process_data()
"""
@@ -195,7 +195,7 @@ def test_code_variables_context_override(tmpdir):
assert b"ZeroDivisionError" in output
assert b"code_variables" in output
assert b"'bank': '$$_posthog_redacted_based_on_masking_rules_$$'" in output
assert b"'bank': '$$_insights_redacted_based_on_masking_rules_$$'" in output
assert b"'__dunder_var': 'should_be_visible'" in output
@@ -205,11 +205,11 @@ def test_code_variables_size_limiter(tmpdir):
dedent(
"""
import os
from posthog import Posthog
from hanzo_insights import Insights
posthog = Posthog(
client = Insights(
'phc_x',
host='https://eu.i.posthog.com',
host='https://eu.i.insights.hanzo.ai',
debug=True,
enable_exception_autocapture=True,
capture_exception_code_variables=True,
@@ -299,11 +299,11 @@ def test_code_variables_disabled_capture(tmpdir):
dedent(
"""
import os
from posthog import Posthog
from hanzo_insights import Insights
posthog = Posthog(
client = Insights(
'phc_x',
host='https://eu.i.posthog.com',
host='https://eu.i.insights.hanzo.ai',
debug=True,
enable_exception_autocapture=True,
capture_exception_code_variables=False,
@@ -340,12 +340,12 @@ def test_code_variables_enabled_then_disabled_in_context(tmpdir):
dedent(
"""
import os
import posthog
from posthog import Posthog
import hanzo_insights
from hanzo_insights import Insights
posthog_client = Posthog(
insights_client = Insights(
'phc_x',
host='https://eu.i.posthog.com',
host='https://eu.i.insights.hanzo.ai',
debug=True,
enable_exception_autocapture=True,
capture_exception_code_variables=True,
@@ -358,8 +358,8 @@ def test_code_variables_enabled_then_disabled_in_context(tmpdir):
1/0
with posthog.new_context(client=posthog_client):
posthog.set_capture_exception_code_variables_context(False)
with hanzo_insights.new_context(client=insights_client):
hanzo_insights.set_capture_exception_code_variables_context(False)
process_data()
"""
@@ -388,15 +388,15 @@ def test_code_variables_repr_fallback(tmpdir):
from datetime import datetime, timedelta
from decimal import Decimal
from fractions import Fraction
from posthog import Posthog
from hanzo_insights import Insights
class CustomReprClass:
def __repr__(self):
return '<CustomReprClass: custom representation>'
posthog = Posthog(
client = Insights(
'phc_x',
host='https://eu.i.posthog.com',
host='https://eu.i.insights.hanzo.ai',
debug=True,
enable_exception_autocapture=True,
capture_exception_code_variables=True,
@@ -450,3 +450,240 @@ def test_code_variables_repr_fallback(tmpdir):
assert "<CustomReprClass: custom representation>" in output
assert "<lambda>" in output
assert "<function trigger_error at" in output
def test_code_variables_too_long_string_value_replaced(tmpdir):
app = tmpdir.join("app.py")
app.write(
dedent(
"""
import os
from hanzo_insights import Insights
client = Insights(
'phc_x',
host='https://eu.i.insights.hanzo.ai',
debug=True,
enable_exception_autocapture=True,
capture_exception_code_variables=True,
project_root=os.path.dirname(os.path.abspath(__file__))
)
def trigger_error():
short_value = "I am short"
long_value = "x" * 20000
long_blob = "password_" + "a" * 20000
1/0
trigger_error()
"""
)
)
with pytest.raises(subprocess.CalledProcessError) as excinfo:
subprocess.check_output([sys.executable, str(app)], stderr=subprocess.STDOUT)
output = excinfo.value.output.decode("utf-8")
assert "ZeroDivisionError" in output
assert "code_variables" in output
assert "'short_value': 'I am short'" in output
assert "$$_insights_value_too_long_$$" in output
assert "'long_blob': '$$_insights_value_too_long_$$'" in output
def test_code_variables_too_long_string_in_nested_dict(tmpdir):
app = tmpdir.join("app.py")
app.write(
dedent(
"""
import os
from hanzo_insights import Insights
client = Insights(
'phc_x',
host='https://eu.i.insights.hanzo.ai',
debug=True,
enable_exception_autocapture=True,
capture_exception_code_variables=True,
project_root=os.path.dirname(os.path.abspath(__file__))
)
def trigger_error():
my_data = {
"short_key": "short_val",
"long_key": "y" * 20000,
"nested": {
"deep_long": "z" * 20000,
"deep_short": "ok",
},
}
1/0
trigger_error()
"""
)
)
with pytest.raises(subprocess.CalledProcessError) as excinfo:
subprocess.check_output([sys.executable, str(app)], stderr=subprocess.STDOUT)
output = excinfo.value.output.decode("utf-8")
assert "ZeroDivisionError" in output
assert "code_variables" in output
assert "short_val" in output
assert "ok" in output
assert "$$_insights_value_too_long_$$" in output
assert "y" * 1000 not in output
assert "z" * 1000 not in output
def test_mask_sensitive_data_too_long_dict_key():
from hanzo_insights.exception_utils import (
CODE_VARIABLES_TOO_LONG_VALUE,
_compile_patterns,
_mask_sensitive_data,
)
compiled_mask = _compile_patterns([r"(?i)password"])
result = _mask_sensitive_data(
{
"short": "visible",
"k" * 20000: "hidden_val",
"password": "secret",
},
compiled_mask,
)
assert result["short"] == "visible"
# This then gets shortened by the JSON truncation at 1024 chars anyways so no worries
assert result["k" * 20000] == CODE_VARIABLES_TOO_LONG_VALUE
assert result["password"] == "$$_insights_redacted_based_on_masking_rules_$$"
def test_mask_sensitive_data_circular_ref():
from hanzo_insights.exception_utils import _compile_patterns, _mask_sensitive_data
compiled_mask = _compile_patterns([r"(?i)password"])
# Circular dict
circular_dict = {"key": "value"}
circular_dict["self"] = circular_dict
result = _mask_sensitive_data(circular_dict, compiled_mask)
assert result["key"] == "value"
assert result["self"] == "<circular ref>"
# Circular list
circular_list = ["item"]
circular_list.append(circular_list)
result = _mask_sensitive_data(circular_list, compiled_mask)
assert result[0] == "item"
assert result[1] == "<circular ref>"
def test_compile_patterns_fast_path_and_regex_fallback():
from hanzo_insights.exception_utils import _compile_patterns, _pattern_matches
# Simple case-insensitive patterns should become substrings
simple_only = _compile_patterns([r"(?i)password", r"(?i)token", r"(?i)jwt"])
substrings, regexes = simple_only
assert substrings == ["password", "token", "jwt"]
assert regexes == []
assert _pattern_matches("my_password_var", simple_only) is True
assert _pattern_matches("MY_TOKEN", simple_only) is True
assert _pattern_matches("safe_variable", simple_only) is False
# Complex regex patterns should stay as compiled regexes
complex_only = _compile_patterns([r"^__.*", r"\d{3,}", r"^sk_live_"])
substrings, regexes = complex_only
assert substrings == []
assert len(regexes) == 3
assert _pattern_matches("__dunder", complex_only) is True
assert _pattern_matches("has_999_numbers", complex_only) is True
assert _pattern_matches("sk_live_abc123", complex_only) is True
assert _pattern_matches("normal_var", complex_only) is False
# Mixed: simple substrings + complex regexes together
mixed = _compile_patterns(
[
r"(?i)secret", # simple
r"(?i)api_key", # simple
r"^__.*", # regex
r"\btoken_\w+", # regex
]
)
substrings, regexes = mixed
assert substrings == ["secret", "api_key"]
assert len(regexes) == 2
# Substring matches
assert _pattern_matches("my_secret", mixed) is True
assert _pattern_matches("API_KEY_VALUE", mixed) is True
# Regex matches
assert _pattern_matches("__private", mixed) is True
assert _pattern_matches("token_abc", mixed) is True
# No match
assert _pattern_matches("safe_var", mixed) is False
def test_mask_sensitive_data_large_dict_replaced():
from hanzo_insights.exception_utils import (
CODE_VARIABLES_TOO_LONG_VALUE,
_compile_patterns,
_mask_sensitive_data,
)
compiled_mask = _compile_patterns([r"(?i)password"])
large_dict = {f"key_{i}": f"value_{i}" for i in range(300)}
result = _mask_sensitive_data(large_dict, compiled_mask)
assert result == CODE_VARIABLES_TOO_LONG_VALUE
def test_mask_sensitive_data_large_list_replaced():
from hanzo_insights.exception_utils import (
CODE_VARIABLES_TOO_LONG_VALUE,
_compile_patterns,
_mask_sensitive_data,
)
compiled_mask = _compile_patterns([r"(?i)password"])
large_list = [f"item_{i}" for i in range(300)]
result = _mask_sensitive_data(large_list, compiled_mask)
assert result == CODE_VARIABLES_TOO_LONG_VALUE
def test_mask_sensitive_data_large_tuple_replaced():
from hanzo_insights.exception_utils import (
CODE_VARIABLES_TOO_LONG_VALUE,
_compile_patterns,
_mask_sensitive_data,
)
compiled_mask = _compile_patterns([r"(?i)password"])
large_tuple = tuple(f"item_{i}" for i in range(300))
result = _mask_sensitive_data(large_tuple, compiled_mask)
assert result == CODE_VARIABLES_TOO_LONG_VALUE
@@ -1,6 +1,6 @@
import unittest
from posthog.types import FeatureFlag, FlagMetadata, FlagReason, LegacyFlagMetadata
from hanzo_insights.types import FeatureFlag, FlagMetadata, FlagReason, LegacyFlagMetadata
class TestFeatureFlag(unittest.TestCase):
@@ -2,9 +2,9 @@ import unittest
import mock
from posthog.client import Client
from posthog.test.test_utils import FAKE_TEST_API_KEY
from posthog.types import (
from hanzo_insights.client import Client
from hanzo_insights.test.test_utils import FAKE_TEST_API_KEY
from hanzo_insights.types import (
FeatureFlag,
FeatureFlagError,
FeatureFlagResult,
@@ -328,7 +328,7 @@ class TestGetFeatureFlagResult(unittest.TestCase):
disable_geoip=None,
)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
@mock.patch.object(Client, "capture")
def test_get_feature_flag_result_boolean_decide(self, patch_capture, patch_flags):
patch_flags.return_value = {
@@ -375,7 +375,7 @@ class TestGetFeatureFlagResult(unittest.TestCase):
captured_properties = patch_capture.call_args[1]["properties"]
self.assertNotIn("$feature_flag_error", captured_properties)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
@mock.patch.object(Client, "capture")
def test_get_feature_flag_result_variant_decide(self, patch_capture, patch_flags):
patch_flags.return_value = {
@@ -421,7 +421,7 @@ class TestGetFeatureFlagResult(unittest.TestCase):
captured_properties = patch_capture.call_args[1]["properties"]
self.assertNotIn("$feature_flag_error", captured_properties)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
@mock.patch.object(Client, "capture")
def test_get_feature_flag_result_unknown_flag(self, patch_capture, patch_flags):
patch_flags.return_value = {
@@ -461,7 +461,7 @@ class TestGetFeatureFlagResult(unittest.TestCase):
disable_geoip=None,
)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
@mock.patch.object(Client, "capture")
def test_get_feature_flag_result_with_errors_while_computing_flags(
self, patch_capture, patch_flags
@@ -507,7 +507,7 @@ class TestGetFeatureFlagResult(unittest.TestCase):
disable_geoip=None,
)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
@mock.patch.object(Client, "capture")
def test_get_feature_flag_result_flag_not_in_response(
self, patch_capture, patch_flags
@@ -549,7 +549,7 @@ class TestGetFeatureFlagResult(unittest.TestCase):
disable_geoip=None,
)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
@mock.patch.object(Client, "capture")
def test_get_feature_flag_result_errors_computing_and_flag_missing(
self, patch_capture, patch_flags
@@ -585,7 +585,7 @@ class TestGetFeatureFlagResult(unittest.TestCase):
disable_geoip=None,
)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
@mock.patch.object(Client, "capture")
def test_get_feature_flag_result_unknown_error(self, patch_capture, patch_flags):
"""Test that unexpected exceptions are captured as unknown_error."""
@@ -608,11 +608,11 @@ class TestGetFeatureFlagResult(unittest.TestCase):
disable_geoip=None,
)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
@mock.patch.object(Client, "capture")
def test_get_feature_flag_result_timeout_error(self, patch_capture, patch_flags):
"""Test that timeout errors are captured specifically."""
from posthog.request import RequestsTimeout
from hanzo_insights.request import RequestsTimeout
patch_flags.side_effect = RequestsTimeout("Request timed out")
@@ -633,11 +633,11 @@ class TestGetFeatureFlagResult(unittest.TestCase):
disable_geoip=None,
)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
@mock.patch.object(Client, "capture")
def test_get_feature_flag_result_connection_error(self, patch_capture, patch_flags):
"""Test that connection errors are captured specifically."""
from posthog.request import RequestsConnectionError
from hanzo_insights.request import RequestsConnectionError
patch_flags.side_effect = RequestsConnectionError("Connection refused")
@@ -658,11 +658,11 @@ class TestGetFeatureFlagResult(unittest.TestCase):
disable_geoip=None,
)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
@mock.patch.object(Client, "capture")
def test_get_feature_flag_result_api_error(self, patch_capture, patch_flags):
"""Test that API errors include the status code."""
from posthog.request import APIError
from hanzo_insights.request import APIError
patch_flags.side_effect = APIError(500, "Internal server error")
@@ -683,11 +683,11 @@ class TestGetFeatureFlagResult(unittest.TestCase):
disable_geoip=None,
)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
@mock.patch.object(Client, "capture")
def test_get_feature_flag_result_quota_limited(self, patch_capture, patch_flags):
"""Test that quota limit errors are captured specifically."""
from posthog.request import QuotaLimitError
from hanzo_insights.request import QuotaLimitError
patch_flags.side_effect = QuotaLimitError(429, "Rate limit exceeded")
@@ -712,7 +712,7 @@ class TestGetFeatureFlagResult(unittest.TestCase):
class TestFeatureFlagErrorWithStaleCacheFallback(unittest.TestCase):
"""Tests for stale cache fallback behavior when flag evaluation fails.
When the PostHog API is unavailable (timeout, connection error, etc.), the SDK
When the Insights API is unavailable (timeout, connection error, etc.), the SDK
falls back to stale cached flag values if available. These tests verify that:
1. The stale cached value is returned when an error occurs
2. The $feature_flag_error property is still set (for debugging)
@@ -741,11 +741,11 @@ class TestFeatureFlagErrorWithStaleCacheFallback(unittest.TestCase):
flag_definition_version=self.client.flag_definition_version,
)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
@mock.patch.object(Client, "capture")
def test_timeout_error_returns_stale_cached_value(self, patch_capture, patch_flags):
"""Test that timeout errors return stale cached value when available."""
from posthog.request import RequestsTimeout
from hanzo_insights.request import RequestsTimeout
# Pre-populate cache with a flag result
cached_result = FeatureFlagResult.from_value_and_payload(
@@ -779,13 +779,13 @@ class TestFeatureFlagErrorWithStaleCacheFallback(unittest.TestCase):
disable_geoip=None,
)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
@mock.patch.object(Client, "capture")
def test_connection_error_returns_stale_cached_value(
self, patch_capture, patch_flags
):
"""Test that connection errors return stale cached value when available."""
from posthog.request import RequestsConnectionError
from hanzo_insights.request import RequestsConnectionError
# Pre-populate cache with a boolean flag result
cached_result = FeatureFlagResult.from_value_and_payload("my-flag", True, None)
@@ -816,11 +816,11 @@ class TestFeatureFlagErrorWithStaleCacheFallback(unittest.TestCase):
disable_geoip=None,
)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
@mock.patch.object(Client, "capture")
def test_api_error_returns_stale_cached_value(self, patch_capture, patch_flags):
"""Test that API errors return stale cached value when available."""
from posthog.request import APIError
from hanzo_insights.request import APIError
# Pre-populate cache
cached_result = FeatureFlagResult.from_value_and_payload(
@@ -852,11 +852,11 @@ class TestFeatureFlagErrorWithStaleCacheFallback(unittest.TestCase):
disable_geoip=None,
)
@mock.patch("posthog.client.flags")
@mock.patch("hanzo_insights.client.flags")
@mock.patch.object(Client, "capture")
def test_error_without_cache_returns_none(self, patch_capture, patch_flags):
"""Test that errors return None when no stale cache is available."""
from posthog.request import RequestsTimeout
from hanzo_insights.request import RequestsTimeout
# Do NOT populate cache - no fallback available
File diff suppressed because it is too large Load Diff
@@ -1,7 +1,7 @@
"""
Tests for FlagDefinitionCacheProvider functionality.
These tests follow the patterns from the TypeScript implementation in posthog-js/packages/node.
These tests follow the patterns from the TypeScript implementation in insights-js/packages/node.
"""
import threading
@@ -9,13 +9,13 @@ import unittest
from typing import Optional
from unittest import mock
from posthog.client import Client
from posthog.flag_definition_cache import (
from hanzo_insights.client import Client
from hanzo_insights.flag_definition_cache import (
FlagDefinitionCacheData,
FlagDefinitionCacheProvider,
)
from posthog.request import GetResponse
from posthog.test.test_utils import FAKE_TEST_API_KEY
from hanzo_insights.request import GetResponse
from hanzo_insights.test.test_utils import FAKE_TEST_API_KEY
class MockCacheProvider:
@@ -63,8 +63,8 @@ class TestFlagDefinitionCacheProvider(unittest.TestCase):
@classmethod
def setUpClass(cls):
# Prevent real HTTP requests
cls.client_post_patcher = mock.patch("posthog.client.batch_post")
cls.consumer_post_patcher = mock.patch("posthog.consumer.batch_post")
cls.client_post_patcher = mock.patch("hanzo_insights.client.batch_post")
cls.consumer_post_patcher = mock.patch("hanzo_insights.consumer.batch_post")
cls.client_post_patcher.start()
cls.consumer_post_patcher.start()
@@ -102,7 +102,7 @@ class TestFlagDefinitionCacheProvider(unittest.TestCase):
class TestCacheInitialization(TestFlagDefinitionCacheProvider):
"""Tests for cache initialization behavior."""
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.get")
def test_uses_cached_data_when_should_fetch_returns_false(self, mock_get):
"""When should_fetch returns False and cache has data, use cached data."""
self.cache_provider.should_fetch_return_value = False
@@ -124,7 +124,7 @@ class TestCacheInitialization(TestFlagDefinitionCacheProvider):
client.join()
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.get")
def test_fetches_from_api_when_should_fetch_returns_true(self, mock_get):
"""When should_fetch returns True, fetch from API."""
self.cache_provider.should_fetch_return_value = True
@@ -148,7 +148,7 @@ class TestCacheInitialization(TestFlagDefinitionCacheProvider):
client.join()
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.get")
def test_emergency_fallback_when_cache_empty_and_no_flags(self, mock_get):
"""When should_fetch=False but cache is empty and no flags loaded, fetch anyway."""
self.cache_provider.should_fetch_return_value = False
@@ -169,7 +169,7 @@ class TestCacheInitialization(TestFlagDefinitionCacheProvider):
client.join()
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.get")
def test_preserves_existing_flags_when_cache_returns_none(self, mock_get):
"""When cache returns None but client has flags, preserve existing flags."""
self.cache_provider.should_fetch_return_value = False
@@ -197,7 +197,7 @@ class TestCacheInitialization(TestFlagDefinitionCacheProvider):
class TestFetchCoordination(TestFlagDefinitionCacheProvider):
"""Tests for fetch coordination between workers."""
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.get")
def test_calls_should_fetch_before_each_poll(self, mock_get):
"""should_fetch_flag_definitions is called before each poll cycle."""
self.cache_provider.should_fetch_return_value = True
@@ -218,7 +218,7 @@ class TestFetchCoordination(TestFlagDefinitionCacheProvider):
client.join()
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.get")
def test_does_not_call_on_received_when_fetch_skipped(self, mock_get):
"""on_flag_definitions_received is NOT called when fetch is skipped."""
self.cache_provider.should_fetch_return_value = False
@@ -232,7 +232,7 @@ class TestFetchCoordination(TestFlagDefinitionCacheProvider):
client.join()
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.get")
def test_stores_data_in_cache_after_api_fetch(self, mock_get):
"""on_flag_definitions_received receives the fetched data."""
self.cache_provider.should_fetch_return_value = True
@@ -251,7 +251,7 @@ class TestFetchCoordination(TestFlagDefinitionCacheProvider):
client.join()
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.get")
def test_304_not_modified_does_not_update_cache(self, mock_get):
"""When API returns 304 Not Modified, cache should not be updated."""
self.cache_provider.should_fetch_return_value = True
@@ -293,7 +293,7 @@ class TestFetchCoordination(TestFlagDefinitionCacheProvider):
class TestErrorHandling(TestFlagDefinitionCacheProvider):
"""Tests for error handling in cache provider operations."""
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.get")
def test_should_fetch_error_defaults_to_fetching(self, mock_get):
"""When should_fetch throws an error, default to fetching from API."""
self.cache_provider.should_fetch_error = Exception("Lock acquisition failed")
@@ -313,7 +313,7 @@ class TestErrorHandling(TestFlagDefinitionCacheProvider):
client.join()
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.get")
def test_get_error_falls_back_to_api_fetch(self, mock_get):
"""When get_flag_definitions throws an error, fetch from API."""
self.cache_provider.should_fetch_return_value = False
@@ -331,7 +331,7 @@ class TestErrorHandling(TestFlagDefinitionCacheProvider):
client.join()
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.get")
def test_on_received_error_keeps_flags_in_memory(self, mock_get):
"""When on_flag_definitions_received throws, flags are still in memory."""
self.cache_provider.should_fetch_return_value = True
@@ -350,7 +350,7 @@ class TestErrorHandling(TestFlagDefinitionCacheProvider):
client.join()
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.get")
def test_shutdown_error_is_logged_but_continues(self, mock_get):
"""When shutdown throws an error, it's logged but shutdown continues."""
self.cache_provider.shutdown_error = Exception("Lock release failed")
@@ -372,7 +372,7 @@ class TestErrorHandling(TestFlagDefinitionCacheProvider):
class TestShutdownLifecycle(TestFlagDefinitionCacheProvider):
"""Tests for shutdown lifecycle."""
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.get")
def test_shutdown_calls_cache_provider_shutdown(self, mock_get):
"""Client shutdown calls cache provider shutdown."""
mock_get.return_value = GetResponse(
@@ -387,7 +387,7 @@ class TestShutdownLifecycle(TestFlagDefinitionCacheProvider):
self.assertEqual(self.cache_provider.shutdown_call_count, 1)
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.get")
def test_shutdown_called_even_without_fetching(self, mock_get):
"""Shutdown is called even when cache was used instead of fetching."""
self.cache_provider.should_fetch_return_value = False
@@ -400,7 +400,7 @@ class TestShutdownLifecycle(TestFlagDefinitionCacheProvider):
# Shutdown should still be called
self.assertEqual(self.cache_provider.shutdown_call_count, 1)
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.get")
def test_multiple_join_calls_only_shutdown_once(self, mock_get):
"""Calling join() multiple times should only call cache provider shutdown once."""
mock_get.return_value = GetResponse(
@@ -423,7 +423,7 @@ class TestShutdownLifecycle(TestFlagDefinitionCacheProvider):
class TestBackwardCompatibility(TestFlagDefinitionCacheProvider):
"""Tests for backward compatibility without cache provider."""
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.get")
def test_works_without_cache_provider(self, mock_get):
"""Client works normally without a cache provider configured."""
mock_get.return_value = GetResponse(
@@ -451,7 +451,7 @@ class TestBackwardCompatibility(TestFlagDefinitionCacheProvider):
class TestDataIntegrity(TestFlagDefinitionCacheProvider):
"""Tests for data integrity between cache and client state."""
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.get")
def test_cached_flags_available_for_evaluation(self, mock_get):
"""Flags loaded from cache are available for local evaluation."""
self.cache_provider.should_fetch_return_value = False
@@ -483,7 +483,7 @@ class TestDataIntegrity(TestFlagDefinitionCacheProvider):
client.join()
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.get")
def test_group_type_mapping_loaded_from_cache(self, mock_get):
"""Group type mapping is correctly loaded from cache."""
self.cache_provider.should_fetch_return_value = False
@@ -497,7 +497,7 @@ class TestDataIntegrity(TestFlagDefinitionCacheProvider):
client.join()
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.get")
def test_cohorts_loaded_from_cache(self, mock_get):
"""Cohorts are correctly loaded from cache."""
self.cache_provider.should_fetch_return_value = False
@@ -510,7 +510,7 @@ class TestDataIntegrity(TestFlagDefinitionCacheProvider):
client.join()
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.get")
def test_cache_updated_when_api_returns_new_data(self, mock_get):
"""State transition: cache has old data -> API returns new -> cache updated."""
# Start with old cached data
@@ -556,7 +556,7 @@ class TestDataIntegrity(TestFlagDefinitionCacheProvider):
class TestConcurrency(TestFlagDefinitionCacheProvider):
"""Tests for thread safety and concurrent access."""
@mock.patch("posthog.client.get")
@mock.patch("hanzo_insights.client.get")
def test_concurrent_load_feature_flags_is_thread_safe(self, mock_get):
"""Multiple threads calling _load_feature_flags should not cause errors."""
mock_get.return_value = GetResponse(
@@ -1,10 +1,10 @@
import unittest
from posthog import Posthog
from hanzo_insights import Insights
class TestModule(unittest.TestCase):
posthog = None
client = None
def _assert_enqueue_result(self, result):
self.assertEqual(type(result[0]), str)
@@ -14,19 +14,19 @@ class TestModule(unittest.TestCase):
def setUp(self):
self.failed = False
self.posthog = Posthog(
self.client = Insights(
"testsecret", host="http://localhost:8000", on_error=self.failed
)
def test_track(self):
res = self.posthog.capture("python module event", distinct_id="distinct_id")
res = self.client.capture("python module event", distinct_id="distinct_id")
self._assert_enqueue_result(res)
self.posthog.flush()
self.client.flush()
def test_alias(self):
res = self.posthog.alias("previousId", "distinct_id")
res = self.client.alias("previousId", "distinct_id")
self._assert_enqueue_result(res)
self.posthog.flush()
self.client.flush()
def test_flush(self):
self.posthog.flush()
self.client.flush()
@@ -6,8 +6,8 @@ import mock
import pytest
import requests
import posthog.request as request_module
from posthog.request import (
import hanzo_insights.request as request_module
from hanzo_insights.request import (
APIError,
DatetimeSerializer,
GetResponse,
@@ -23,7 +23,7 @@ from posthog.request import (
get,
set_socket_options,
)
from posthog.test.test_utils import TEST_API_KEY
from hanzo_insights.test.test_utils import TEST_API_KEY
@pytest.mark.parametrize(
@@ -128,7 +128,7 @@ class TestRequests(unittest.TestCase):
}
).encode("utf-8")
with mock.patch("posthog.request._session.post", return_value=mock_response):
with mock.patch("hanzo_insights.request._session.post", return_value=mock_response):
with self.assertRaises(QuotaLimitError) as cm:
decide("fake_key", "fake_host")
@@ -146,7 +146,7 @@ class TestRequests(unittest.TestCase):
}
).encode("utf-8")
with mock.patch("posthog.request._session.post", return_value=mock_response):
with mock.patch("hanzo_insights.request._session.post", return_value=mock_response):
response = decide("fake_key", "fake_host")
self.assertEqual(response["featureFlags"], {"flag1": True})
@@ -154,7 +154,7 @@ class TestRequests(unittest.TestCase):
class TestGet(unittest.TestCase):
"""Unit tests for the get() function HTTP-level behavior."""
@mock.patch("posthog.request._session.get")
@mock.patch("hanzo_insights.request._session.get")
def test_get_returns_data_and_etag(self, mock_get):
"""Test that get() returns GetResponse with data and etag from headers."""
mock_response = requests.Response()
@@ -172,7 +172,7 @@ class TestGet(unittest.TestCase):
self.assertEqual(response.etag, '"abc123"')
self.assertFalse(response.not_modified)
@mock.patch("posthog.request._session.get")
@mock.patch("hanzo_insights.request._session.get")
def test_get_sends_if_none_match_header_when_etag_provided(self, mock_get):
"""Test that If-None-Match header is sent when etag parameter is provided."""
mock_response = requests.Response()
@@ -186,7 +186,7 @@ class TestGet(unittest.TestCase):
call_kwargs = mock_get.call_args[1]
self.assertEqual(call_kwargs["headers"]["If-None-Match"], '"previous-etag"')
@mock.patch("posthog.request._session.get")
@mock.patch("hanzo_insights.request._session.get")
def test_get_does_not_send_if_none_match_when_no_etag(self, mock_get):
"""Test that If-None-Match header is not sent when no etag provided."""
mock_response = requests.Response()
@@ -199,7 +199,7 @@ class TestGet(unittest.TestCase):
call_kwargs = mock_get.call_args[1]
self.assertNotIn("If-None-Match", call_kwargs["headers"])
@mock.patch("posthog.request._session.get")
@mock.patch("hanzo_insights.request._session.get")
def test_get_handles_304_not_modified(self, mock_get):
"""Test that 304 Not Modified response returns not_modified=True with no data."""
mock_response = requests.Response()
@@ -216,7 +216,7 @@ class TestGet(unittest.TestCase):
self.assertEqual(response.etag, '"unchanged-etag"')
self.assertTrue(response.not_modified)
@mock.patch("posthog.request._session.get")
@mock.patch("hanzo_insights.request._session.get")
def test_get_304_without_etag_header_uses_request_etag(self, mock_get):
"""Test that 304 response without ETag header falls back to request etag."""
mock_response = requests.Response()
@@ -231,7 +231,7 @@ class TestGet(unittest.TestCase):
self.assertTrue(response.not_modified)
self.assertEqual(response.etag, '"original-etag"')
@mock.patch("posthog.request._session.get")
@mock.patch("hanzo_insights.request._session.get")
def test_get_200_without_etag_header(self, mock_get):
"""Test that 200 response without ETag header returns None for etag."""
mock_response = requests.Response()
@@ -246,7 +246,7 @@ class TestGet(unittest.TestCase):
self.assertIsNone(response.etag)
self.assertEqual(response.data, {"flags": []})
@mock.patch("posthog.request._session.get")
@mock.patch("hanzo_insights.request._session.get")
def test_get_error_response_raises_api_error(self, mock_get):
"""Test that error responses raise APIError."""
mock_response = requests.Response()
@@ -260,7 +260,7 @@ class TestGet(unittest.TestCase):
self.assertEqual(ctx.exception.status, 401)
self.assertEqual(ctx.exception.message, "Unauthorized")
@mock.patch("posthog.request._session.get")
@mock.patch("hanzo_insights.request._session.get")
def test_get_sends_authorization_header(self, mock_get):
"""Test that Authorization header is sent with Bearer token."""
mock_response = requests.Response()
@@ -273,7 +273,7 @@ class TestGet(unittest.TestCase):
call_kwargs = mock_get.call_args[1]
self.assertEqual(call_kwargs["headers"]["Authorization"], "Bearer my-api-key")
@mock.patch("posthog.request._session.get")
@mock.patch("hanzo_insights.request._session.get")
def test_get_sends_user_agent_header(self, mock_get):
"""Test that User-Agent header is sent."""
mock_response = requests.Response()
@@ -286,10 +286,10 @@ class TestGet(unittest.TestCase):
call_kwargs = mock_get.call_args[1]
self.assertIn("User-Agent", call_kwargs["headers"])
self.assertTrue(
call_kwargs["headers"]["User-Agent"].startswith("posthog-python/")
call_kwargs["headers"]["User-Agent"].startswith("hanzo-insights-python/")
)
@mock.patch("posthog.request._session.get")
@mock.patch("hanzo_insights.request._session.get")
def test_get_passes_timeout(self, mock_get):
"""Test that timeout parameter is passed to the request."""
mock_response = requests.Response()
@@ -302,7 +302,7 @@ class TestGet(unittest.TestCase):
call_kwargs = mock_get.call_args[1]
self.assertEqual(call_kwargs["timeout"], 30)
@mock.patch("posthog.request._session.get")
@mock.patch("hanzo_insights.request._session.get")
def test_get_constructs_full_url(self, mock_get):
"""Test that host and url are combined correctly."""
mock_response = requests.Response()
@@ -315,7 +315,7 @@ class TestGet(unittest.TestCase):
call_args = mock_get.call_args[0]
self.assertEqual(call_args[0], "https://example.com/api/flags")
@mock.patch("posthog.request._session.get")
@mock.patch("hanzo_insights.request._session.get")
def test_get_removes_trailing_slash_from_host(self, mock_get):
"""Test that trailing slash is removed from host."""
mock_response = requests.Response()
@@ -339,13 +339,13 @@ class TestGet(unittest.TestCase):
("https://us.posthog.com.rg.proxy.com", "https://us.posthog.com.rg.proxy.com"),
("app.posthog.com", "app.posthog.com"),
("eu.posthog.com", "eu.posthog.com"),
("https://app.posthog.com", "https://us.i.posthog.com"),
("https://eu.posthog.com", "https://eu.i.posthog.com"),
("https://us.posthog.com", "https://us.i.posthog.com"),
("https://app.posthog.com/", "https://us.i.posthog.com"),
("https://eu.posthog.com/", "https://eu.i.posthog.com"),
("https://us.posthog.com/", "https://us.i.posthog.com"),
(None, "https://us.i.posthog.com"),
("https://app.posthog.com", "https://us.i.insights.hanzo.ai"),
("https://eu.posthog.com", "https://eu.i.insights.hanzo.ai"),
("https://us.posthog.com", "https://us.i.insights.hanzo.ai"),
("https://app.posthog.com/", "https://us.i.insights.hanzo.ai"),
("https://eu.posthog.com/", "https://eu.i.insights.hanzo.ai"),
("https://us.posthog.com/", "https://us.i.insights.hanzo.ai"),
(None, "https://us.i.insights.hanzo.ai"),
],
)
def test_routing_to_custom_host(host, expected):
@@ -355,7 +355,7 @@ def test_routing_to_custom_host(host, expected):
def test_enable_keep_alive_sets_socket_options():
try:
enable_keep_alive()
from posthog.request import _session
from hanzo_insights.request import _session
adapter = _session.get_adapter("https://example.com")
assert adapter.socket_options == KEEP_ALIVE_SOCKET_OPTIONS
@@ -367,7 +367,7 @@ def test_set_socket_options_clears_with_none():
try:
enable_keep_alive()
set_socket_options(None)
from posthog.request import _session
from hanzo_insights.request import _session
adapter = _session.get_adapter("https://example.com")
assert adapter.socket_options is None
@@ -401,17 +401,17 @@ class TestFlagsSession(unittest.TestCase):
def test_retry_status_forcelist_excludes_rate_limits(self):
"""Verify 429 (rate limit) is NOT retried - need to wait, not hammer."""
from posthog.request import RETRY_STATUS_FORCELIST
from hanzo_insights.request import RETRY_STATUS_FORCELIST
self.assertNotIn(429, RETRY_STATUS_FORCELIST)
def test_retry_status_forcelist_excludes_quota_errors(self):
"""Verify 402 (payment required/quota) is NOT retried - won't resolve."""
from posthog.request import RETRY_STATUS_FORCELIST
from hanzo_insights.request import RETRY_STATUS_FORCELIST
self.assertNotIn(402, RETRY_STATUS_FORCELIST)
@mock.patch("posthog.request._get_flags_session")
@mock.patch("hanzo_insights.request._get_flags_session")
def test_flags_uses_flags_session(self, mock_get_flags_session):
"""flags() uses the dedicated flags session, not the general session."""
mock_response = requests.Response()
@@ -434,7 +434,7 @@ class TestFlagsSession(unittest.TestCase):
mock_get_flags_session.assert_called_once()
mock_session.post.assert_called_once()
@mock.patch("posthog.request._get_flags_session")
@mock.patch("hanzo_insights.request._get_flags_session")
def test_flags_no_retry_on_quota_limit(self, mock_get_flags_session):
"""flags() raises QuotaLimitError without retrying (at application level)."""
mock_response = requests.Response()
@@ -470,7 +470,7 @@ class TestFlagsSessionNetworkRetries(unittest.TestCase):
retries on network-level failures (DNS failures, connection refused,
connection reset, etc.) up to 2 times each.
"""
from posthog.request import _build_flags_session
from hanzo_insights.request import _build_flags_session
session = _build_flags_session()
@@ -491,7 +491,7 @@ class TestFlagsSessionNetworkRetries(unittest.TestCase):
This tests the status_forcelist configuration which specifies
which HTTP status codes should trigger a retry.
"""
from posthog.request import _build_flags_session, RETRY_STATUS_FORCELIST
from hanzo_insights.request import _build_flags_session, RETRY_STATUS_FORCELIST
session = _build_flags_session()
adapter = session.get_adapter("https://test.posthog.com")
@@ -518,7 +518,7 @@ class TestFlagsSessionNetworkRetries(unittest.TestCase):
"""
Verify that retries use exponential backoff to avoid thundering herd.
"""
from posthog.request import _build_flags_session
from hanzo_insights.request import _build_flags_session
session = _build_flags_session()
adapter = session.get_adapter("https://test.posthog.com")
@@ -545,7 +545,7 @@ class TestFlagsSessionRetryIntegration(unittest.TestCase):
from http.server import HTTPServer, BaseHTTPRequestHandler
from socketserver import ThreadingMixIn
from urllib3.util.retry import Retry
from posthog.request import HTTPAdapterWithSocketOptions, RETRY_STATUS_FORCELIST
from hanzo_insights.request import HTTPAdapterWithSocketOptions, RETRY_STATUS_FORCELIST
request_count = 0
@@ -631,7 +631,7 @@ class TestFlagsSessionRetryIntegration(unittest.TestCase):
import socket
import time
from urllib3.util.retry import Retry
from posthog.request import HTTPAdapterWithSocketOptions, RETRY_STATUS_FORCELIST
from hanzo_insights.request import HTTPAdapterWithSocketOptions, RETRY_STATUS_FORCELIST
# Get an available port by binding then closing a socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -2,7 +2,7 @@ import unittest
from parameterized import parameterized
from posthog import utils
from hanzo_insights import utils
class TestSizeLimitedDict(unittest.TestCase):
@@ -2,7 +2,7 @@ import unittest
from parameterized import parameterized
from posthog.types import (
from hanzo_insights.types import (
FeatureFlag,
FlagMetadata,
FlagReason,
@@ -13,8 +13,8 @@ from parameterized import parameterized
from pydantic import BaseModel
from pydantic.v1 import BaseModel as BaseModelV1
from posthog import utils
from posthog.types import FeatureFlagResult
from hanzo_insights import utils
from hanzo_insights.types import FeatureFlagResult
TEST_API_KEY = "kOOlRy2QlMY9jHZQv0bKz0FZyazBUoY8Arj0lFVNjs4"
FAKE_TEST_API_KEY = "random_key"
@@ -96,8 +96,8 @@ class TestUtils(unittest.TestCase):
@parameterized.expand(
[
("http://posthog.io/", "http://posthog.io"),
("http://posthog.io", "http://posthog.io"),
("http://hanzo_insights.io/", "http://hanzo_insights.io"),
("http://hanzo_insights.io", "http://hanzo_insights.io"),
("https://example.com/path/", "https://example.com/path"),
("https://example.com/path", "https://example.com/path"),
]
+2 -2
View File
@@ -16,7 +16,7 @@ import distro # For Linux OS detection
import six
from dateutil.tz import tzlocal, tzutc
log = logging.getLogger("posthog")
log = logging.getLogger("hanzo_insights")
def is_naive(dt):
@@ -277,7 +277,7 @@ class FlagCache:
class RedisFlagCache:
def __init__(
self, redis_client, default_ttl=300, stale_ttl=3600, key_prefix="posthog:flags:"
self, redis_client, default_ttl=300, stale_ttl=3600, key_prefix="insights:flags:"
):
self.redis = redis_client
self.default_ttl = default_ttl
+1
View File
@@ -0,0 +1 @@
VERSION = "7.9.7"
+3
View File
@@ -0,0 +1,3 @@
# Convenience re-export so `from insights import Insights` works.
from hanzo_insights import * # noqa: F401, F403
from hanzo_insights import Insights, Client # noqa: F401
@@ -1,10 +1,10 @@
"""
Test that verifies exception capture functionality.
These tests verify that exceptions are actually captured to PostHog, not just that
These tests verify that exceptions are actually captured to Insights, not just that
500 responses are returned.
Without process_exception(), view exceptions are NOT captured to PostHog (v6.7.11 and earlier).
Without process_exception(), view exceptions are NOT captured to Insights (v6.7.11 and earlier).
With process_exception(), Django calls this method to capture exceptions before
converting them to 500 responses.
"""
@@ -30,7 +30,7 @@ def asgi_app():
@pytest.mark.asyncio
async def test_async_exception_is_captured(asgi_app):
"""
Test that async view exceptions are captured to PostHog.
Test that async view exceptions are captured to Insights.
The middleware's process_exception() method ensures exceptions are captured.
Without it (v6.7.11 and earlier), exceptions are NOT captured even though 500 is returned.
@@ -50,8 +50,8 @@ async def test_async_exception_is_captured(asgi_app):
}
)
# Patch at the posthog module level where middleware imports from
with patch("posthog.capture_exception", side_effect=mock_capture):
# Patch at the hanzo_insights module level where middleware imports from
with patch("hanzo_insights.capture_exception", side_effect=mock_capture):
async with AsyncClient(
transport=ASGITransport(app=asgi_app), base_url="http://testserver"
) as ac:
@@ -60,8 +60,8 @@ async def test_async_exception_is_captured(asgi_app):
# Django returns 500
assert response.status_code == 500
# CRITICAL: Verify PostHog captured the exception
assert len(captured) > 0, "Exception was NOT captured to PostHog!"
# CRITICAL: Verify Insights captured the exception
assert len(captured) > 0, "Exception was NOT captured to Insights!"
# Verify it's the right exception
exception_data = captured[0]
@@ -72,7 +72,7 @@ async def test_async_exception_is_captured(asgi_app):
@pytest.mark.asyncio
async def test_sync_exception_is_captured(asgi_app):
"""
Test that sync view exceptions are captured to PostHog.
Test that sync view exceptions are captured to Insights.
The middleware's process_exception() method ensures exceptions are captured.
Without it (v6.7.11 and earlier), exceptions are NOT captured even though 500 is returned.
@@ -92,8 +92,8 @@ async def test_sync_exception_is_captured(asgi_app):
}
)
# Patch at the posthog module level where middleware imports from
with patch("posthog.capture_exception", side_effect=mock_capture):
# Patch at the hanzo_insights module level where middleware imports from
with patch("hanzo_insights.capture_exception", side_effect=mock_capture):
async with AsyncClient(
transport=ASGITransport(app=asgi_app), base_url="http://testserver"
) as ac:
@@ -102,8 +102,8 @@ async def test_sync_exception_is_captured(asgi_app):
# Django returns 500
assert response.status_code == 500
# CRITICAL: Verify PostHog captured the exception
assert len(captured) > 0, "Exception was NOT captured to PostHog!"
# CRITICAL: Verify Insights captured the exception
assert len(captured) > 0, "Exception was NOT captured to Insights!"
# Verify it's the right exception
exception_data = captured[0]
+5 -5
View File
@@ -1,5 +1,5 @@
"""
Tests for PostHog Django middleware in async context.
Tests for Insights Django middleware in async context.
These tests verify that the middleware correctly handles:
1. Async user access (request.auser() in Django 5)
@@ -103,7 +103,7 @@ async def test_async_authenticated_user_access(asgi_app):
# Make request with session cookie - this should trigger the bug in v6.7.11
# Disable exception capture to see the SynchronousOnlyOperation clearly
with override_settings(POSTHOG_MW_CAPTURE_EXCEPTIONS=False):
with override_settings(INSIGHTS_MW_CAPTURE_EXCEPTIONS=False):
async with AsyncClient(
transport=ASGITransport(app=asgi_app),
base_url="http://testserver",
@@ -139,10 +139,10 @@ async def test_async_exception_capture(asgi_app):
"""
Test that middleware handles exceptions from async views.
The middleware's process_exception() method captures view exceptions to PostHog
The middleware's process_exception() method captures view exceptions to Insights
before Django converts them to 500 responses. This test verifies the exception
causes a 500 response. See test_exception_capture.py for tests that verify
actual exception capture to PostHog.
actual exception capture to Insights.
"""
async with AsyncClient(
transport=ASGITransport(app=asgi_app), base_url="http://testserver"
@@ -158,7 +158,7 @@ async def test_sync_exception_capture(asgi_app):
"""
Test that middleware handles exceptions from sync views.
The middleware's process_exception() method captures view exceptions to PostHog.
The middleware's process_exception() method captures view exceptions to Insights.
This test verifies the exception causes a 500 response.
"""
async with AsyncClient(
@@ -47,7 +47,7 @@ MIDDLEWARE = [
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"posthog.integrations.django.PosthogContextMiddleware", # Test PostHog middleware
"hanzo_insights.integrations.django.InsightsContextMiddleware", # Test Insights middleware
]
ROOT_URLCONF = "testdjango.urls"
@@ -123,7 +123,7 @@ STATIC_URL = "static/"
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
# PostHog settings for testing
POSTHOG_API_KEY = "test-key"
# Insights settings for testing
INSIGHTS_API_KEY = "test-key"
POSTHOG_HOST = "https://app.posthog.com"
POSTHOG_MW_CAPTURE_EXCEPTIONS = True
INSIGHTS_MW_CAPTURE_EXCEPTIONS = True
@@ -1,5 +1,5 @@
"""
Test views for validating PostHog middleware with Django 5 ASGI.
Test views for validating Insights middleware with Django 5 ASGI.
"""
from django.http import JsonResponse
+2 -2
View File
@@ -17,10 +17,10 @@ ignore_missing_imports = True
[mypy-sentry_sdk.*]
ignore_missing_imports = True
[mypy-posthog.test.*]
[mypy-hanzo_insights.test.*]
ignore_errors = True
[mypy-posthog.*.test.*]
[mypy-hanzo_insights.*.test.*]
ignore_errors = True
[mypy-openai.*]
-3
View File
@@ -1,3 +0,0 @@
from posthog.ai.prompts import Prompts
__all__ = ["Prompts"]
-4
View File
@@ -1,4 +0,0 @@
VERSION = "7.8.2"
if __name__ == "__main__":
print(VERSION, end="") # noqa: T201
+20 -22
View File
@@ -3,11 +3,11 @@ requires = ["setuptools>=61.0", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "posthog"
dynamic = ["version"]
description = "Integrate PostHog into any python application."
authors = [{ name = "PostHog", email = "hey@posthog.com" }]
maintainers = [{ name = "PostHog", email = "hey@posthog.com" }]
name = "hanzo-insights"
version = "7.9.7"
description = "Integrate Hanzo Insights into any python application."
authors = [{ name = "Hanzo AI", email = "hey@hanzo.ai" }]
maintainers = [{ name = "Hanzo AI", email = "hey@hanzo.ai" }]
license = { text = "MIT" }
readme = "README.md"
requires-python = ">=3.10"
@@ -33,8 +33,8 @@ dependencies = [
]
[project.urls]
Homepage = "https://github.com/posthog/posthog-python"
Repository = "https://github.com/posthog/posthog-python"
Homepage = "https://github.com/hanzoai/insights"
Repository = "https://github.com/hanzoai/posthog-python"
[project.optional-dependencies]
langchain = ["langchain>=0.2.0"]
@@ -80,24 +80,22 @@ test = [
[tool.setuptools]
packages = [
"posthog",
"posthog.ai",
"posthog.ai.langchain",
"posthog.ai.openai",
"posthog.ai.openai_agents",
"posthog.ai.anthropic",
"posthog.ai.gemini",
"posthog.test",
"posthog.test.ai",
"posthog.test.ai.openai_agents",
"posthog.integrations",
"hanzo_insights",
"hanzo_insights.ai",
"hanzo_insights.ai.langchain",
"hanzo_insights.ai.openai",
"hanzo_insights.ai.openai_agents",
"hanzo_insights.ai.anthropic",
"hanzo_insights.ai.gemini",
"hanzo_insights.test",
"hanzo_insights.test.ai",
"hanzo_insights.test.ai.openai_agents",
"hanzo_insights.integrations",
"insights",
]
[tool.setuptools.dynamic]
version = { attr = "posthog.version.VERSION" }
[tool.pytest.ini_options]
asyncio_mode = "auto"
asyncio_default_fixture_loop_scope = "function"
testpaths = ["posthog/test"]
testpaths = ["hanzo_insights/test"]
norecursedirs = ["integration_tests"]

Some files were not shown because too many files have changed in this diff Show More