Skip to content

test(gemini): assert tool turns reach $ai_input as normalized blocks - #834

Closed
marco-g-pm wants to merge 1 commit into
mainfrom
claude/gemini-tool-capture-test
Closed

test(gemini): assert tool turns reach $ai_input as normalized blocks#834
marco-g-pm wants to merge 1 commit into
mainfrom
claude/gemini-tool-capture-test

Conversation

@marco-g-pm

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

Follow-up to #823, which fixed the Gemini tool block shapes for #725. That PR tested the converter. This one tests the payload PostHog actually receives.

The gap mattered. A converter test proves format_gemini_input returns the right dict. It does not prove that dict reaches $ai_input, survives the capture path, and stays JSON-serializable.

Changes

One test. It drives the whole path with a mocked google-genai client and real types.Content objects, then reads the $ai_input property handed to capture():

  • the text turn stays a text block
  • the model turn becomes {"type": "function", "function": {name, arguments}}
  • the tool-response turn becomes {"type": "function", "tool_name": ..., "content": ...}
  • the whole payload survives a JSON round trip

💚 How did you test it?

I ran the test against the pre-fix converter to confirm it catches the original bug:

At index 0 diff:
  {'type': 'function_call', 'function_call': {'args': {'city': 'SF'}, 'name': 'get_weather'}}
!={'type': 'function',      'function':      {'name': 'get_weather', 'arguments': {'city': 'SF'}}}

That is the exact defect from #725. A test that cannot fail is not worth adding.

On current main, posthog/test/ai/gemini/ gives 74 pass and 5 skip.

I also fed the captured payload through both downstream consumers:

  • The trace view guards sort it correctly. The call reads as a tool step. The response reads as a tool result, so isInternalToolResultUserMessage hides it instead of letting it leak as a user bubble.
  • The eval formatter prints the tool output as [FUNCTION] {'temp_c': 18}, which is what the judge needs.

No live Gemini key and no real project.

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file

Tests only, so there is nothing to release.

A gap this test surfaced

The eval formatter still prints a Gemini tool call as a raw dict dump. _format_special_block returns None for type: "function" and leaves it to the message-level tool_calls field, which this payload does not use. Anthropic tool_use and OpenAI tool_calls both print a clean call.

This predates #823. The SDK already used that block for $ai_output_choices, so Gemini tool calls have always printed this way for the judge. The fix belongs in message_formatter.py, not here.

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Written with Claude Code (Opus 5), directed by @marco-g-pm.

I wrote this after #823 merged, so it lands as its own PR. The end-to-end run is also what surfaced the eval formatter gap above, which no converter test could have shown.

The existing coverage stops at the converter. This drives the whole
capture path with a mocked google-genai client and real `types.Content`
objects, then asserts on the `$ai_input` property actually handed to
`capture()`, including that it survives a JSON round trip.

Verified it fails against the pre-fix converter with the exact defect
from #725: the tool call arrives as `{"type": "function_call",
"function_call": {...}}` instead of the shared block.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@marco-g-pm marco-g-pm self-assigned this Aug 5, 2026
@marco-g-pm marco-g-pm closed this Aug 5, 2026
@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-08-05 19:02:44 UTC
Duration: 256042ms

✅ All Tests Passed!

111/111 tests passed


Capture_V1 Tests

94/94 tests passed

View Details
Test Status Duration
Endpoint And Method.Targets V1 Endpoint 513ms
Endpoint And Method.Does Not Use Legacy Endpoints 509ms
Required Headers.Has Authorization Bearer Header 508ms
Required Headers.Has Content Type Json 509ms
Required Headers.Has Posthog Sdk Info Format 508ms
Required Headers.Has Posthog Attempt Header 508ms
Required Headers.Has Posthog Request Id 509ms
Required Headers.Has Posthog Request Timestamp 507ms
Required Headers.Has User Agent 509ms
Body Format.Body Has Created At And Batch 508ms
Body Format.No Api Key In Body 509ms
Body Format.No Sent At In Body 508ms
Event Format.Event Has Required Root Fields 508ms
Event Format.Event Uuid Is Valid 508ms
Event Format.Event Timestamp Is Rfc3339 508ms
Event Format.Distinct Id Is String 508ms
Event Format.Distinct Id At Root Not Properties 508ms
Event Format.Custom Properties Preserved 511ms
Event Format.Set Properties Preserved 508ms
Event Format.Set Once Properties Preserved 509ms
Event Format.Groups Properties Preserved 509ms
Event Format.Sdk Generates Uuid If Not Provided 508ms
Event Format.Event Has Required Root Fields Batch 511ms
Event Format.Event Uuid Is Valid Batch 510ms
Event Format.Event Timestamp Is Rfc3339 Batch 511ms
Event Format.Distinct Id Is String Batch 511ms
Event Format.Distinct Id At Root Not Properties Batch 511ms
Event Format.Custom Properties Preserved Batch 510ms
Event Format.Set Properties Preserved Batch 511ms
Event Format.Set Once Properties Preserved Batch 510ms
Event Format.Groups Properties Preserved Batch 511ms
Event Format.Sdk Generates Uuid If Not Provided Batch 511ms
Batch Behavior.Multiple Events In Single Batch 513ms
Batch Behavior.Batch Envelope Smoke 512ms
Batch Behavior.Flush With No Events Sends Nothing 505ms
Batch Behavior.Flush At Triggers Batch 1009ms
Batch Behavior.Created At Reflects Batch Creation Time 509ms
Deduplication.Generates Unique Uuids 513ms
Deduplication.Different Events Same Content Different Uuids 510ms
Deduplication.Preserves Uuid On Retry 6517ms
Deduplication.Preserves Timestamp On Retry 6518ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 6519ms
Deduplication.No Duplicate Events In Batch 515ms
Header Behavior On Retry.Attempt Header Starts At One 508ms
Header Behavior On Retry.Attempt Header Increments On Retry 13517ms
Header Behavior On Retry.Request Id Preserved On Retry 6515ms
Header Behavior On Retry.Different Requests Have Different Request Ids 3016ms
Header Behavior On Retry.Request Timestamp Changes On Retry 6517ms
Response Format Validation.Success Response Has Uuid Keyed Results 508ms
Response Format Validation.Success Response Has Ok For Each Event 512ms
Response Format Validation.Success No Retry After When All Ok 510ms
Response Format Validation.Success Retry After Present When Retry Events 1513ms
Response Format Validation.Success No Retry After When Drop Only 510ms
Response Format Validation.Response Echoes Request Id 509ms
Retry Behavior.Retries On 408 6516ms
Retry Behavior.Retries On 500 6517ms
Retry Behavior.Retries On 503 8520ms
Retry Behavior.Retries On 504 6517ms
Retry Behavior.Retryable Errors Have Retry After 3515ms
Retry Behavior.Respects Retry After On Retryable Error 11520ms
Retry Behavior.Does Not Retry On 400 2511ms
Retry Behavior.Does Not Retry On 401 2510ms
Retry Behavior.Does Not Retry On 402 2511ms
Retry Behavior.Does Not Retry On 413 2511ms
Retry Behavior.Does Not Retry On 415 2510ms
Retry Behavior.Non Retryable Errors Have No Retry After 2511ms
Retry Behavior.Implements Backoff 22533ms
Retry Behavior.Max Retries Respected 22533ms
Partial Batch Handling.Handles 200 Full Success 2511ms
Partial Batch Handling.Handles 200 With All Ok 3513ms
Partial Batch Handling.Does Not Retry Dropped Events 3511ms
Partial Batch Handling.Does Not Retry Limited Events 3510ms
Partial Batch Handling.Prunes Ok Events On Partial Retry 6519ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry 6518ms
Partial Batch Handling.Retries Only Retry Events From Partial 6519ms
Partial Batch Handling.Partial Retry Preserves Uuids 6515ms
Partial Batch Handling.Partial Retry Attempt Header Increments 6518ms
Partial Batch Handling.Partial Retry Request Id Preserved 6515ms
Partial Batch Handling.Respects Retry After On Partial 8518ms
Partial Batch Handling.Unknown Result Treated As Terminal 3511ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry 3516ms
Compression.Sends Gzip Content Encoding 509ms
Compression.No Content Encoding When Disabled 508ms
Compression.Compressed Body Is Decompressible 509ms
Error Handling.Does Not Retry On Unknown 4Xx 2510ms
Event Options.Cookieless Mode Override 508ms
Event Options.Disable Skew Correction Override 508ms
Event Options.Process Person Profile Override 509ms
Event Options.Product Tour Id Override 508ms
Event Options.Unset Options Omitted 508ms
Event Options.Options Override In Batch 510ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties 508ms
Geoip And Historical Migration.Historical Migration Set In Body 508ms
Geoip And Historical Migration.Historical Migration Absent By Default 508ms

Feature_Flags Tests

17/17 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 8ms
Request Payload.Flags Request Uses V2 Query Param 7ms
Request Payload.Flags Request Hits Flags Path Not Decide 7ms
Request Payload.Flags Request Omits Authorization Header 7ms
Request Payload.Token In Flags Body Matches Init 7ms
Request Payload.Groups Round Trip 7ms
Request Payload.Groups Default To Empty Object 6ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 8ms
Request Payload.Disable Geoip Omitted Defaults To False 6ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 7ms
Request Lifecycle.No Flags Request On Init Alone 3ms
Request Lifecycle.No Flags Request On Normal Capture 507ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 11ms
Request Lifecycle.Mock Response Value Is Returned To Caller 7ms
Retry Behavior.Retries Flags On 502 310ms
Retry Behavior.Retries Flags On 504 311ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 509ms

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.

1 participant