Skip to content

feat(ai): expose prompt config on fetched prompts - #801

Merged
jurajmajerik merged 3 commits into
mainfrom
prompt-config
Jul 30, 2026
Merged

feat(ai): expose prompt config on fetched prompts#801
jurajmajerik merged 3 commits into
mainfrom
prompt-config

Conversation

@jurajmajerik

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

PostHog prompt versions can now store a config object next to the prompt content (PostHog/posthog#74264): model parameters, tools, or any agent configuration, versioned with the prompt and returned by the fetch endpoint. The SDK parses API responses into typed objects, so without this change the field is fetched and then thrown away.

With this change:

result=prompts.get("summarizer", label="production", with_metadata=True)
client.chat.completions.create(
model=(result.configor {}).get("model", "gpt-4o"),
temperature=(result.configor {}).get("temperature", 0),
messages=[{"role": "system", "content": result.prompt}],
)

PromptResult gains config (object or None), CachedPrompt stores it so cache hits and the stale-cache fallback keep it, and servers that don't send the field (or send an unexpected shape) read as None. The hardcoded fallback string has no config, hence the defensive access in the example.

💚 How did you test it?

Five new tests in test_prompts.py: config surviving the API fetch and a cache hit, the stale-cache path preserving it, and a parameterized matrix for responses where config is absent, null, or a non-object (all read as None). Full file (67 tests) green, ruff clean.

📝 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)

Claude Code authored this under my direction, as the SDK follow-up to PostHog/posthog#74264. The changeset file was written manually in .sampo/changesets/ rather than via interactive sampo add. Docs for the config feature land separately on posthog.com once both SDKs support it.

@jurajmajerik
jurajmajerik requested a review from a team as a code ownerJuly 30, 2026 08:43
@jurajmajerikjurajmajerik self-assigned this Jul 30, 2026
Comment threadposthog/ai/prompts.py Outdated
@greptile-apps

Copy link
Copy Markdown
Contributor
Prompt To Fix All With AI
### Issue 1
posthog/ai/prompts.py:362
**Cached config shares mutable state**
When a caller mutates a fetched `PromptResult.config`, the shared dictionary stored in `CachedPrompt` is also modified, causing later cache-hit and stale-cache results to return caller-modified model parameters or agent configuration instead of the server-provided version.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Reviews (1): Last reviewed commit: "chore: add changeset" | Re-trigger Greptile

@github-actions

github-actionsBot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-07-30 08:53:30 UTC
Duration: 338844ms

✅ All Tests Passed!

111/111 tests passed


Capture_V1 Tests

94/94 tests passed

View Details
TestStatusDuration
Endpoint And Method.Targets V1 Endpoint517ms
Endpoint And Method.Does Not Use Legacy Endpoints1010ms
Required Headers.Has Authorization Bearer Header1008ms
Required Headers.Has Content Type Json1009ms
Required Headers.Has Posthog Sdk Info Format1008ms
Required Headers.Has Posthog Attempt Header1009ms
Required Headers.Has Posthog Request Id1009ms
Required Headers.Has Posthog Request Timestamp1008ms
Required Headers.Has User Agent1009ms
Body Format.Body Has Created At And Batch1008ms
Body Format.No Api Key In Body1009ms
Body Format.No Sent At In Body1009ms
Event Format.Event Has Required Root Fields1009ms
Event Format.Event Uuid Is Valid1009ms
Event Format.Event Timestamp Is Rfc33391009ms
Event Format.Distinct Id Is String1008ms
Event Format.Distinct Id At Root Not Properties1009ms
Event Format.Custom Properties Preserved1009ms
Event Format.Set Properties Preserved1008ms
Event Format.Set Once Properties Preserved1010ms
Event Format.Groups Properties Preserved1008ms
Event Format.Sdk Generates Uuid If Not Provided1009ms
Event Format.Event Has Required Root Fields Batch1012ms
Event Format.Event Uuid Is Valid Batch1013ms
Event Format.Event Timestamp Is Rfc3339 Batch1012ms
Event Format.Distinct Id Is String Batch1012ms
Event Format.Distinct Id At Root Not Properties Batch1013ms
Event Format.Custom Properties Preserved Batch1012ms
Event Format.Set Properties Preserved Batch1013ms
Event Format.Set Once Properties Preserved Batch1012ms
Event Format.Groups Properties Preserved Batch1012ms
Event Format.Sdk Generates Uuid If Not Provided Batch1012ms
Batch Behavior.Multiple Events In Single Batch1507ms
Batch Behavior.Batch Envelope Smoke1014ms
Batch Behavior.Flush With No Events Sends Nothing1005ms
Batch Behavior.Flush At Triggers Batch1508ms
Batch Behavior.Created At Reflects Batch Creation Time1011ms
Deduplication.Generates Unique Uuids1506ms
Deduplication.Different Events Same Content Different Uuids1508ms
Deduplication.Preserves Uuid On Retry7515ms
Deduplication.Preserves Timestamp On Retry7511ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry7515ms
Deduplication.No Duplicate Events In Batch1503ms
Header Behavior On Retry.Attempt Header Starts At One1010ms
Header Behavior On Retry.Attempt Header Increments On Retry14516ms
Header Behavior On Retry.Request Id Preserved On Retry7516ms
Header Behavior On Retry.Different Requests Have Different Request Ids3515ms
Header Behavior On Retry.Request Timestamp Changes On Retry7510ms
Response Format Validation.Success Response Has Uuid Keyed Results1010ms
Response Format Validation.Success Response Has Ok For Each Event1506ms
Response Format Validation.Success No Retry After When All Ok1507ms
Response Format Validation.Success Retry After Present When Retry Events2510ms
Response Format Validation.Success No Retry After When Drop Only1508ms
Response Format Validation.Response Echoes Request Id1008ms
Retry Behavior.Retries On 4087511ms
Retry Behavior.Retries On 5007517ms
Retry Behavior.Retries On 5039511ms
Retry Behavior.Retries On 5047516ms
Retry Behavior.Retryable Errors Have Retry After4509ms
Retry Behavior.Respects Retry After On Retryable Error12511ms
Retry Behavior.Does Not Retry On 4003510ms
Retry Behavior.Does Not Retry On 4013507ms
Retry Behavior.Does Not Retry On 4023509ms
Retry Behavior.Does Not Retry On 4133508ms
Retry Behavior.Does Not Retry On 4153508ms
Retry Behavior.Non Retryable Errors Have No Retry After3508ms
Retry Behavior.Implements Backoff23531ms
Retry Behavior.Max Retries Respected23510ms
Partial Batch Handling.Handles 200 Full Success3012ms
Partial Batch Handling.Handles 200 With All Ok4508ms
Partial Batch Handling.Does Not Retry Dropped Events4507ms
Partial Batch Handling.Does Not Retry Limited Events4507ms
Partial Batch Handling.Prunes Ok Events On Partial Retry7511ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry7516ms
Partial Batch Handling.Retries Only Retry Events From Partial7513ms
Partial Batch Handling.Partial Retry Preserves Uuids7511ms
Partial Batch Handling.Partial Retry Attempt Header Increments7508ms
Partial Batch Handling.Partial Retry Request Id Preserved7516ms
Partial Batch Handling.Respects Retry After On Partial9508ms
Partial Batch Handling.Unknown Result Treated As Terminal4508ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry4511ms
Compression.Sends Gzip Content Encoding1006ms
Compression.No Content Encoding When Disabled1009ms
Compression.Compressed Body Is Decompressible1009ms
Error Handling.Does Not Retry On Unknown 4Xx3509ms
Event Options.Cookieless Mode Override1007ms
Event Options.Disable Skew Correction Override1009ms
Event Options.Process Person Profile Override1008ms
Event Options.Product Tour Id Override1009ms
Event Options.Unset Options Omitted1009ms
Event Options.Options Override In Batch1012ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties1009ms
Geoip And Historical Migration.Historical Migration Set In Body1009ms
Geoip And Historical Migration.Historical Migration Absent By Default1009ms

Feature_Flags Tests

17/17 tests passed

View Details
TestStatusDuration
Request Payload.Request With Person Properties Device Id1007ms
Request Payload.Flags Request Uses V2 Query Param1006ms
Request Payload.Flags Request Hits Flags Path Not Decide1007ms
Request Payload.Flags Request Omits Authorization Header1007ms
Request Payload.Token In Flags Body Matches Init1006ms
Request Payload.Groups Round Trip1007ms
Request Payload.Groups Default To Empty Object1007ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False1007ms
Request Payload.Disable Geoip Omitted Defaults To False1006ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key1008ms
Request Lifecycle.No Flags Request On Init Alone503ms
Request Lifecycle.No Flags Request On Normal Capture1507ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests1011ms
Request Lifecycle.Mock Response Value Is Returned To Caller1002ms
Retry Behavior.Retries Flags On 5021007ms
Retry Behavior.Retries Flags On 5041007ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event1509ms

Sign up for freeto 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

@jurajmajerik@marandaneto