Skip to content

feat(ai): add Prompts.get_all to fetch all prompts at a label - #938

Merged
jurajmajerik merged 4 commits into
mainfrom
prompts-get-all-by-label
Sep 10, 2026
Merged

jurajmajerik merged 4 commits into
mainfrom
prompts-get-all-by-label

Conversation

@jurajmajerik

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

What it does:

  • prompts.get_all(label="production") fetches every prompt at that label in one call, following pagination, and returns a dict of name to PromptResult.
  • Each fetched prompt is stored in the existing cache, so later prompts.get(name, label="production") calls are cache hits. One call per cache cycle instead of one per prompt.
  • Old servers ignore the label param on the list endpoint and return latest versions. Each row is checked against its all_labels field; if nothing resolves the label, the call raises a clear error instead of caching wrong versions.

💚 How did you test it?

New unit tests in test_prompts.py:

  • pagination is followed and the cache is seeded (a later labeled get() makes no request)
  • a server that ignores the label raises instead of caching latest versions
  • a row whose label moved mid-request is skipped, the rest is kept
  • HTTP errors raise

Also ran ruff, mypy (baseline clean), the full prompts test file, and the public API snapshot check.

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed. (Docs live on posthog.com; will follow with the release.)
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Changeset added in .sampo/changesets/ (written by hand, same format as sampo add)

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

@jurajmajerik jurajmajerik self-assigned this Sep 10, 2026
@github-actions

github-actions Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-09-10 13:32:34 UTC
Duration: 256427ms

✅ 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 ✅ 511ms
Required Headers.Has Authorization Bearer Header ✅ 511ms
Required Headers.Has Content Type Json ✅ 511ms
Required Headers.Has Posthog Sdk Info Format ✅ 511ms
Required Headers.Has Posthog Attempt Header ✅ 511ms
Required Headers.Has Posthog Request Id ✅ 510ms
Required Headers.Has Posthog Request Timestamp ✅ 511ms
Required Headers.Has User Agent ✅ 512ms
Body Format.Body Has Created At And Batch ✅ 510ms
Body Format.No Api Key In Body ✅ 512ms
Body Format.No Sent At In Body ✅ 511ms
Event Format.Event Has Required Root Fields ✅ 510ms
Event Format.Event Uuid Is Valid ✅ 511ms
Event Format.Event Timestamp Is Rfc3339 ✅ 510ms
Event Format.Distinct Id Is String ✅ 511ms
Event Format.Distinct Id At Root Not Properties ✅ 511ms
Event Format.Custom Properties Preserved ✅ 510ms
Event Format.Set Properties Preserved ✅ 510ms
Event Format.Set Once Properties Preserved ✅ 510ms
Event Format.Groups Properties Preserved ✅ 511ms
Event Format.Sdk Generates Uuid If Not Provided ✅ 510ms
Event Format.Event Has Required Root Fields Batch ✅ 515ms
Event Format.Event Uuid Is Valid Batch ✅ 513ms
Event Format.Event Timestamp Is Rfc3339 Batch ✅ 514ms
Event Format.Distinct Id Is String Batch ✅ 514ms
Event Format.Distinct Id At Root Not Properties Batch ✅ 516ms
Event Format.Custom Properties Preserved Batch ✅ 514ms
Event Format.Set Properties Preserved Batch ✅ 514ms
Event Format.Set Once Properties Preserved Batch ✅ 514ms
Event Format.Groups Properties Preserved Batch ✅ 514ms
Event Format.Sdk Generates Uuid If Not Provided Batch ✅ 514ms
Batch Behavior.Multiple Events In Single Batch ✅ 518ms
Batch Behavior.Batch Envelope Smoke ✅ 516ms
Batch Behavior.Flush With No Events Sends Nothing ✅ 506ms
Batch Behavior.Flush At Triggers Batch ✅ 1011ms
Batch Behavior.Created At Reflects Batch Creation Time ✅ 511ms
Deduplication.Generates Unique Uuids ✅ 517ms
Deduplication.Different Events Same Content Different Uuids ✅ 513ms
Deduplication.Preserves Uuid On Retry ✅ 6519ms
Deduplication.Preserves Timestamp On Retry ✅ 6515ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry ✅ 6523ms
Deduplication.No Duplicate Events In Batch ✅ 519ms
Header Behavior On Retry.Attempt Header Starts At One ✅ 510ms
Header Behavior On Retry.Attempt Header Increments On Retry ✅ 13529ms
Header Behavior On Retry.Request Id Preserved On Retry ✅ 6520ms
Header Behavior On Retry.Different Requests Have Different Request Ids ✅ 3021ms
Header Behavior On Retry.Request Timestamp Changes On Retry ✅ 6516ms
Response Format Validation.Success Response Has Uuid Keyed Results ✅ 511ms
Response Format Validation.Success Response Has Ok For Each Event ✅ 514ms
Response Format Validation.Success No Retry After When All Ok ✅ 513ms
Response Format Validation.Success Retry After Present When Retry Events ✅ 1516ms
Response Format Validation.Success No Retry After When Drop Only ✅ 513ms
Response Format Validation.Response Echoes Request Id ✅ 511ms
Retry Behavior.Retries On 408 ✅ 6519ms
Retry Behavior.Retries On 500 ✅ 6520ms
Retry Behavior.Retries On 503 ✅ 8523ms
Retry Behavior.Retries On 504 ✅ 6520ms
Retry Behavior.Retryable Errors Have Retry After ✅ 3516ms
Retry Behavior.Respects Retry After On Retryable Error ✅ 11523ms
Retry Behavior.Does Not Retry On 400 ✅ 2514ms
Retry Behavior.Does Not Retry On 401 ✅ 2511ms
Retry Behavior.Does Not Retry On 402 ✅ 2513ms
Retry Behavior.Does Not Retry On 413 ✅ 2514ms
Retry Behavior.Does Not Retry On 415 ✅ 2514ms
Retry Behavior.Non Retryable Errors Have No Retry After ✅ 2513ms
Retry Behavior.Implements Backoff ✅ 22525ms
Retry Behavior.Max Retries Respected ✅ 22537ms
Partial Batch Handling.Handles 200 Full Success ✅ 2513ms
Partial Batch Handling.Handles 200 With All Ok ✅ 3516ms
Partial Batch Handling.Does Not Retry Dropped Events ✅ 3515ms
Partial Batch Handling.Does Not Retry Limited Events ✅ 3515ms
Partial Batch Handling.Prunes Ok Events On Partial Retry ✅ 6522ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry ✅ 6522ms
Partial Batch Handling.Retries Only Retry Events From Partial ✅ 6519ms
Partial Batch Handling.Partial Retry Preserves Uuids ✅ 6521ms
Partial Batch Handling.Partial Retry Attempt Header Increments ✅ 6520ms
Partial Batch Handling.Partial Retry Request Id Preserved ✅ 6522ms
Partial Batch Handling.Respects Retry After On Partial ✅ 8518ms
Partial Batch Handling.Unknown Result Treated As Terminal ✅ 3516ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry ✅ 3519ms
Compression.Sends Gzip Content Encoding ✅ 511ms
Compression.No Content Encoding When Disabled ✅ 510ms
Compression.Compressed Body Is Decompressible ✅ 510ms
Error Handling.Does Not Retry On Unknown 4Xx ✅ 2511ms
Event Options.Cookieless Mode Override ✅ 510ms
Event Options.Disable Skew Correction Override ✅ 511ms
Event Options.Process Person Profile Override ✅ 510ms
Event Options.Product Tour Id Override ✅ 510ms
Event Options.Unset Options Omitted ✅ 510ms
Event Options.Options Override In Batch ✅ 513ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties ✅ 511ms
Geoip And Historical Migration.Historical Migration Set In Body ✅ 510ms
Geoip And Historical Migration.Historical Migration Absent By Default ✅ 510ms

Feature_Flags Tests

✅ 17/17 tests passed

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

@jurajmajerik
jurajmajerik marked this pull request as ready for review September 10, 2026 12:07
@jurajmajerik
jurajmajerik requested a review from a team as a code owner September 10, 2026 12:07
Comment thread posthog/ai/prompts.py Outdated
@veria-ai

veria-ai Bot commented Sep 10, 2026 •

Copy link
Copy Markdown

PR overview

All previously flagged issues have been addressed. No open security concerns remain on this pull request.

Security review

No open security issues remain on this pull request.

Fixed/addressed: 1 · PR risk: 0/10

@greptile-apps

greptile-apps Bot commented Sep 10, 2026

Copy link
Copy Markdown
Contributor
Prompt To Fix All With AI
### Issue 1
posthog/ai/prompts.py:639-646
**Pagination returns incomplete results**

When a label spans more than 100 pages, this branch stops pagination and returns the accumulated prompts as a successful result. Callers therefore receive an incomplete dictionary and only a partially warmed cache even though `get_all` promises to fetch every prompt. The method should fail explicitly when it reaches the safety limit instead of presenting truncated data as complete.

### Issue 2
posthog/ai/prompts.py:408-412
**Compatibility failure skips telemetry**

The “none resolve label” exception is raised outside the block that calls `_maybe_capture_error`. Users who enable prompt error capture therefore receive telemetry for HTTP and parsing failures but not for this compatibility failure, making an important production misconfiguration harder to diagnose.

---

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

Reviews (1): Last reviewed commit: "feat(ai): add Prompts.get_all to fetch a..." | Re-trigger Greptile

Comment thread posthog/ai/prompts.py Outdated
Comment on lines +639 to +646
if pages >= _MAX_PROMPT_LIST_PAGES:
log.warning(
"[PostHog Prompts] Stopped following pagination for %s after %d pages. "
"The result may be incomplete.",
reference,
pages,
)
break

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 Pagination returns incomplete results

When a label spans more than 100 pages, this branch stops pagination and returns the accumulated prompts as a successful result. Callers therefore receive an incomplete dictionary and only a partially warmed cache even though get_all promises to fetch every prompt. The method should fail explicitly when it reaches the safety limit instead of presenting truncated data as complete.

Knowledge Base Used: AI observability

Prompt To Fix With AI
This is a comment left during a code review.
Path: posthog/ai/prompts.py
Line: 639-646

Comment:
**Pagination returns incomplete results**

When a label spans more than 100 pages, this branch stops pagination and returns the accumulated prompts as a successful result. Callers therefore receive an incomplete dictionary and only a partially warmed cache even though `get_all` promises to fetch every prompt. The method should fail explicitly when it reaches the safety limit instead of presenting truncated data as complete.

**Knowledge Base Used:** [AI observability](https://app.greptile.com/posthog-org-19734/-/custom-context/knowledge-base/posthog/posthog-python/-/docs/ai-observability.md)

---

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed in f42d247

Comment thread posthog/ai/prompts.py Outdated
Comment on lines +408 to +412
raise Exception(
f'[PostHog Prompts] The server returned prompts, but none resolve label "{label}". '
"It may not support fetching prompts by label on the list endpoint yet. "
"Upgrade PostHog, or fetch prompts one by one with get()."
)

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.

P2 Compatibility failure skips telemetry

The “none resolve label” exception is raised outside the block that calls _maybe_capture_error. Users who enable prompt error capture therefore receive telemetry for HTTP and parsing failures but not for this compatibility failure, making an important production misconfiguration harder to diagnose.

Knowledge Base Used: AI observability

Prompt To Fix With AI
This is a comment left during a code review.
Path: posthog/ai/prompts.py
Line: 408-412

Comment:
**Compatibility failure skips telemetry**

The “none resolve label” exception is raised outside the block that calls `_maybe_capture_error`. Users who enable prompt error capture therefore receive telemetry for HTTP and parsing failures but not for this compatibility failure, making an important production misconfiguration harder to diagnose.

**Knowledge Base Used:** [AI observability](https://app.greptile.com/posthog-org-19734/-/custom-context/knowledge-base/posthog/posthog-python/-/docs/ai-observability.md)

---

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed in 48ee3e7

@marandaneto
marandaneto requested a review from a team September 10, 2026 12:17
@marandaneto

Copy link
Copy Markdown
Member

found the same issues as the bots above
@PostHog/team-ai-observability should review

@marandaneto

Copy link
Copy Markdown
Member

No atomic snapshot guarantee: labels can move during pagination; the SDK currently skips mismatched rows

which is ok i guess?

@Radu-Raicea

Copy link
Copy Markdown
Member

@marandaneto For Prompt Management, Juraj now has more context than AIO. We'll still review it if needed, but Juraj holds the context and reasoning behind the changes. So if the SDK code looks good to your team, it's good with us!

@marandaneto

Copy link
Copy Markdown
Member

@marandaneto For Prompt Management, Juraj now has more context than AIO. We'll still review it if needed, but Juraj holds the context and reasoning behind the changes. So if the SDK code looks good to your team, it's good with us!

sorry my bad, my comment was supposed to be in the API PR :D

@marandaneto marandaneto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

approving to unblock but see comments

@jurajmajerik
jurajmajerik force-pushed the prompts-get-all-by-label branch from 7c53e57 to 48ee3e7 Compare September 10, 2026 13:27
@jurajmajerik
jurajmajerik merged commit 7db5105 into main Sep 10, 2026
42 checks passed
@jurajmajerik
jurajmajerik deleted the prompts-get-all-by-label branch September 10, 2026 13:40
@jurajmajerik

Copy link
Copy Markdown
Contributor Author

Thanks @marandaneto! Yes I expect a label moving during pagination to be rare. The skipped row will log a warning and gets picked up on the next fetch, and if nothing resolves the label we raise an error rather than caching wrong versions.

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.

3 participants