From a88ad2eaec90241530476fdfcb11022a72c1e5d7 Mon Sep 17 00:00:00 2001 From: Cohen Karnell <7363269+ckarnell@users.noreply.github.com> Date: Fri, 14 Aug 2026 09:31:02 -0400 Subject: [PATCH 1/2] fix(ai): honor a default_cache_ttl_seconds of 0 --- posthog/ai/prompts.py | 4 +++- posthog/test/ai/test_prompts.py | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/posthog/ai/prompts.py b/posthog/ai/prompts.py index 0e5ef38b..c83483a4 100644 --- a/posthog/ai/prompts.py +++ b/posthog/ai/prompts.py @@ -173,7 +173,9 @@ def __init__( failures are reported to PostHog error tracking via capture_exception(). """ self._default_cache_ttl_seconds = ( - default_cache_ttl_seconds or DEFAULT_CACHE_TTL_SECONDS + default_cache_ttl_seconds + if default_cache_ttl_seconds is not None + else DEFAULT_CACHE_TTL_SECONDS ) self._cache: Dict[PromptCacheKey, CachedPrompt] = {} self._has_warned_deprecation = False diff --git a/posthog/test/ai/test_prompts.py b/posthog/test/ai/test_prompts.py index 15d4b911..df9d9ed7 100644 --- a/posthog/test/ai/test_prompts.py +++ b/posthog/test/ai/test_prompts.py @@ -512,6 +512,26 @@ def test_use_custom_default_cache_ttl_from_constructor( prompts.get("test-prompt", with_metadata=False) self.assertEqual(mock_get.call_count, 2) + @patch("posthog.ai.prompts._get_session") + @patch("posthog.ai.prompts.time.time") + def test_default_cache_ttl_seconds_zero_disables_caching( + self, mock_time, mock_get_session + ): + """A default TTL of 0 should disable caching, matching the per-call option.""" + mock_get = mock_get_session.return_value.get + mock_get.return_value = MockResponse(json_data=self.mock_prompt_response) + mock_time.return_value = 1000.0 + + posthog = self.create_mock_posthog() + prompts = Prompts(posthog, default_cache_ttl_seconds=0) + + prompts.get("test-prompt", with_metadata=False) + self.assertEqual(mock_get.call_count, 1) + + # No time has passed, and a TTL of 0 still means every read refetches. + prompts.get("test-prompt", with_metadata=False) + self.assertEqual(mock_get.call_count, 2) + @patch("posthog.ai.prompts._get_session") def test_url_encode_prompt_names_with_special_characters(self, mock_get_session): """Should URL-encode prompt names with special characters.""" From c8946e99318f9afe818da4007941505e0551d3fc Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Sat, 15 Aug 2026 08:21:28 +0200 Subject: [PATCH 2/2] chore: add changeset for prompt cache TTL fix --- .sampo/changesets/gallant-queen-vainamoinen.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .sampo/changesets/gallant-queen-vainamoinen.md diff --git a/.sampo/changesets/gallant-queen-vainamoinen.md b/.sampo/changesets/gallant-queen-vainamoinen.md new file mode 100644 index 00000000..3b7bf481 --- /dev/null +++ b/.sampo/changesets/gallant-queen-vainamoinen.md @@ -0,0 +1,5 @@ +--- +pypi/posthog: patch +--- + +Honor `default_cache_ttl_seconds=0` in AI prompts so callers can disable default prompt caching.