Skip to content

Commit 1f784bb

Browse files
committed
fix: keep async capture_ai from raising on a non-string event name
1 parent a43e49f commit 1f784bb

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎posthog/async_client.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ def capture_ai(
587587
return self._capture(event, self._ai_lane, kwargs)
588588

589589
def _log_non_ai_event(self, event: str) -> None:
590-
if not event.startswith("$ai_"):
590+
if isinstance(event, str) and not event.startswith("$ai_"):
591591
self.log.debug(
592592
"capture_ai called with non-AI event name %r; routing it to the AI endpoint anyway.",
593593
event,

‎posthog/test/test_async_client.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,19 @@ async def send_batch(api_key, host, batch, **kwargs):
374374
assert client._all_worker_tasks() == []
375375

376376

377+
@pytest.mark.asyncio
378+
@pytest.mark.parametrize("event", [None, 123])
379+
async def test_ai_capture_handles_a_non_string_event_name_like_capture(event):
380+
client = AsyncPosthog("test-key", send=False)
381+
try:
382+
expected = client.capture(event, distinct_id="user-1") is None
383+
assert (client.capture_ai(event, distinct_id="user-1") is None) is expected
384+
immediate = await client.capture_ai_immediate(event, distinct_id="user-1")
385+
assert (immediate is None) is expected
386+
finally:
387+
await client.shutdown()
388+
389+
377390
@pytest.mark.asyncio
378391
async def test_send_false_accepts_without_starting_workers_or_transport():
379392
with mock.patch("posthog.async_client._build_client") as build_client:

0 commit comments

Comments
 (0)