Skip to content

Commit c07d1e8

Browse files
fix: gemini system prompt capture
1 parent f09f639 commit c07d1e8

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

posthog/ai/utils.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,28 @@ def merge_system_prompt(kwargs: Dict[str, Any], provider: str) -> List[Formatted
173173

174174
contents = kwargs.get("contents", [])
175175
formatted_messages = format_gemini_input(contents)
176-
177-
if kwargs.get("system_instruction") is not None:
178-
system_instruction = kwargs.get("system_instruction")
179-
system_message = cast(FormattedMessage, {
180-
"role": "system",
181-
"content": system_instruction
182-
})
183-
formatted_messages = [system_message] + list(formatted_messages)
176+
177+
# Check if system instruction is provided in config parameter
178+
config = kwargs.get("config")
179+
system_instruction = None
180+
181+
if config is not None:
182+
# Handle different config formats
183+
if hasattr(config, "system_instruction"):
184+
system_instruction = config.system_instruction
185+
elif isinstance(config, dict) and "system_instruction" in config:
186+
system_instruction = config["system_instruction"]
187+
elif isinstance(config, dict) and "systemInstruction" in config:
188+
system_instruction = config["systemInstruction"]
189+
190+
if system_instruction is not None:
191+
has_system = any(msg.get("role") == "system" for msg in formatted_messages)
192+
if not has_system:
193+
system_message = cast(FormattedMessage, {
194+
"role": "system",
195+
"content": system_instruction
196+
})
197+
formatted_messages = [system_message] + list(formatted_messages)
184198

185199
return formatted_messages
186200
elif provider == "openai":

posthog/test/ai/test_system_prompts.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def test_gemini_contents_array_system_prompt(self):
231231
self._assert_system_prompt_captured(properties["$ai_input"])
232232

233233
def test_gemini_system_instruction_parameter(self):
234-
"""Test Gemini with system_instruction parameter."""
234+
"""Test Gemini with system_instruction in config parameter."""
235235
try:
236236
from posthog.ai.gemini import Client
237237
except ImportError:
@@ -256,10 +256,10 @@ def test_gemini_system_instruction_parameter(self):
256256
client = Client(posthog_client=self.client, api_key="test")
257257

258258
contents = [{"role": "user", "content": self.test_user_message}]
259+
config = {"system_instruction": self.test_system_prompt}
259260

260261
client.models.generate_content(
261-
model="gemini-2.0-flash", contents=contents,
262-
system_instruction=self.test_system_prompt, posthog_distinct_id="test-user"
262+
model="gemini-2.0-flash", contents=contents, config=config, posthog_distinct_id="test-user"
263263
)
264264

265265
self.assertEqual(len(self.client.capture.call_args_list), 1)

0 commit comments

Comments
 (0)