Skip to content

feat(prompts): Fetch prompts by label#744

Merged
jurajmajerik merged 5 commits into
mainfrom
prompt-labels
Jul 17, 2026
Merged

feat(prompts): Fetch prompts by label#744
jurajmajerik merged 5 commits into
mainfrom
prompt-labels

Conversation

@jurajmajerik

Copy link
Copy Markdown
Contributor

Problem

PostHog prompt management is gaining labels: movable pointers from a name like production to exactly one version of a prompt, so a version can be released or rolled back without a code deploy (PostHog/posthog#70862, PostHog/posthog#71509). The SDK can fetch a prompt by name or by version number, but not by label — and fetching by label is the way labels are meant to be consumed.

Changes

Adds a label parameter to Prompts.get():

prompt = prompts.get('support-system-prompt', label='production')
  • label is passed as a query param on the existing fetch endpoint; mutually exclusive with version (raises ValueError if both are given, matching the API's 400)
  • Cache keys now include the label, so a labeled fetch is never served from the latest-version cache entry
  • PromptResult and the cache carry a label field, so callers can see which label a prompt resolved through
  • Fetching without a label or version still returns the latest version, unchanged

Draft until the backend PR (PostHog/posthog#71509) is deployed: the API ignores unknown query params, so releasing this first would send label=production and silently get the latest version back.

How did you test this code?

New unit tests: the label query param is sent and label metadata comes back on PromptResult, version+label together raises, and labeled fetches are cached separately from latest fetches (without the cache-key change, a labeled fetch after a latest fetch silently returns the wrong version). Full test_prompts.py suite passes (61 tests), ruff and mypy clean.

@jurajmajerik
jurajmajerik marked this pull request as ready for review July 16, 2026 12:44
@jurajmajerik
jurajmajerik requested a review from a team as a code owner July 16, 2026 12:44
@greptile-apps

greptile-apps Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "feat(prompts): fetch prompts by label" | Re-trigger Greptile

Comment thread posthog/ai/prompts.py
Comment thread posthog/ai/prompts.py Outdated
@github-actions

github-actions Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-07-17 09:50:09 UTC
Duration: 338871ms

✅ 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 517ms
Endpoint And Method.Does Not Use Legacy Endpoints 1010ms
Required Headers.Has Authorization Bearer Header 1009ms
Required Headers.Has Content Type Json 1008ms
Required Headers.Has Posthog Sdk Info Format 1009ms
Required Headers.Has Posthog Attempt Header 1009ms
Required Headers.Has Posthog Request Id 1010ms
Required Headers.Has Posthog Request Timestamp 1009ms
Required Headers.Has User Agent 1009ms
Body Format.Body Has Created At And Batch 1009ms
Body Format.No Api Key In Body 1009ms
Body Format.No Sent At In Body 1010ms
Event Format.Event Has Required Root Fields 1008ms
Event Format.Event Uuid Is Valid 1010ms
Event Format.Event Timestamp Is Rfc3339 1009ms
Event Format.Distinct Id Is String 1009ms
Event Format.Distinct Id At Root Not Properties 1011ms
Event Format.Custom Properties Preserved 1008ms
Event Format.Set Properties Preserved 1009ms
Event Format.Set Once Properties Preserved 1010ms
Event Format.Groups Properties Preserved 1008ms
Event Format.Sdk Generates Uuid If Not Provided 1010ms
Event Format.Event Has Required Root Fields Batch 1012ms
Event Format.Event Uuid Is Valid Batch 1013ms
Event Format.Event Timestamp Is Rfc3339 Batch 1012ms
Event Format.Distinct Id Is String Batch 1012ms
Event Format.Distinct Id At Root Not Properties Batch 1013ms
Event Format.Custom Properties Preserved Batch 1012ms
Event Format.Set Properties Preserved Batch 1014ms
Event Format.Set Once Properties Preserved Batch 1013ms
Event Format.Groups Properties Preserved Batch 1012ms
Event Format.Sdk Generates Uuid If Not Provided Batch 1013ms
Batch Behavior.Multiple Events In Single Batch 1507ms
Batch Behavior.Batch Envelope Smoke 1014ms
Batch Behavior.Flush With No Events Sends Nothing 1005ms
Batch Behavior.Flush At Triggers Batch 1509ms
Batch Behavior.Created At Reflects Batch Creation Time 1011ms
Deduplication.Generates Unique Uuids 1506ms
Deduplication.Different Events Same Content Different Uuids 1507ms
Deduplication.Preserves Uuid On Retry 7514ms
Deduplication.Preserves Timestamp On Retry 7514ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 7509ms
Deduplication.No Duplicate Events In Batch 1508ms
Header Behavior On Retry.Attempt Header Starts At One 1009ms
Header Behavior On Retry.Attempt Header Increments On Retry 14522ms
Header Behavior On Retry.Request Id Preserved On Retry 7510ms
Header Behavior On Retry.Different Requests Have Different Request Ids 3516ms
Header Behavior On Retry.Request Timestamp Changes On Retry 7514ms
Response Format Validation.Success Response Has Uuid Keyed Results 1005ms
Response Format Validation.Success Response Has Ok For Each Event 1507ms
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 1508ms
Response Format Validation.Response Echoes Request Id 1009ms
Retry Behavior.Retries On 408 7515ms
Retry Behavior.Retries On 500 7513ms
Retry Behavior.Retries On 503 9511ms
Retry Behavior.Retries On 504 7517ms
Retry Behavior.Retryable Errors Have Retry After 4509ms
Retry Behavior.Respects Retry After On Retryable Error 12517ms
Retry Behavior.Does Not Retry On 400 3504ms
Retry Behavior.Does Not Retry On 401 3508ms
Retry Behavior.Does Not Retry On 402 3507ms
Retry Behavior.Does Not Retry On 413 3510ms
Retry Behavior.Does Not Retry On 415 3507ms
Retry Behavior.Non Retryable Errors Have No Retry After 3507ms
Retry Behavior.Implements Backoff 23533ms
Retry Behavior.Max Retries Respected 23511ms
Partial Batch Handling.Handles 200 Full Success 3011ms
Partial Batch Handling.Handles 200 With All Ok 4509ms
Partial Batch Handling.Does Not Retry Dropped Events 4507ms
Partial Batch Handling.Does Not Retry Limited Events 4508ms
Partial Batch Handling.Prunes Ok Events On Partial Retry 7511ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry 7517ms
Partial Batch Handling.Retries Only Retry Events From Partial 7513ms
Partial Batch Handling.Partial Retry Preserves Uuids 7512ms
Partial Batch Handling.Partial Retry Attempt Header Increments 7511ms
Partial Batch Handling.Partial Retry Request Id Preserved 7514ms
Partial Batch Handling.Respects Retry After On Partial 9513ms
Partial Batch Handling.Unknown Result Treated As Terminal 4505ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry 4509ms
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 3510ms
Event Options.Cookieless Mode Override 1008ms
Event Options.Disable Skew Correction Override 1010ms
Event Options.Process Person Profile Override 1008ms
Event Options.Product Tour Id Override 1009ms
Event Options.Unset Options Omitted 1010ms
Event Options.Options Override In Batch 1012ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties 1010ms
Geoip And Historical Migration.Historical Migration Set In Body 1008ms
Geoip And Historical Migration.Historical Migration Absent By Default 1010ms

Feature_Flags Tests

17/17 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 1008ms
Request Payload.Flags Request Uses V2 Query Param 1006ms
Request Payload.Flags Request Hits Flags Path Not Decide 1007ms
Request Payload.Flags Request Omits Authorization Header 1008ms
Request Payload.Token In Flags Body Matches Init 1007ms
Request Payload.Groups Round Trip 1007ms
Request Payload.Groups Default To Empty Object 1006ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 1007ms
Request Payload.Disable Geoip Omitted Defaults To False 1007ms
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 1508ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 1011ms
Request Lifecycle.Mock Response Value Is Returned To Caller 1003ms
Retry Behavior.Retries Flags On 502 1007ms
Retry Behavior.Retries Flags On 504 1007ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 1510ms

@Radu-Raicea Radu-Raicea 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.

You should bump the library version and add whatever label is necessary to publish this new version.

@jurajmajerik
jurajmajerik merged commit 1653bcb into main Jul 17, 2026
37 checks passed
@jurajmajerik
jurajmajerik deleted the prompt-labels branch July 17, 2026 12:50
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.

2 participants