fix: clarify feature flag auth errors#542
Open
marandaneto wants to merge 1 commit intoPostHog:mainfrom
Open
Conversation
6 tasks
Contributor
Prompt To Fix All With AIFix the following 1 code review issue. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 1
posthog/client.py:1353-1370
**Duplicated message content (OnceAndOnlyOnce)**
The error message body — `"Error loading feature flags: {e.message}. Please verify both your project_api_key and personal_api_key. More information: https://posthog.com/docs/api/overview"` — is constructed twice: once as a `%s` format string for the log call, and again as an f-string for the re-raised `APIError`. Extracting it into a local variable eliminates the duplication and keeps the two outputs in sync automatically.
```suggestion
detail = (
f"Error loading feature flags: {e.message}. "
"Please verify both your project_api_key and personal_api_key. "
"More information: https://posthog.com/docs/api/overview"
)
self.log.error("[FEATURE FLAGS] %s", detail)
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=detail)
```
Reviews (1): Last reviewed commit: "fix: clarify feature flag auth errors" | Re-trigger Greptile |
Comment on lines
1353
to
1370
| @@ -1363,8 +1364,8 @@ def _fetch_feature_flags_from_api(self): | |||
| 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 " | |||
| message=f"Error loading feature flags: {e.message}. " | |||
| "Please verify both your project_api_key and personal_api_key. " | |||
| "More information: https://posthog.com/docs/api/overview", | |||
| ) | |||
Contributor
There was a problem hiding this comment.
Duplicated message content (OnceAndOnlyOnce)
The error message body — "Error loading feature flags: {e.message}. Please verify both your project_api_key and personal_api_key. More information: https://posthog.com/docs/api/overview" — is constructed twice: once as a %s format string for the log call, and again as an f-string for the re-raised APIError. Extracting it into a local variable eliminates the duplication and keeps the two outputs in sync automatically.
Suggested change
| detail = ( | |
| f"Error loading feature flags: {e.message}. " | |
| "Please verify both your project_api_key and personal_api_key. " | |
| "More information: https://posthog.com/docs/api/overview" | |
| ) | |
| self.log.error("[FEATURE FLAGS] %s", detail) | |
| 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=detail) |
Prompt To Fix With AI
This is a comment left during a code review.
Path: posthog/client.py
Line: 1353-1370
Comment:
**Duplicated message content (OnceAndOnlyOnce)**
The error message body — `"Error loading feature flags: {e.message}. Please verify both your project_api_key and personal_api_key. More information: https://posthog.com/docs/api/overview"` — is constructed twice: once as a `%s` format string for the log call, and again as an f-string for the re-raised `APIError`. Extracting it into a local variable eliminates the duplication and keeps the two outputs in sync automatically.
```suggestion
detail = (
f"Error loading feature flags: {e.message}. "
"Please verify both your project_api_key and personal_api_key. "
"More information: https://posthog.com/docs/api/overview"
)
self.log.error("[FEATURE FLAGS] %s", detail)
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=detail)
```
How can I resolve this? If you propose a fix, please make it concise.
ioannisj
approved these changes
Apr 30, 2026
ioannisj
left a comment
There was a problem hiding this comment.
LG, minor optimization from greptile makes sense
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
💡 Motivation and Context
Relates to #526.
Local feature flag loading uses both the project API key and the personal/feature-flags API key, but 401 errors always blamed only
personal_api_key. This keeps the server-provided error detail and asks users to verify both credentials.💚 How did you test it?
uv run pytest posthog/test/test_client.py::TestClient::test_load_feature_flags_unauthorized posthog/test/test_feature_flags.py::TestLocalEvaluation::test_load_feature_flags_wrong_key -quv run ruff format --check posthog/client.py posthog/test/test_client.py posthog/test/test_feature_flags.pyuv run ruff check posthog/client.py posthog/test/test_client.py posthog/test/test_feature_flags.py📝 Checklist
If releasing new changes
sampo addto generate a changeset filereleaselabel to the PR