Skip to content

Commit fda06d8

Browse files
committed
fix: graceful fallback in claude_agent_sdk query wrapper when PostHog not configured
When PostHog is not configured (no API key), the query() convenience function now catches the initialization error and falls back to the plain claude_agent_sdk.query() with a warning log instead of crashing. The wrapper should be fail-safe — instrumentation is additive and must never break the underlying SDK call.
1 parent 6a660b0 commit fda06d8

2 files changed

Lines changed: 37 additions & 7 deletions

File tree

posthog/ai/claude_agent_sdk/__init__.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import logging
34
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Union
45

56
if TYPE_CHECKING:
@@ -17,6 +18,8 @@
1718
from posthog.ai.claude_agent_sdk.client import PostHogClaudeSDKClient
1819
from posthog.ai.claude_agent_sdk.processor import PostHogClaudeAgentProcessor
1920

21+
log = logging.getLogger("posthog")
22+
2023
__all__ = [
2124
"PostHogClaudeAgentProcessor",
2225
"PostHogClaudeSDKClient",
@@ -113,13 +116,21 @@ async def query(
113116
print(message)
114117
```
115118
"""
116-
processor = PostHogClaudeAgentProcessor(
117-
client=posthog_client,
118-
distinct_id=posthog_distinct_id,
119-
privacy_mode=posthog_privacy_mode,
120-
groups=posthog_groups,
121-
properties={},
122-
)
119+
from claude_agent_sdk import query as original_query
120+
121+
try:
122+
processor = PostHogClaudeAgentProcessor(
123+
client=posthog_client,
124+
distinct_id=posthog_distinct_id,
125+
privacy_mode=posthog_privacy_mode,
126+
groups=posthog_groups,
127+
properties={},
128+
)
129+
except Exception as e:
130+
log.warning("PostHog instrumentation disabled: %s — falling back to plain claude_agent_sdk.query()", e)
131+
async for message in original_query(prompt=prompt, options=options, transport=transport):
132+
yield message
133+
return
123134

124135
async for message in processor.query(
125136
prompt=prompt,

posthog/test/ai/claude_agent_sdk/test_processor.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from posthog.ai.claude_agent_sdk import (
2020
PostHogClaudeAgentProcessor,
2121
instrument,
22+
query as posthog_query,
2223
)
2324

2425
CLAUDE_AGENT_SDK_AVAILABLE = True
@@ -580,3 +581,21 @@ async def fake_query_capture(**kwargs):
580581
pass
581582

582583
assert captured_options.get("options").include_partial_messages is True
584+
585+
586+
class TestQueryGracefulFallback:
587+
@pytest.mark.asyncio
588+
async def test_falls_back_to_original_query_when_posthog_not_configured(self):
589+
result_msg = _make_result()
590+
messages_from_sdk = [result_msg]
591+
592+
with (
593+
patch("posthog.ai.claude_agent_sdk.PostHogClaudeAgentProcessor", side_effect=ValueError("API key is required")),
594+
patch("claude_agent_sdk.query", side_effect=lambda **kwargs: _fake_query(messages_from_sdk)),
595+
):
596+
collected = []
597+
async for msg in posthog_query(prompt="Hello", options=ClaudeAgentOptions()):
598+
collected.append(msg)
599+
600+
assert len(collected) == 1
601+
assert collected[0] is result_msg

0 commit comments

Comments
 (0)