Skip to content

Honor a default_cache_ttl_seconds of 0 in Prompts - #873

Open
ckarnell wants to merge 1 commit into
PostHog:mainfrom
ckarnell:fix/prompts-default-cache-ttl-zero
Open

Honor a default_cache_ttl_seconds of 0 in Prompts#873
ckarnell wants to merge 1 commit into
PostHog:mainfrom
ckarnell:fix/prompts-default-cache-ttl-zero

Conversation

@ckarnell

Copy link
Copy Markdown
Contributor

--- LETTER BELOW THIS LINE ---
Prompts.__init__ resolves the default cache TTL with default_cache_ttl_seconds or DEFAULT_CACHE_TTL_SECONDS, so passing 0 is treated as if nothing was passed and the value becomes 300.

0 is a meaningful setting here, and the same class already honors it on the other path. _get_internal resolves the per-call TTL with cache_ttl_seconds if cache_ttl_seconds is not None else ..., and is_fresh = (now - fetched_at) < ttl with ttl=0 means "never serve from cache". So the per-call option lets you disable caching with 0 while the constructor default silently cannot.

Measured against the library, with no network: Prompts(default_cache_ttl_seconds=0) reports a TTL of 300. Counting fetch attempts against a seeded cache, the constructor 0 serves from cache (0 fetches) identically to passing no TTL at all, while the per-call get(cache_ttl_seconds=0) correctly refetches.

The fix matches the per-call form, +3/-1:

self._default_cache_ttl_seconds = (
    default_cache_ttl_seconds
    if default_cache_ttl_seconds is not None
    else DEFAULT_CACHE_TTL_SECONDS
)

I added a regression test that fails on the current code (AssertionError: 1 != 2) and passes with the fix. The existing suite is 68 passing before and 69 after, so this path was never covered. They test a TTL of 60. Never 0. None still yields 300 and default behavior does not change.

I did not run against a live PostHog instance, so all measurements use the repo's mocked session or a seeded cache.

@ckarnell
ckarnell requested a review from a team as a code owner August 14, 2026 13:36
@marandaneto
marandaneto requested a review from a team August 14, 2026 13:41
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