Skip to content

Conversation

@Twixes
Copy link
Member

@Twixes Twixes commented Jul 21, 2025

It's been inconvenient to have to explicitly pass a posthog_client to every AI provider wrapper. We should do what happens when you call posthog methods like posthog.capture() – run setup() to init the default_client and use default_client.

This is what this PR introduces.

@Twixes Twixes requested a review from k11kirky July 21, 2025 17:01
@Twixes Twixes force-pushed the ai-optional-client branch from aad169c to 20a7b57 Compare July 21, 2025 17:01
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greptile Summary

This PR modifies all AI provider wrapper classes (OpenAI, Anthropic, and their variants) to make the posthog_client parameter optional in their constructors. Instead of requiring explicit client initialization, the wrappers now follow PostHog's standard pattern where if no client is provided, they automatically call posthog.setup() to initialize and use the default_client. The change affects all AI provider implementations including async versions.

This improvement aligns the AI wrappers with PostHog's core functionality pattern (like posthog.capture()), making the library more ergonomic while maintaining the flexibility to use custom clients when needed.

Confidence score: 5/5

  1. This PR is very safe to merge as it adds convenience without breaking existing functionality
  2. The implementation is consistent across all providers, uses proper type safety with cast(), and follows established PostHog patterns
  3. Key files that warrant attention:
    • posthog/ai/openai/openai.py
    • posthog/ai/anthropic/anthropic.py
    • Corresponding async implementations

6 files reviewed, 3 comments

Edit Code Review Bot Settings | Greptile

Copy link
Contributor

@k11kirky k11kirky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Added to gemini as well, Realized i need to fix the docs as well and support this for node.

k11kirky and others added 2 commits July 21, 2025 15:32
@Twixes Twixes force-pushed the ai-optional-client branch from dcbd8ac to 8c16f3f Compare July 21, 2025 22:42
@Twixes Twixes enabled auto-merge (squash) July 22, 2025 06:51
@Twixes Twixes merged commit 7a8b091 into master Jul 22, 2025
10 checks passed
@Twixes Twixes deleted the ai-optional-client branch July 22, 2025 06:53
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.

3 participants