-
Notifications
You must be signed in to change notification settings - Fork 3
feat: Add Pydantic AI provider with PostHog OTel instrumentation #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andrewm4894
wants to merge
1
commit into
main
Choose a base branch
from
feature/pydantic-ai-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| """ | ||
| Pydantic AI provider with PostHog instrumentation via OpenTelemetry. | ||
|
|
||
| This provider uses Pydantic AI's native OTel instrumentation, translated to PostHog | ||
| events via the PostHogSpanExporter. | ||
| """ | ||
|
|
||
| import os | ||
| from typing import Optional | ||
| from posthog import Posthog | ||
|
|
||
| from .base import BaseProvider | ||
| from .constants import ( | ||
| OPENAI_CHAT_MODEL, | ||
| OPENAI_VISION_MODEL, | ||
| DEFAULT_POSTHOG_DISTINCT_ID, | ||
| SYSTEM_PROMPT_FRIENDLY | ||
| ) | ||
|
|
||
|
|
||
| class PydanticAIProvider(BaseProvider): | ||
| """Pydantic AI provider with PostHog OpenTelemetry instrumentation.""" | ||
|
|
||
| _instrumented = False # Class-level flag to prevent double instrumentation | ||
|
|
||
| def __init__(self, posthog_client: Posthog): | ||
| # Import pydantic-ai here to avoid import errors if not installed | ||
| try: | ||
| from pydantic_ai import Agent | ||
| except ImportError: | ||
| raise ImportError( | ||
| "pydantic-ai is required for this provider. " | ||
| "Install it with: pip install pydantic-ai" | ||
| ) | ||
|
|
||
| super().__init__(posthog_client) | ||
|
|
||
| # Extract distinct_id from environment or use default | ||
| self.distinct_id = os.getenv("POSTHOG_DISTINCT_ID", DEFAULT_POSTHOG_DISTINCT_ID) | ||
|
|
||
| # Extract session ID from super_properties if available | ||
| existing_props = posthog_client.super_properties or {} | ||
| self.ai_session_id = existing_props.get("$ai_session_id") | ||
|
|
||
| # Set up PostHog instrumentation for Pydantic AI (once globally) | ||
| if not PydanticAIProvider._instrumented: | ||
| self._setup_instrumentation(posthog_client) | ||
| PydanticAIProvider._instrumented = True | ||
|
|
||
| # Configure model | ||
| self.model = f"openai:{OPENAI_CHAT_MODEL}" | ||
|
|
||
| # Create the agent with tools | ||
| self.agent = self._create_agent() | ||
|
|
||
| # Store conversation history for multi-turn conversations | ||
| self._message_history = None | ||
|
|
||
| def _setup_instrumentation(self, posthog_client: Posthog): | ||
| """Set up PostHog instrumentation for Pydantic AI.""" | ||
| try: | ||
| from posthog.ai.pydantic_ai import instrument_pydantic_ai | ||
| except ImportError: | ||
| raise ImportError( | ||
| "PostHog pydantic-ai integration is required. " | ||
| "Make sure you're using a posthog version with pydantic-ai support." | ||
| ) | ||
|
|
||
| # Build additional properties | ||
| properties = {} | ||
| if self.ai_session_id: | ||
| properties["$ai_session_id"] = self.ai_session_id | ||
|
|
||
| # Instrument all Pydantic AI agents with PostHog | ||
| instrument_pydantic_ai( | ||
| client=posthog_client, | ||
| distinct_id=self.distinct_id, | ||
| privacy_mode=False, # Include content in traces | ||
| properties=properties, | ||
| debug=self.debug_mode, | ||
| ) | ||
|
|
||
| if self.debug_mode: | ||
| print("✅ Pydantic AI instrumentation configured for PostHog") | ||
|
|
||
| def _create_agent(self): | ||
| """Create a Pydantic AI agent with tools.""" | ||
| from pydantic_ai import Agent | ||
|
|
||
| # Create agent with system prompt | ||
| agent = Agent( | ||
| self.model, | ||
| system_prompt=SYSTEM_PROMPT_FRIENDLY, | ||
| ) | ||
|
|
||
| # Register tools using decorator | ||
| @agent.tool_plain | ||
| def get_weather(latitude: float, longitude: float, location_name: str) -> str: | ||
| """Get the current weather for a specific location using geographical coordinates. | ||
|
|
||
| Args: | ||
| latitude: The latitude of the location (e.g., 37.7749 for San Francisco) | ||
| longitude: The longitude of the location (e.g., -122.4194 for San Francisco) | ||
| location_name: A human-readable name for the location (e.g., 'San Francisco, CA') | ||
| """ | ||
| return self.get_weather(latitude, longitude, location_name) | ||
|
|
||
| @agent.tool_plain | ||
| def tell_joke(setup: str, punchline: str) -> str: | ||
| """Tell a joke with a question-style setup and an answer punchline. | ||
|
|
||
| Args: | ||
| setup: The setup or question part of the joke | ||
| punchline: The punchline or answer part of the joke | ||
| """ | ||
| return self.tell_joke(setup, punchline) | ||
|
|
||
| return agent | ||
|
|
||
| def get_tool_definitions(self): | ||
| """Return tool definitions (for interface compatibility). | ||
|
|
||
| Note: Pydantic AI tools are defined via decorators, so this returns | ||
| a description of the available tools rather than provider-specific format. | ||
| """ | ||
| return [ | ||
| { | ||
| "name": "get_weather", | ||
| "description": "Get the current weather for a specific location using geographical coordinates", | ||
| }, | ||
| { | ||
| "name": "tell_joke", | ||
| "description": "Tell a joke with a question-style setup and an answer punchline", | ||
| }, | ||
| ] | ||
|
|
||
| def get_name(self): | ||
| return "Pydantic AI (OpenTelemetry)" | ||
|
|
||
| def chat(self, user_input: str, base64_image: Optional[str] = None) -> str: | ||
| """Send a message to the Pydantic AI agent and get response. | ||
|
|
||
| Args: | ||
| user_input: The user's message | ||
| base64_image: Optional base64-encoded image (not currently supported) | ||
|
|
||
| Returns: | ||
| The agent's response as a string | ||
| """ | ||
| if base64_image: | ||
| # Pydantic AI image handling would require additional setup | ||
| return "❌ Image input is not yet supported with Pydantic AI provider" | ||
|
|
||
| try: | ||
| # Run the agent with conversation history for multi-turn support | ||
| result = self.agent.run_sync( | ||
| user_input, | ||
| message_history=self._message_history, | ||
| ) | ||
|
|
||
| # Store message history for next turn | ||
| self._message_history = result.all_messages() | ||
|
|
||
| # Debug logging | ||
| if self.debug_mode: | ||
| self._debug_log("Pydantic AI Result", { | ||
| "output": str(result.output), | ||
| "all_messages_count": len(result.all_messages()), | ||
| }) | ||
|
|
||
| # Return the result output (the agent's response) | ||
| return str(result.output) | ||
|
|
||
| except Exception as e: | ||
| error_msg = f"❌ Error: {str(e)}" | ||
| if self.debug_mode: | ||
| import traceback | ||
| self._debug_log("Pydantic AI Error", traceback.format_exc()) | ||
| return error_msg | ||
|
|
||
| def reset_conversation(self): | ||
| """Reset the conversation by clearing message history.""" | ||
| self._message_history = None | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_setup_instrumentationimportsposthog.ai.pydantic_ai.instrument_pydantic_aiand raises immediately if it is absent, butpython/requirements.txtstill pinsposthog>=6.6.1, which does not ship that module (the commit text even calls it an “upcoming PR”). With the current dependency set, selecting provider 13 or runningrun_all_testswill hit this ImportError and the provider cannot be used at all. Pin PostHog to a release that contains the pydantic AI instrumentation or guard the provider until that version is available.Useful? React with 👍 / 👎.