Skip to content

fix: propagate module-level settings to default client - #793

Open
marandaneto wants to merge 2 commits into
mainfrom
fix/module-level-settings-768
Open

fix: propagate module-level settings to default client#793
marandaneto wants to merge 2 commits into
mainfrom
fix/module-level-settings-768

Conversation

@marandaneto

@marandaneto marandaneto commented Jul 27, 2026

Copy link
Copy Markdown
Member

💡 Motivation and Context

Module-level configuration is intended to configure the lazily initialized default client, but privacy_mode, project_root, and project_api_key were not propagated. In particular, setting posthog.privacy_mode = True still allowed AI wrappers to capture full prompts and outputs.

This also clarifies the established API key naming: project_api_key is the preferred setting and takes precedence, while api_key is the deprecated legacy alias retained for backwards compatibility.

Fixes #768.

💚 How did you test it?

  • python -m pytest -q posthog/test/test_module.py posthog/test/ai/openai/test_openai.py::test_privacy_mode_global posthog/test/test_code_variables.py (185 passed)
  • ruff format --check posthog/__init__.py posthog/test/test_module.py
  • ruff check posthog/__init__.py posthog/test/test_module.py
  • python -W error -c "import posthog"

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Implemented with the Pi coding agent after validating the issue against the current source and confirming the API key naming history in issues #31 and #33 and PR #32. The API key resolution preserves the SDK's established project_api_key or api_key precedence, while privacy mode is re-synchronized on repeated setup() calls so late module-level changes take effect. A human-authored review is still required; no agent session transcript was published.

@marandaneto marandaneto self-assigned this Jul 27, 2026
@github-actions

github-actions Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-07-27 20:43:28 UTC
Duration: 338391ms

✅ All Tests Passed!

111/111 tests passed


Capture_V1 Tests

94/94 tests passed

View Details
Test Status Duration
Endpoint And Method.Targets V1 Endpoint 518ms
Endpoint And Method.Does Not Use Legacy Endpoints 1009ms
Required Headers.Has Authorization Bearer Header 1009ms
Required Headers.Has Content Type Json 1009ms
Required Headers.Has Posthog Sdk Info Format 1008ms
Required Headers.Has Posthog Attempt Header 1009ms
Required Headers.Has Posthog Request Id 1009ms
Required Headers.Has Posthog Request Timestamp 1009ms
Required Headers.Has User Agent 1008ms
Body Format.Body Has Created At And Batch 1009ms
Body Format.No Api Key In Body 1009ms
Body Format.No Sent At In Body 1009ms
Event Format.Event Has Required Root Fields 1009ms
Event Format.Event Uuid Is Valid 1008ms
Event Format.Event Timestamp Is Rfc3339 1009ms
Event Format.Distinct Id Is String 1009ms
Event Format.Distinct Id At Root Not Properties 1009ms
Event Format.Custom Properties Preserved 1009ms
Event Format.Set Properties Preserved 1009ms
Event Format.Set Once Properties Preserved 1008ms
Event Format.Groups Properties Preserved 1011ms
Event Format.Sdk Generates Uuid If Not Provided 1008ms
Event Format.Event Has Required Root Fields Batch 1013ms
Event Format.Event Uuid Is Valid Batch 1014ms
Event Format.Event Timestamp Is Rfc3339 Batch 1012ms
Event Format.Distinct Id Is String Batch 1013ms
Event Format.Distinct Id At Root Not Properties Batch 1012ms
Event Format.Custom Properties Preserved Batch 1012ms
Event Format.Set Properties Preserved Batch 1013ms
Event Format.Set Once Properties Preserved Batch 1013ms
Event Format.Groups Properties Preserved Batch 1013ms
Event Format.Sdk Generates Uuid If Not Provided Batch 1012ms
Batch Behavior.Multiple Events In Single Batch 1507ms
Batch Behavior.Batch Envelope Smoke 1014ms
Batch Behavior.Flush With No Events Sends Nothing 1006ms
Batch Behavior.Flush At Triggers Batch 1508ms
Batch Behavior.Created At Reflects Batch Creation Time 1011ms
Deduplication.Generates Unique Uuids 1506ms
Deduplication.Different Events Same Content Different Uuids 1508ms
Deduplication.Preserves Uuid On Retry 7515ms
Deduplication.Preserves Timestamp On Retry 7513ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 7512ms
Deduplication.No Duplicate Events In Batch 1504ms
Header Behavior On Retry.Attempt Header Starts At One 1009ms
Header Behavior On Retry.Attempt Header Increments On Retry 14524ms
Header Behavior On Retry.Request Id Preserved On Retry 7509ms
Header Behavior On Retry.Different Requests Have Different Request Ids 3514ms
Header Behavior On Retry.Request Timestamp Changes On Retry 7516ms
Response Format Validation.Success Response Has Uuid Keyed Results 1006ms
Response Format Validation.Success Response Has Ok For Each Event 1506ms
Response Format Validation.Success No Retry After When All Ok 1507ms
Response Format Validation.Success Retry After Present When Retry Events 2511ms
Response Format Validation.Success No Retry After When Drop Only 1507ms
Response Format Validation.Response Echoes Request Id 1010ms
Retry Behavior.Retries On 408 7513ms
Retry Behavior.Retries On 500 7513ms
Retry Behavior.Retries On 503 9517ms
Retry Behavior.Retries On 504 7510ms
Retry Behavior.Retryable Errors Have Retry After 4511ms
Retry Behavior.Respects Retry After On Retryable Error 12516ms
Retry Behavior.Does Not Retry On 400 3504ms
Retry Behavior.Does Not Retry On 401 3507ms
Retry Behavior.Does Not Retry On 402 3509ms
Retry Behavior.Does Not Retry On 413 3506ms
Retry Behavior.Does Not Retry On 415 3509ms
Retry Behavior.Non Retryable Errors Have No Retry After 3506ms
Retry Behavior.Implements Backoff 23533ms
Retry Behavior.Max Retries Respected 23507ms
Partial Batch Handling.Handles 200 Full Success 2515ms
Partial Batch Handling.Handles 200 With All Ok 4508ms
Partial Batch Handling.Does Not Retry Dropped Events 4508ms
Partial Batch Handling.Does Not Retry Limited Events 4508ms
Partial Batch Handling.Prunes Ok Events On Partial Retry 7509ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry 7519ms
Partial Batch Handling.Retries Only Retry Events From Partial 7512ms
Partial Batch Handling.Partial Retry Preserves Uuids 7511ms
Partial Batch Handling.Partial Retry Attempt Header Increments 7508ms
Partial Batch Handling.Partial Retry Request Id Preserved 7512ms
Partial Batch Handling.Respects Retry After On Partial 9516ms
Partial Batch Handling.Unknown Result Treated As Terminal 4505ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry 4508ms
Compression.Sends Gzip Content Encoding 1009ms
Compression.No Content Encoding When Disabled 1009ms
Compression.Compressed Body Is Decompressible 1009ms
Error Handling.Does Not Retry On Unknown 4Xx 3508ms
Event Options.Cookieless Mode Override 1009ms
Event Options.Disable Skew Correction Override 1009ms
Event Options.Process Person Profile Override 1009ms
Event Options.Product Tour Id Override 1009ms
Event Options.Unset Options Omitted 1008ms
Event Options.Options Override In Batch 1012ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties 1009ms
Geoip And Historical Migration.Historical Migration Set In Body 1009ms
Geoip And Historical Migration.Historical Migration Absent By Default 1008ms

Feature_Flags Tests

17/17 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 1007ms
Request Payload.Flags Request Uses V2 Query Param 1007ms
Request Payload.Flags Request Hits Flags Path Not Decide 1006ms
Request Payload.Flags Request Omits Authorization Header 1007ms
Request Payload.Token In Flags Body Matches Init 1007ms
Request Payload.Groups Round Trip 1006ms
Request Payload.Groups Default To Empty Object 1007ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 1007ms
Request Payload.Disable Geoip Omitted Defaults To False 1006ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 1007ms
Request Lifecycle.No Flags Request On Init Alone 503ms
Request Lifecycle.No Flags Request On Normal Capture 1507ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 1010ms
Request Lifecycle.Mock Response Value Is Returned To Caller 1003ms
Retry Behavior.Retries Flags On 502 1006ms
Retry Behavior.Retries Flags On 504 1007ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 1509ms

@marandaneto
marandaneto marked this pull request as ready for review July 27, 2026 20:37
@marandaneto
marandaneto requested a review from a team as a code owner July 27, 2026 20:37
@greptile-apps

greptile-apps Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor
Prompt To Fix All With AI
### Issue 1
posthog/__init__.py:1175
**Blank preferred key disables capture**

When `project_api_key` contains only whitespace and `api_key` is valid, this expression selects the truthy preferred key before trimming it, producing an empty configured key and disabling the default client, so module-level events are silently dropped.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Reviews (1): Last reviewed commit: "docs: clarify module API key naming" | Re-trigger Greptile

1 similar comment
@greptile-apps

greptile-apps Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor
Prompt To Fix All With AI
### Issue 1
posthog/__init__.py:1175
**Blank preferred key disables capture**

When `project_api_key` contains only whitespace and `api_key` is valid, this expression selects the truthy preferred key before trimming it, producing an empty configured key and disabling the default client, so module-level events are silently dropped.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Reviews (1): Last reviewed commit: "docs: clarify module API key naming" | Re-trigger Greptile

Comment thread posthog/__init__.py
global default_client
if not default_client:
configured_api_key = api_key.strip() if api_key else ""
configured_api_key = (project_api_key or api_key or "").strip()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Blank preferred key disables capture

When project_api_key contains only whitespace and api_key is valid, this expression selects the truthy preferred key before trimming it, producing an empty configured key and disabling the default client, so module-level events are silently dropped.

Knowledge Base Used: Client Core

Prompt To Fix With AI
This is a comment left during a code review.
Path: posthog/__init__.py
Line: 1175

Comment:
**Blank preferred key disables capture**

When `project_api_key` contains only whitespace and `api_key` is valid, this expression selects the truthy preferred key before trimming it, producing an empty configured key and disabling the default client, so module-level events are silently dropped.

**Knowledge Base Used:** [Client Core](https://app.greptile.com/posthog-org-19734/-/custom-context/knowledge-base/posthog/posthog-python/-/docs/client-core.md)

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

privacy_mode set at module level never reaches the client, so AI wrappers capture full prompts and outputs

1 participant