Skip to content

feat: add with_metadata option to Prompts.get()#510

Merged
carlos-marchal-ph merged 3 commits intomasterfrom
feat(llma)/prompts-get-with-metadata
Apr 15, 2026
Merged

feat: add with_metadata option to Prompts.get()#510
carlos-marchal-ph merged 3 commits intomasterfrom
feat(llma)/prompts-get-with-metadata

Conversation

@carlos-marchal-ph
Copy link
Copy Markdown
Contributor

💡 Motivation and Context

Port of PostHog/posthog-js#3387 to Python.

Prompts.get() returns only the prompt string, discarding name and version from the API response. Users want to track $ai_prompt_name / $ai_prompt_version on AI generation events without a separate HTTP call.

Adds a with_metadata option to get(). When True, returns a PromptResult dataclass with source (api, cache, stale_cache, or code_fallback), name, and version. Deprecates calling get() without with_metadata (warns once per instance via DeprecationWarning).

💚 How did you test it?

  • 14 new unit tests covering all metadata sources, deprecation warnings, cache sharing, and strengthened API response validation
  • All 51 tests pass (21 existing unchanged + 14 new + 16 compile/cache)

📝 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
  • Added the release label to the PR

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 15, 2026

posthog-python Compliance Report

Date: 2026-04-15 15:33:16 UTC
Duration: 159995ms

⚠️ Some Tests Failed

29/30 tests passed, 1 failed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 517ms
Format Validation.Event Has Uuid 1508ms
Format Validation.Event Has Lib Properties 1507ms
Format Validation.Distinct Id Is String 1507ms
Format Validation.Token Is Present 1509ms
Format Validation.Custom Properties Preserved 1507ms
Format Validation.Event Has Timestamp 1507ms
Retry Behavior.Retries On 503 9515ms
Retry Behavior.Does Not Retry On 400 3511ms
Retry Behavior.Does Not Retry On 401 3507ms
Retry Behavior.Respects Retry After Header 9515ms
Retry Behavior.Implements Backoff 23530ms
Retry Behavior.Retries On 500 7501ms
Retry Behavior.Retries On 502 7517ms
Retry Behavior.Retries On 504 7508ms
Retry Behavior.Max Retries Respected 23535ms
Deduplication.Generates Unique Uuids 1497ms
Deduplication.Preserves Uuid On Retry 7512ms
Deduplication.Preserves Uuid And Timestamp On Retry 14519ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 7511ms
Deduplication.No Duplicate Events In Batch 1508ms
Deduplication.Different Events Have Different Uuids 1508ms
Compression.Sends Gzip When Enabled 1508ms
Batch Format.Uses Proper Batch Structure 1507ms
Batch Format.Flush With No Events Sends Nothing 1006ms
Batch Format.Multiple Events Batched Together 1506ms
Error Handling.Does Not Retry On 403 3508ms
Error Handling.Does Not Retry On 413 3509ms
Error Handling.Retries On 408 7510ms

Feature_Flags Tests

⚠️ 0/1 tests passed, 1 failed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 505ms

Failures

request_payload.request_with_person_properties_device_id

404, message='NOT FOUND', url='http://sdk-adapter:8080/get_feature_flag'

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 15, 2026

Comments Outside Diff (1)

  1. posthog/ai/prompts.py, line 65-72 (link)

    P2 bool passes isinstance(..., int) check

    bool is a subclass of int in Python, so isinstance(True, int) returns True. A response body with "version": true from a misconfigured server or test stub would silently pass validation and be stored as version=True in CachedPrompt and returned in PromptResult. Use type(...) is int to exclude booleans:

    Prompt To Fix With AI
    This is a comment left during a code review.
    Path: posthog/ai/prompts.py
    Line: 65-72
    
    Comment:
    **`bool` passes `isinstance(..., int)` check**
    
    `bool` is a subclass of `int` in Python, so `isinstance(True, int)` returns `True`. A response body with `"version": true` from a misconfigured server or test stub would silently pass validation and be stored as `version=True` in `CachedPrompt` and returned in `PromptResult`. Use `type(...) is int` to exclude booleans:
    
    
    
    How can I resolve this? If you propose a fix, please make it concise.
Prompt To Fix All With AI
This is a comment left during a code review.
Path: posthog/ai/prompts.py
Line: 65-72

Comment:
**`bool` passes `isinstance(..., int)` check**

`bool` is a subclass of `int` in Python, so `isinstance(True, int)` returns `True`. A response body with `"version": true` from a misconfigured server or test stub would silently pass validation and be stored as `version=True` in `CachedPrompt` and returned in `PromptResult`. Use `type(...) is int` to exclude booleans:

```suggestion
def _is_prompt_api_response(data: Any) -> bool:
    """Check if the response is a valid prompt API response."""
    return (
        isinstance(data, dict)
        and isinstance(data.get("prompt"), str)
        and isinstance(data.get("name"), str)
        and type(data.get("version")) is int
    )
```

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/ai/prompts.py
Line: 211-213

Comment:
**Warn-once flag is not thread-safe**

The read-then-write on `_has_warned_deprecation` is not atomic. Under concurrent access, two threads can both observe `False` before either sets it to `True`, resulting in duplicate warnings. Python's standard library uses a threading lock for this pattern (e.g. `threading.Lock` or `warnings.warn` with a filter). For a deprecation-only warning the practical impact is low, but if thread safety matters here a `threading.Lock` or calling `warnings.warn(..., stacklevel=2)` without the manual flag (relying on the warning module's own per-location deduplication) would be safer.

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "chore: add sampo changeset for prompt me..." | Re-trigger Greptile

Comment thread posthog/ai/prompts.py
@carlos-marchal-ph carlos-marchal-ph merged commit 7c83f2b into master Apr 15, 2026
24 checks passed
@carlos-marchal-ph carlos-marchal-ph deleted the feat(llma)/prompts-get-with-metadata branch April 15, 2026 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants