Skip to content

feat(ai): capture gemini thought summaries as thinking blocks - #827

Merged
fasyy612 merged 4 commits into
mainfrom
posthog-code/gemini-thinking-blocks
Sep 11, 2026
Merged

fasyy612 merged 4 commits into
mainfrom
posthog-code/gemini-thinking-blocks

Conversation

@fivestarspicy

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

Gemini can return thought summaries when a request sets thinking_config.include_thoughts=True, and the SDK already reports thoughts_token_count as reasoning tokens — but the thought content was formatted as ordinary {"type": "text"} blocks, indistinguishable from the answer in LLM analytics. The Anthropic converter already emits {"type": "thinking", "thinking": ...} blocks, which the PostHog UI renders as reasoning; this brings the Gemini converter in line.

Parts marked thought=True now become thinking blocks in all three formatting paths: responses, inputs (multi-turn callers append prior model content, thoughts included, back into the conversation), and streaming chunks.

Companion PostHog PR that makes a thinking block next to a Gemini-style {"type": "function"} tool-call block render correctly: PostHog/posthog#76883

💚 How did you test it?

  • New tests in posthog/test/ai/gemini/test_gemini_converter.py covering the response, input, and streaming-chunk paths (each is a separate code path in the converter).
  • Full Gemini suite: 76 passed. ruff format --check, ruff check, and mypy (baseline-filtered) all clean.
  • The response-path check uses is True on part.thought so loosely-specced mocks (whose .thought is a truthy Mock) still land on the text branch — the existing mock-based wrapper tests stay green.

📝 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

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

  • Authored with Claude Code (PostHog Code cloud task), driven by instrumenting a Gemini-based feature in the PostHog app whose reasoning wasn't visible in LLM analytics.
  • Verified against the PostHog AIO frontend normalizer that {"type": "thinking", "thinking": ...} is the block shape rendered as reasoning (the Anthropic recipe matches it per-block, provider-agnostically).
  • Changeset added as minor: new capture behavior, only active when callers opt into include_thoughts.

Created with PostHog Code

Gemini parts marked thought=True were formatted as plain text blocks, so
reasoning was indistinguishable from the answer in LLM analytics. They now
become {"type": "thinking", "thinking": ...} blocks — the same shape the
Anthropic converter emits and the PostHog UI renders as reasoning — across
the response, input, and streaming-chunk paths.

Generated-By: PostHog Code
Task-Id: a7eb2477-e1b0-4c68-8933-cfb0b2f6b2b0
@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-09-11 15:59:05 UTC
Duration: 256357ms

✅ 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 517ms
Endpoint And Method.Does Not Use Legacy Endpoints 510ms
Required Headers.Has Authorization Bearer Header 509ms
Required Headers.Has Content Type Json 511ms
Required Headers.Has Posthog Sdk Info Format 510ms
Required Headers.Has Posthog Attempt Header 510ms
Required Headers.Has Posthog Request Id 511ms
Required Headers.Has Posthog Request Timestamp 510ms
Required Headers.Has User Agent 510ms
Body Format.Body Has Created At And Batch 510ms
Body Format.No Api Key In Body 512ms
Body Format.No Sent At In Body 510ms
Event Format.Event Has Required Root Fields 510ms
Event Format.Event Uuid Is Valid 510ms
Event Format.Event Timestamp Is Rfc3339 510ms
Event Format.Distinct Id Is String 510ms
Event Format.Distinct Id At Root Not Properties 511ms
Event Format.Custom Properties Preserved 510ms
Event Format.Set Properties Preserved 510ms
Event Format.Set Once Properties Preserved 509ms
Event Format.Groups Properties Preserved 511ms
Event Format.Sdk Generates Uuid If Not Provided 512ms
Event Format.Event Has Required Root Fields Batch 513ms
Event Format.Event Uuid Is Valid Batch 514ms
Event Format.Event Timestamp Is Rfc3339 Batch 513ms
Event Format.Distinct Id Is String Batch 514ms
Event Format.Distinct Id At Root Not Properties Batch 514ms
Event Format.Custom Properties Preserved Batch 516ms
Event Format.Set Properties Preserved Batch 514ms
Event Format.Set Once Properties Preserved Batch 513ms
Event Format.Groups Properties Preserved Batch 513ms
Event Format.Sdk Generates Uuid If Not Provided Batch 514ms
Batch Behavior.Multiple Events In Single Batch 517ms
Batch Behavior.Batch Envelope Smoke 516ms
Batch Behavior.Flush With No Events Sends Nothing 506ms
Batch Behavior.Flush At Triggers Batch 1011ms
Batch Behavior.Created At Reflects Batch Creation Time 510ms
Deduplication.Generates Unique Uuids 518ms
Deduplication.Different Events Same Content Different Uuids 512ms
Deduplication.Preserves Uuid On Retry 6518ms
Deduplication.Preserves Timestamp On Retry 6519ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 6519ms
Deduplication.No Duplicate Events In Batch 518ms
Header Behavior On Retry.Attempt Header Starts At One 511ms
Header Behavior On Retry.Attempt Header Increments On Retry 13522ms
Header Behavior On Retry.Request Id Preserved On Retry 6515ms
Header Behavior On Retry.Different Requests Have Different Request Ids 3020ms
Header Behavior On Retry.Request Timestamp Changes On Retry 6520ms
Response Format Validation.Success Response Has Uuid Keyed Results 512ms
Response Format Validation.Success Response Has Ok For Each Event 514ms
Response Format Validation.Success No Retry After When All Ok 512ms
Response Format Validation.Success Retry After Present When Retry Events 1516ms
Response Format Validation.Success No Retry After When Drop Only 513ms
Response Format Validation.Response Echoes Request Id 510ms
Retry Behavior.Retries On 408 6516ms
Retry Behavior.Retries On 500 6520ms
Retry Behavior.Retries On 503 8523ms
Retry Behavior.Retries On 504 6520ms
Retry Behavior.Retryable Errors Have Retry After 3515ms
Retry Behavior.Respects Retry After On Retryable Error 11523ms
Retry Behavior.Does Not Retry On 400 2511ms
Retry Behavior.Does Not Retry On 401 2513ms
Retry Behavior.Does Not Retry On 402 2513ms
Retry Behavior.Does Not Retry On 413 2511ms
Retry Behavior.Does Not Retry On 415 2513ms
Retry Behavior.Non Retryable Errors Have No Retry After 2513ms
Retry Behavior.Implements Backoff 22533ms
Retry Behavior.Max Retries Respected 22534ms
Partial Batch Handling.Handles 200 Full Success 2513ms
Partial Batch Handling.Handles 200 With All Ok 3516ms
Partial Batch Handling.Does Not Retry Dropped Events 3516ms
Partial Batch Handling.Does Not Retry Limited Events 3515ms
Partial Batch Handling.Prunes Ok Events On Partial Retry 6522ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry 6517ms
Partial Batch Handling.Retries Only Retry Events From Partial 6523ms
Partial Batch Handling.Partial Retry Preserves Uuids 6522ms
Partial Batch Handling.Partial Retry Attempt Header Increments 6517ms
Partial Batch Handling.Partial Retry Request Id Preserved 6521ms
Partial Batch Handling.Respects Retry After On Partial 8518ms
Partial Batch Handling.Unknown Result Treated As Terminal 3513ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry 3516ms
Compression.Sends Gzip Content Encoding 511ms
Compression.No Content Encoding When Disabled 510ms
Compression.Compressed Body Is Decompressible 511ms
Error Handling.Does Not Retry On Unknown 4Xx 2512ms
Event Options.Cookieless Mode Override 511ms
Event Options.Disable Skew Correction Override 509ms
Event Options.Process Person Profile Override 510ms
Event Options.Product Tour Id Override 511ms
Event Options.Unset Options Omitted 509ms
Event Options.Options Override In Batch 514ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties 510ms
Geoip And Historical Migration.Historical Migration Set In Body 509ms
Geoip And Historical Migration.Historical Migration Absent By Default 510ms

Feature_Flags Tests

17/17 tests passed

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

@fivestarspicy
fivestarspicy marked this pull request as ready for review August 3, 2026 22:15
@fivestarspicy
fivestarspicy requested a review from a team as a code owner August 3, 2026 22:15
@fivestarspicy
fivestarspicy requested a review from a team August 3, 2026 22:15
@github-actions

Copy link
Copy Markdown
Contributor

This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, post a comment or remove the stale label – otherwise this will be closed in another week.

@github-actions github-actions Bot added the stale label Aug 17, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR was closed due to lack of activity. Feel free to reopen if it's still relevant.

@github-actions github-actions Bot closed this Aug 24, 2026
@fasyy612 fasyy612 reopened this Sep 10, 2026
@fasyy612 fasyy612 removed the stale label Sep 10, 2026
@fasyy612
fasyy612 merged commit c3d3c96 into main Sep 11, 2026
42 checks passed
@fasyy612
fasyy612 deleted the posthog-code/gemini-thinking-blocks branch September 11, 2026 16:22
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.

4 participants