Skip to content

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

Open
fivestarspicy wants to merge 2 commits into
mainfrom
posthog-code/gemini-thinking-blocks
Open

feat(ai): capture gemini thought summaries as thinking blocks#827
fivestarspicy wants to merge 2 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-actionsBot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-08-07 17:11:37 UTC
Duration: 256520ms

✅ All Tests Passed!

111/111 tests passed


Capture_V1 Tests

94/94 tests passed

View Details
TestStatusDuration
Endpoint And Method.Targets V1 Endpoint519ms
Endpoint And Method.Does Not Use Legacy Endpoints512ms
Required Headers.Has Authorization Bearer Header512ms
Required Headers.Has Content Type Json511ms
Required Headers.Has Posthog Sdk Info Format512ms
Required Headers.Has Posthog Attempt Header512ms
Required Headers.Has Posthog Request Id511ms
Required Headers.Has Posthog Request Timestamp511ms
Required Headers.Has User Agent511ms
Body Format.Body Has Created At And Batch511ms
Body Format.No Api Key In Body513ms
Body Format.No Sent At In Body511ms
Event Format.Event Has Required Root Fields512ms
Event Format.Event Uuid Is Valid512ms
Event Format.Event Timestamp Is Rfc3339511ms
Event Format.Distinct Id Is String510ms
Event Format.Distinct Id At Root Not Properties511ms
Event Format.Custom Properties Preserved511ms
Event Format.Set Properties Preserved511ms
Event Format.Set Once Properties Preserved511ms
Event Format.Groups Properties Preserved512ms
Event Format.Sdk Generates Uuid If Not Provided510ms
Event Format.Event Has Required Root Fields Batch514ms
Event Format.Event Uuid Is Valid Batch515ms
Event Format.Event Timestamp Is Rfc3339 Batch514ms
Event Format.Distinct Id Is String Batch514ms
Event Format.Distinct Id At Root Not Properties Batch516ms
Event Format.Custom Properties Preserved Batch514ms
Event Format.Set Properties Preserved Batch514ms
Event Format.Set Once Properties Preserved Batch515ms
Event Format.Groups Properties Preserved Batch514ms
Event Format.Sdk Generates Uuid If Not Provided Batch515ms
Batch Behavior.Multiple Events In Single Batch518ms
Batch Behavior.Batch Envelope Smoke516ms
Batch Behavior.Flush With No Events Sends Nothing507ms
Batch Behavior.Flush At Triggers Batch1012ms
Batch Behavior.Created At Reflects Batch Creation Time512ms
Deduplication.Generates Unique Uuids519ms
Deduplication.Different Events Same Content Different Uuids513ms
Deduplication.Preserves Uuid On Retry6520ms
Deduplication.Preserves Timestamp On Retry6517ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry6523ms
Deduplication.No Duplicate Events In Batch520ms
Header Behavior On Retry.Attempt Header Starts At One511ms
Header Behavior On Retry.Attempt Header Increments On Retry13529ms
Header Behavior On Retry.Request Id Preserved On Retry6521ms
Header Behavior On Retry.Different Requests Have Different Request Ids3022ms
Header Behavior On Retry.Request Timestamp Changes On Retry6519ms
Response Format Validation.Success Response Has Uuid Keyed Results512ms
Response Format Validation.Success Response Has Ok For Each Event515ms
Response Format Validation.Success No Retry After When All Ok513ms
Response Format Validation.Success Retry After Present When Retry Events1518ms
Response Format Validation.Success No Retry After When Drop Only514ms
Response Format Validation.Response Echoes Request Id511ms
Retry Behavior.Retries On 4086520ms
Retry Behavior.Retries On 5006522ms
Retry Behavior.Retries On 5038525ms
Retry Behavior.Retries On 5046521ms
Retry Behavior.Retryable Errors Have Retry After3518ms
Retry Behavior.Respects Retry After On Retryable Error11518ms
Retry Behavior.Does Not Retry On 4002515ms
Retry Behavior.Does Not Retry On 4012513ms
Retry Behavior.Does Not Retry On 4022514ms
Retry Behavior.Does Not Retry On 4132514ms
Retry Behavior.Does Not Retry On 4152514ms
Retry Behavior.Non Retryable Errors Have No Retry After2515ms
Retry Behavior.Implements Backoff22526ms
Retry Behavior.Max Retries Respected22528ms
Partial Batch Handling.Handles 200 Full Success2511ms
Partial Batch Handling.Handles 200 With All Ok3519ms
Partial Batch Handling.Does Not Retry Dropped Events3515ms
Partial Batch Handling.Does Not Retry Limited Events3514ms
Partial Batch Handling.Prunes Ok Events On Partial Retry6519ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry6522ms
Partial Batch Handling.Retries Only Retry Events From Partial6523ms
Partial Batch Handling.Partial Retry Preserves Uuids6523ms
Partial Batch Handling.Partial Retry Attempt Header Increments6523ms
Partial Batch Handling.Partial Retry Request Id Preserved6522ms
Partial Batch Handling.Respects Retry After On Partial8521ms
Partial Batch Handling.Unknown Result Treated As Terminal3514ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry3523ms
Compression.Sends Gzip Content Encoding512ms
Compression.No Content Encoding When Disabled511ms
Compression.Compressed Body Is Decompressible511ms
Error Handling.Does Not Retry On Unknown 4Xx2512ms
Event Options.Cookieless Mode Override512ms
Event Options.Disable Skew Correction Override510ms
Event Options.Process Person Profile Override512ms
Event Options.Product Tour Id Override510ms
Event Options.Unset Options Omitted511ms
Event Options.Options Override In Batch515ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties510ms
Geoip And Historical Migration.Historical Migration Set In Body511ms
Geoip And Historical Migration.Historical Migration Absent By Default512ms

Feature_Flags Tests

17/17 tests passed

View Details
TestStatusDuration
Request Payload.Request With Person Properties Device Id14ms
Request Payload.Flags Request Uses V2 Query Param10ms
Request Payload.Flags Request Hits Flags Path Not Decide10ms
Request Payload.Flags Request Omits Authorization Header10ms
Request Payload.Token In Flags Body Matches Init10ms
Request Payload.Groups Round Trip10ms
Request Payload.Groups Default To Empty Object12ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False10ms
Request Payload.Disable Geoip Omitted Defaults To False11ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key11ms
Request Lifecycle.No Flags Request On Init Alone4ms
Request Lifecycle.No Flags Request On Normal Capture510ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests15ms
Request Lifecycle.Mock Response Value Is Returned To Caller10ms
Retry Behavior.Retries Flags On 502314ms
Retry Behavior.Retries Flags On 504315ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event512ms

@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 ownerAugust 3, 2026 22:15
@fivestarspicy
fivestarspicy requested a review from a teamAugust 3, 2026 22:15
Sign up for freeto 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

@fivestarspicy@marandaneto@Radu-Raicea