feat(prompts): add capture_errors option for error tracking#520
feat(prompts): add capture_errors option for error tracking#520andrewm4894 merged 3 commits intomainfrom
Conversation
…error tracking When enabled (opt-in, default off), prompt fetch failures are reported to PostHog error tracking via `capture_exception()` before the existing fallback logic runs (stale cache, code fallback, or re-raise). Guarded with `hasattr` check so clients without `capture_exception` don't blow up. Capture failures are silently logged and never affect fallback resolution.
posthog-python Compliance ReportDate: 2026-04-17 10:09:41 UTC ✅ All Tests Passed!30/30 tests passed Capture Tests✅ 29/29 tests passed View Details
Feature_Flags Tests✅ 1/1 tests passed View Details
|
Prompt To Fix All With AIThis is a comment left during a code review.
Path: posthog/test/ai/test_prompts.py
Line: 926-936
Comment:
**Dead mock — wrong error path exercised**
`mock_get.side_effect` is never triggered here because `Prompts` is constructed without a `project_api_key`, so `_fetch_prompt_from_api` raises `"project_api_key is required"` before the HTTP call is ever made. The test is exercising a config-error path rather than the intended "network error with no client" path, and the `@patch` decorator and `mock_get` setup are superfluous.
To test the documented scenario, supply `project_api_key` (mirroring `create_mock_posthog`'s defaults) so the mock HTTP error is actually reached.
How can I resolve this? If you propose a fix, please make it concise.
---
This is a comment left during a code review.
Path: posthog/test/ai/test_prompts.py
Line: 851-964
Comment:
**Prefer parametrised tests**
The three "NOT called" scenarios (`capture_errors=False`, no client, successful fetch) and the two basic "called-once" scenarios (with fallback, with re-raise) share nearly identical structure and could each be collapsed into a single `@parameterized.expand` test, matching the pattern already used elsewhere in this file (e.g. `test_handle_404_response`, `test_deprecation_warning_count`).
**Context Used:** Do not attempt to comment on incorrect alphabetica... ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "feat(prompts): add capture_errors option..." | Re-trigger Greptile |
…tions Include prompt_name, prompt_version, posthog_host, and $lib_feature so users can build suppression/grouping rules in error tracking without string-matching on exception messages.
Test was missing project_api_key so _fetch_prompt_from_api raised 'project_api_key is required' before the mocked HTTP error was reached. Adding both keys so the intended 'capture_errors=True with no client' path is actually exercised.
|
FYI — did a bit of research into SDK + error tracking conventions while thinking about this one. Sharing in case it's useful for anyone else looking at this later. posthog-python SDK:
Error tracking product:
Leaving it as "capture everything when the user opts in" for now — feels like the simplest starting point. If it turns out to be noisy in practice we can always add an opt-in filter later (e.g. only capture 4xx/config errors and skip 5xx/network/timeout, or a Also threaded |
Replaces #427 — rebased on current main to resolve conflicts. The old branch couldn't be force-pushed due to a signed-commits ruleset that flags historical unsigned release commits (v7.9.2–v7.9.8) as "new" on force-push/merge.
Exploratory. As a user using prompts in my app, if prompt fetches started failing and PostHog didn't surface it in error tracking, I'd be pretty upset — regardless of whether the root cause is on PostHog's side or mine. This adds an opt-in path for that.
Summary
capture_errorsparameter toPrompts.__init__()(defaultFalse, fully backward compatible)Trueand a PostHog client is provided, prompt fetch failures are reported viaclient.capture_exception()before the existing fallback logic runs (stale cache → code fallback → re-raise)capture_exception()itself fails, it's silently swallowed and never affects prompt resolutionhasattr(client, "capture_exception")check (addresses Greptile review on feat(prompts): add capture_errors option for error tracking #427)Motivation
Failed prompt fetches are almost always actionable by the app developer:
404— typo in the prompt name, or someone deleted it in the UI403—personal_api_keylost permissionsToday these are silently handled — the caller gets a fallback string and the error is only logged at WARNING level. With
capture_errors=True, they surface in PostHog error tracking, making it easy to spot API issues, auth problems, or missing prompts without configuring Python logging.Usage
Changes
posthog/ai/prompts.py— newcapture_errorsparam,_maybe_capture_error()helper withhasattrguardposthog/test/ai/test_prompts.py— 7 new tests covering all branches.sampo/changesets/prompts-capture-errors.md— sampo changeset for release (per RELEASING.md)Test plan