posthog 8.2.1__tar.gz → 8.2.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (306) hide show
  1. {posthog-8.2.1/posthog.egg-info → posthog-8.2.3}/PKG-INFO +1 -1
  2. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/claude_agent_sdk/client.py +34 -12
  3. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/claude_agent_sdk/processor.py +209 -89
  4. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/langchain/callbacks.py +73 -4
  5. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/claude_agent_sdk/test_client.py +89 -0
  6. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/claude_agent_sdk/test_processor.py +157 -0
  7. posthog-8.2.3/posthog/version.py +1 -0
  8. {posthog-8.2.1 → posthog-8.2.3/posthog.egg-info}/PKG-INFO +1 -1
  9. {posthog-8.2.1 → posthog-8.2.3}/pyproject.toml +1 -1
  10. posthog-8.2.1/posthog/version.py +0 -1
  11. {posthog-8.2.1 → posthog-8.2.3}/LICENSE +0 -0
  12. {posthog-8.2.1 → posthog-8.2.3}/MANIFEST.in +0 -0
  13. {posthog-8.2.1 → posthog-8.2.3}/README.md +0 -0
  14. {posthog-8.2.1 → posthog-8.2.3}/posthog/__init__.py +0 -0
  15. {posthog-8.2.1 → posthog-8.2.3}/posthog/_async_consumer.py +0 -0
  16. {posthog-8.2.1 → posthog-8.2.3}/posthog/_async_request.py +0 -0
  17. {posthog-8.2.1 → posthog-8.2.3}/posthog/_async_utils.py +0 -0
  18. {posthog-8.2.1 → posthog-8.2.3}/posthog/_disabled_lane_queue.py +0 -0
  19. {posthog-8.2.1 → posthog-8.2.3}/posthog/_logging.py +0 -0
  20. {posthog-8.2.1 → posthog-8.2.3}/posthog/_metrics_autocapture.py +0 -0
  21. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/__init__.py +0 -0
  22. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/anthropic/__init__.py +0 -0
  23. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/anthropic/_anthropic_stream.py +0 -0
  24. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/anthropic/anthropic.py +0 -0
  25. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/anthropic/anthropic_async.py +0 -0
  26. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/anthropic/anthropic_converter.py +0 -0
  27. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/anthropic/anthropic_providers.py +0 -0
  28. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/claude_agent_sdk/__init__.py +0 -0
  29. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/claude_agent_sdk/formatting.py +0 -0
  30. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/evaluations/__init__.py +0 -0
  31. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/evaluations/_async_datasets.py +0 -0
  32. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/evaluations/_client.py +0 -0
  33. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/evaluations/_dataset_serialization.py +0 -0
  34. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/evaluations/_dataset_types.py +0 -0
  35. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/evaluations/_datasets.py +0 -0
  36. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/evaluations/_errors.py +0 -0
  37. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/evaluations/_mutations.py +0 -0
  38. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/evaluations/_scorer_types.py +0 -0
  39. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/evaluations/_scorers.py +0 -0
  40. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/evaluations/_serialization.py +0 -0
  41. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/evaluations/_tags.py +0 -0
  42. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/evaluations/_transport.py +0 -0
  43. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/evaluations/_types.py +0 -0
  44. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/gateway.py +0 -0
  45. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/gemini/__init__.py +0 -0
  46. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/gemini/_shared.py +0 -0
  47. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/gemini/gemini.py +0 -0
  48. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/gemini/gemini_async.py +0 -0
  49. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/gemini/gemini_converter.py +0 -0
  50. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/langchain/__init__.py +0 -0
  51. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/langchain/middleware.py +0 -0
  52. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/media.py +0 -0
  53. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/openai/__init__.py +0 -0
  54. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/openai/_embeddings.py +0 -0
  55. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/openai/_streaming.py +0 -0
  56. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/openai/openai.py +0 -0
  57. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/openai/openai_async.py +0 -0
  58. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/openai/openai_converter.py +0 -0
  59. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/openai/openai_providers.py +0 -0
  60. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/openai/wrapper_utils.py +0 -0
  61. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/openai_agents/__init__.py +0 -0
  62. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/openai_agents/processor.py +0 -0
  63. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/otel/__init__.py +0 -0
  64. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/otel/_privacy.py +0 -0
  65. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/otel/_request_context.py +0 -0
  66. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/otel/exporter.py +0 -0
  67. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/otel/processor.py +0 -0
  68. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/otel/spans.py +0 -0
  69. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/prompts.py +0 -0
  70. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/sanitization.py +0 -0
  71. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/stream.py +0 -0
  72. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/types.py +0 -0
  73. {posthog-8.2.1 → posthog-8.2.3}/posthog/ai/utils.py +0 -0
  74. {posthog-8.2.1 → posthog-8.2.3}/posthog/args.py +0 -0
  75. {posthog-8.2.1 → posthog-8.2.3}/posthog/async_client.py +0 -0
  76. {posthog-8.2.1 → posthog-8.2.3}/posthog/bucketed_rate_limiter.py +0 -0
  77. {posthog-8.2.1 → posthog-8.2.3}/posthog/capture_compression.py +0 -0
  78. {posthog-8.2.1 → posthog-8.2.3}/posthog/capture_event.py +0 -0
  79. {posthog-8.2.1 → posthog-8.2.3}/posthog/capture_send.py +0 -0
  80. {posthog-8.2.1 → posthog-8.2.3}/posthog/client.py +0 -0
  81. {posthog-8.2.1 → posthog-8.2.3}/posthog/consumer.py +0 -0
  82. {posthog-8.2.1 → posthog-8.2.3}/posthog/contexts.py +0 -0
  83. {posthog-8.2.1 → posthog-8.2.3}/posthog/emails.py +0 -0
  84. {posthog-8.2.1 → posthog-8.2.3}/posthog/exception_capture.py +0 -0
  85. {posthog-8.2.1 → posthog-8.2.3}/posthog/exception_utils.py +0 -0
  86. {posthog-8.2.1 → posthog-8.2.3}/posthog/feature_flag_evaluations.py +0 -0
  87. {posthog-8.2.1 → posthog-8.2.3}/posthog/feature_flags.py +0 -0
  88. {posthog-8.2.1 → posthog-8.2.3}/posthog/flag_definition_cache.py +0 -0
  89. {posthog-8.2.1 → posthog-8.2.3}/posthog/integrations/__init__.py +0 -0
  90. {posthog-8.2.1 → posthog-8.2.3}/posthog/integrations/_tracing_headers.py +0 -0
  91. {posthog-8.2.1 → posthog-8.2.3}/posthog/integrations/asgi.py +0 -0
  92. {posthog-8.2.1 → posthog-8.2.3}/posthog/integrations/celery.py +0 -0
  93. {posthog-8.2.1 → posthog-8.2.3}/posthog/integrations/django.py +0 -0
  94. {posthog-8.2.1 → posthog-8.2.3}/posthog/integrations/drf.py +0 -0
  95. {posthog-8.2.1 → posthog-8.2.3}/posthog/integrations/flask.py +0 -0
  96. {posthog-8.2.1 → posthog-8.2.3}/posthog/integrations/otel.py +0 -0
  97. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/__init__.py +0 -0
  98. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_argument_ownership.py +0 -0
  99. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_capture.py +0 -0
  100. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_compatibility.py +0 -0
  101. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_context_parameters.py +0 -0
  102. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_conversation_id.py +0 -0
  103. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_event_types.py +0 -0
  104. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_exceptions.py +0 -0
  105. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_ids.py +0 -0
  106. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_instrument_fastmcp.py +0 -0
  107. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_instrument_lowlevel.py +0 -0
  108. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_instrument_v2.py +0 -0
  109. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_instrumentation.py +0 -0
  110. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_intent.py +0 -0
  111. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_internal.py +0 -0
  112. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_model_parameters.py +0 -0
  113. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_output_instructions.py +0 -0
  114. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_posthog_events.py +0 -0
  115. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_raised_tool_error.py +0 -0
  116. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_sanitization.py +0 -0
  117. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_server_build.py +0 -0
  118. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_sink.py +0 -0
  119. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_tool_input.py +0 -0
  120. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_transport_identity.py +0 -0
  121. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/_truncation.py +0 -0
  122. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/asgi.py +0 -0
  123. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/constants.py +0 -0
  124. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/feedback.py +0 -0
  125. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/logger.py +0 -0
  126. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/posthog_mcp.py +0 -0
  127. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/request_headers.py +0 -0
  128. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/session.py +0 -0
  129. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/session_token.py +0 -0
  130. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/tools.py +0 -0
  131. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/types.py +0 -0
  132. {posthog-8.2.1 → posthog-8.2.3}/posthog/mcp/version.py +0 -0
  133. {posthog-8.2.1 → posthog-8.2.3}/posthog/metrics_capture.py +0 -0
  134. {posthog-8.2.1 → posthog-8.2.3}/posthog/poller.py +0 -0
  135. {posthog-8.2.1 → posthog-8.2.3}/posthog/py.typed +0 -0
  136. {posthog-8.2.1 → posthog-8.2.3}/posthog/release_id.py +0 -0
  137. {posthog-8.2.1 → posthog-8.2.3}/posthog/request.py +0 -0
  138. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/__init__.py +0 -0
  139. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/__init__.py +0 -0
  140. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/claude_agent_sdk/__init__.py +0 -0
  141. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/claude_agent_sdk/test_processor_content.py +0 -0
  142. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/openai_agents/__init__.py +0 -0
  143. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/openai_agents/test_processor.py +0 -0
  144. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/otel/__init__.py +0 -0
  145. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/otel/conftest.py +0 -0
  146. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/otel/test_exporter.py +0 -0
  147. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/otel/test_processor.py +0 -0
  148. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/otel/test_spans.py +0 -0
  149. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/test_async_cancellation.py +0 -0
  150. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/test_async_stream_wrapper.py +0 -0
  151. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/test_capture_contract.py +0 -0
  152. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/test_capture_pipeline.py +0 -0
  153. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/test_evaluations.py +0 -0
  154. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/test_evaluations_datasets.py +0 -0
  155. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/test_evaluations_mutations.py +0 -0
  156. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/test_evaluations_scorers.py +0 -0
  157. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/test_evaluations_transport.py +0 -0
  158. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/test_gateway.py +0 -0
  159. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/test_http_status.py +0 -0
  160. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/test_media.py +0 -0
  161. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/test_privacy_mode.py +0 -0
  162. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/test_prompts.py +0 -0
  163. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/test_sanitization.py +0 -0
  164. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/test_stream_midstream_error.py +0 -0
  165. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/test_system_prompts.py +0 -0
  166. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/test_token_reporting.py +0 -0
  167. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/test_tokens_source.py +0 -0
  168. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/test_utils.py +0 -0
  169. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/ai/utils.py +0 -0
  170. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/capture_helpers.py +0 -0
  171. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/conftest.py +0 -0
  172. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/logging_helpers.py +0 -0
  173. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/__init__.py +0 -0
  174. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/_helpers.py +0 -0
  175. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/_helpers_lowlevel.py +0 -0
  176. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/_helpers_v2.py +0 -0
  177. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/conftest.py +0 -0
  178. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_asgi.py +0 -0
  179. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_capture.py +0 -0
  180. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_conversation_session.py +0 -0
  181. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_defaults.py +0 -0
  182. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_error_properties.py +0 -0
  183. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_error_status.py +0 -0
  184. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_fastmcp.py +0 -0
  185. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_fastmcp_tool_errors.py +0 -0
  186. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_fastmcp_v2.py +0 -0
  187. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_fastmcp_v4.py +0 -0
  188. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_features_m4.py +0 -0
  189. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_feedback.py +0 -0
  190. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_initialize_handshake.py +0 -0
  191. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_instrument_lowlevel_ownership.py +0 -0
  192. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_instrument_v2.py +0 -0
  193. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_instrumentation_fork.py +0 -0
  194. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_lib_label.py +0 -0
  195. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_lowlevel.py +0 -0
  196. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_model_parameters.py +0 -0
  197. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_no_crash.py +0 -0
  198. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_output_instructions.py +0 -0
  199. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_pending_tasks.py +0 -0
  200. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_pipeline.py +0 -0
  201. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_posthog_mcp.py +0 -0
  202. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_request_headers.py +0 -0
  203. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_resources.py +0 -0
  204. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_review_fixes.py +0 -0
  205. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_send_tools_list_event.py +0 -0
  206. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_session_token.py +0 -0
  207. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_tool_input.py +0 -0
  208. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_transport_identity.py +0 -0
  209. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_truncation.py +0 -0
  210. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_types.py +0 -0
  211. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_units.py +0 -0
  212. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_unknown_tool.py +0 -0
  213. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_v2_lowlevel.py +0 -0
  214. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_v2_mcpserver.py +0 -0
  215. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_v2_wire_dual_era.py +0 -0
  216. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/mcp/test_virtual_tools.py +0 -0
  217. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/released_flags_scenarios.py +0 -0
  218. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_ai_capture_lane.py +0 -0
  219. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_async_client.py +0 -0
  220. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_async_consumer.py +0 -0
  221. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_async_feature_flags.py +0 -0
  222. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_async_request.py +0 -0
  223. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_async_utils.py +0 -0
  224. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_before_send.py +0 -0
  225. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_bucketed_rate_limiter.py +0 -0
  226. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_capture_compression.py +0 -0
  227. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_capture_event.py +0 -0
  228. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_capture_invariants.py +0 -0
  229. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_capture_send.py +0 -0
  230. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_client.py +0 -0
  231. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_client_crap_flags.py +0 -0
  232. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_client_fork.py +0 -0
  233. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_code_variables.py +0 -0
  234. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_consumer.py +0 -0
  235. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_contexts.py +0 -0
  236. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_emails.py +0 -0
  237. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_evaluate_flags.py +0 -0
  238. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_event_options.py +0 -0
  239. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_exception_capture.py +0 -0
  240. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_exception_utils.py +0 -0
  241. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_experiment_exposure.py +0 -0
  242. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_failed_flag_records.py +0 -0
  243. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_feature_flag.py +0 -0
  244. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_feature_flag_called_minimization.py +0 -0
  245. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_feature_flag_has_experiment.py +0 -0
  246. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_feature_flag_result.py +0 -0
  247. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_feature_flags.py +0 -0
  248. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_feature_flags_regex.py +0 -0
  249. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_flag_cache_eviction_reads.py +0 -0
  250. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_flag_cache_publication.py +0 -0
  251. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_flag_definition_cache.py +0 -0
  252. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_flag_payload_parsing.py +0 -0
  253. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_flags_released_behaviour.py +0 -0
  254. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_flags_v4_response.py +0 -0
  255. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_gevent_compat.py +0 -0
  256. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_metrics.py +0 -0
  257. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_metrics_autocapture.py +0 -0
  258. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_module.py +0 -0
  259. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_posthog_upgrade_workflow.py +0 -0
  260. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_property_matching_version.py +0 -0
  261. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_redis_flag_cache_snapshot.py +0 -0
  262. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_reference_markdown.py +0 -0
  263. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_release_id.py +0 -0
  264. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_request.py +0 -0
  265. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_server_payload_snapshots.py +0 -0
  266. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_size_limited_dict.py +0 -0
  267. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_types.py +0 -0
  268. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_utils.py +0 -0
  269. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/test_utils_acceptance.py +0 -0
  270. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/tracing/__init__.py +0 -0
  271. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/tracing/conftest.py +0 -0
  272. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/tracing/helpers.py +0 -0
  273. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/tracing/test_client_traces.py +0 -0
  274. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/tracing/test_config.py +0 -0
  275. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/tracing/test_export.py +0 -0
  276. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/tracing/test_ids.py +0 -0
  277. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/tracing/test_limits.py +0 -0
  278. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/tracing/test_otlp.py +0 -0
  279. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/tracing/test_pipeline.py +0 -0
  280. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/tracing/test_sanitize.py +0 -0
  281. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/tracing/test_span.py +0 -0
  282. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/tracing/test_traceparent.py +0 -0
  283. {posthog-8.2.1 → posthog-8.2.3}/posthog/test/tracing/test_transport.py +0 -0
  284. {posthog-8.2.1 → posthog-8.2.3}/posthog/tracing/__init__.py +0 -0
  285. {posthog-8.2.1 → posthog-8.2.3}/posthog/tracing/_before_span_send.py +0 -0
  286. {posthog-8.2.1 → posthog-8.2.3}/posthog/tracing/_config.py +0 -0
  287. {posthog-8.2.1 → posthog-8.2.3}/posthog/tracing/_context.py +0 -0
  288. {posthog-8.2.1 → posthog-8.2.3}/posthog/tracing/_drops.py +0 -0
  289. {posthog-8.2.1 → posthog-8.2.3}/posthog/tracing/_export.py +0 -0
  290. {posthog-8.2.1 → posthog-8.2.3}/posthog/tracing/_ids.py +0 -0
  291. {posthog-8.2.1 → posthog-8.2.3}/posthog/tracing/_limits.py +0 -0
  292. {posthog-8.2.1 → posthog-8.2.3}/posthog/tracing/_otlp.py +0 -0
  293. {posthog-8.2.1 → posthog-8.2.3}/posthog/tracing/_pipeline.py +0 -0
  294. {posthog-8.2.1 → posthog-8.2.3}/posthog/tracing/_sanitize.py +0 -0
  295. {posthog-8.2.1 → posthog-8.2.3}/posthog/tracing/_span.py +0 -0
  296. {posthog-8.2.1 → posthog-8.2.3}/posthog/tracing/_traceparent.py +0 -0
  297. {posthog-8.2.1 → posthog-8.2.3}/posthog/tracing/_transport.py +0 -0
  298. {posthog-8.2.1 → posthog-8.2.3}/posthog/tracing/span.py +0 -0
  299. {posthog-8.2.1 → posthog-8.2.3}/posthog/types.py +0 -0
  300. {posthog-8.2.1 → posthog-8.2.3}/posthog/utils.py +0 -0
  301. {posthog-8.2.1 → posthog-8.2.3}/posthog.egg-info/SOURCES.txt +0 -0
  302. {posthog-8.2.1 → posthog-8.2.3}/posthog.egg-info/dependency_links.txt +0 -0
  303. {posthog-8.2.1 → posthog-8.2.3}/posthog.egg-info/requires.txt +0 -0
  304. {posthog-8.2.1 → posthog-8.2.3}/posthog.egg-info/top_level.txt +0 -0
  305. {posthog-8.2.1 → posthog-8.2.3}/setup.cfg +0 -0
  306. {posthog-8.2.1 → posthog-8.2.3}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: posthog
3
- Version: 8.2.1
3
+ Version: 8.2.3
4
4
  Summary: Integrate PostHog into any python application.
5
5
  Home-page: https://github.com/posthog/posthog-python
6
6
  Author: Posthog
@@ -32,9 +32,11 @@ from .formatting import (
32
32
  from posthog.ai.claude_agent_sdk.processor import (
33
33
  PostHogClaudeAgentProcessor,
34
34
  _GenerationTracker,
35
+ _PendingToolSpan,
35
36
  _from_subagent,
36
37
  _promote_input,
37
38
  _starts_generation,
39
+ _tool_result_blocks,
38
40
  )
39
41
  from posthog.client import Client
40
42
 
@@ -119,6 +121,8 @@ class PostHogClaudeSDKClient:
119
121
  self._current_input: Optional[List[Dict[str, Any]]] = None
120
122
  self._next_input: Optional[List[Dict[str, Any]]] = None
121
123
  self._pending_output: List[Dict[str, Any]] = []
124
+ # Tool spans waiting for their result, keyed by tool_use_id
125
+ self._pending_tools: Dict[str, _PendingToolSpan] = {}
122
126
  self._query_start = time.time()
123
127
 
124
128
  async def connect(self, prompt: Any = None) -> None:
@@ -146,12 +150,20 @@ class PostHogClaudeSDKClient:
146
150
 
147
151
  async def receive_response(self):
148
152
  """Instrumented receive_response -- yields all messages, emits PostHog events."""
149
- async for message in self._client.receive_response():
153
+ try:
154
+ async for message in self._client.receive_response():
155
+ try:
156
+ self._process_message(message)
157
+ except Exception as e:
158
+ log.debug(f"PostHog instrumentation error (non-fatal): {e}")
159
+ yield message
160
+ finally:
161
+ # A turn that stops early (break, interrupt, no ResultMessage) would
162
+ # otherwise hold its unfinished tool spans until disconnect().
150
163
  try:
151
- self._process_message(message)
164
+ self._processor._flush_tool_spans(self._pending_tools)
152
165
  except Exception as e:
153
166
  log.debug(f"PostHog instrumentation error (non-fatal): {e}")
154
- yield message
155
167
 
156
168
  def _process_message(self, message: Any) -> None:
157
169
  if isinstance(message, StreamEvent):
@@ -186,16 +198,23 @@ class PostHogClaudeSDKClient:
186
198
  elif isinstance(message, AssistantMessage):
187
199
  self._process_assistant_message(message)
188
200
 
189
- elif isinstance(message, UserMessage) and not _from_subagent(message):
190
- if not self._tracker.had_any_stream_events:
191
- # Without stream events, a tool result ends the assistant turn;
192
- # the result fallback reports the final turn only.
193
- self._pending_output = []
194
- self._next_input = _format_user_input(
195
- message.content, self._processor._client, self._next_input
196
- )
201
+ elif isinstance(message, UserMessage):
202
+ # Tool results close their tool spans, subagent ones included.
203
+ for block in _tool_result_blocks(message):
204
+ self._processor._finish_tool_span(
205
+ self._pending_tools, block, self._privacy
206
+ )
207
+ if not _from_subagent(message):
208
+ if not self._tracker.had_any_stream_events:
209
+ # Without stream events, a tool result ends the assistant
210
+ # turn; the result fallback reports the final turn only.
211
+ self._pending_output = []
212
+ self._next_input = _format_user_input(
213
+ message.content, self._processor._client, self._next_input
214
+ )
197
215
 
198
216
  elif isinstance(message, ResultMessage):
217
+ self._processor._flush_tool_spans(self._pending_tools)
199
218
  if not self._tracker.had_any_stream_events:
200
219
  self._processor._emit_generation_from_result(
201
220
  message,
@@ -217,7 +236,8 @@ class PostHogClaudeSDKClient:
217
236
  parent_id = self._tracker.current_span_id or self._current_generation_span_id
218
237
  for block in message.content:
219
238
  if isinstance(block, ToolUseBlock):
220
- self._processor._emit_tool_span(
239
+ self._processor._start_tool_span(
240
+ self._pending_tools,
221
241
  block,
222
242
  self._trace_id,
223
243
  parent_id,
@@ -237,6 +257,8 @@ class PostHogClaudeSDKClient:
237
257
  """
238
258
  # Emit the trace event covering the entire session
239
259
  try:
260
+ # Tool spans whose result never arrived are emitted without one.
261
+ self._processor._flush_tool_spans(self._pending_tools)
240
262
  latency = time.time() - self._query_start
241
263
  resolved_id = self._processor._resolve_distinct_id(self._distinct_id)
242
264
 
@@ -62,6 +62,36 @@ class _GenerationData:
62
62
  stop_reason: Optional[str] = None
63
63
 
64
64
 
65
+ @dataclass
66
+ class _PendingToolSpan:
67
+ """A tool span waiting for its ToolResultBlock."""
68
+
69
+ properties: Dict[str, Any]
70
+ start_time: float
71
+ distinct_id: Any
72
+ groups: Dict[str, Any]
73
+ extra_options: Optional[Dict[str, Any]] = None
74
+ privacy: bool = False
75
+
76
+
77
+ _TOOL_SPAN_CONTENT_PROPERTIES = ("$ai_input_state", "$ai_output_state", "$ai_error")
78
+
79
+
80
+ def _tool_error_text(content: Any) -> str:
81
+ """The `$ai_error` text for an errored tool result's content."""
82
+ if isinstance(content, str):
83
+ return content
84
+ if isinstance(content, list):
85
+ texts = [
86
+ item["text"]
87
+ for item in content
88
+ if isinstance(item, dict) and isinstance(item.get("text"), str)
89
+ ]
90
+ if texts:
91
+ return "\n".join(texts)
92
+ return "" if content is None else str(content)
93
+
94
+
65
95
  class _GenerationTracker:
66
96
  """Tracks StreamEvent boundaries to reconstruct per-generation metrics.
67
97
 
@@ -162,7 +192,9 @@ class PostHogClaudeAgentProcessor:
162
192
 
163
193
  Emits:
164
194
  - $ai_generation: one per Anthropic API call (reconstructed from StreamEvents)
165
- - $ai_span: one per tool use (ToolUseBlock in AssistantMessage)
195
+ - $ai_span: one per tool use (ToolUseBlock in AssistantMessage), emitted
196
+ when its ToolResultBlock arrives so it carries the result, error flag
197
+ and latency
166
198
  - $ai_trace: one per query() call (on ResultMessage)
167
199
  """
168
200
 
@@ -315,90 +347,114 @@ class PostHogClaudeAgentProcessor:
315
347
  # Accumulate assistant output per generation
316
348
  pending_output: List[Dict[str, Any]] = []
317
349
 
318
- async for message in original_query(
319
- prompt=prompt, options=options, transport=transport
320
- ):
321
- # All instrumentation is wrapped in try/except so PostHog errors
322
- # never interrupt the underlying Claude Agent SDK query.
323
- try:
324
- if isinstance(message, StreamEvent):
325
- if _starts_generation(message):
326
- if tracker.current_span_id is not None:
327
- # The previous request never finished (it was
328
- # retried), so its partial output is dropped.
350
+ # Tool spans waiting for their result, keyed by tool_use_id
351
+ pending_tools: Dict[str, _PendingToolSpan] = {}
352
+
353
+ try:
354
+ async for message in original_query(
355
+ prompt=prompt, options=options, transport=transport
356
+ ):
357
+ # All instrumentation is wrapped in try/except so PostHog errors
358
+ # never interrupt the underlying Claude Agent SDK query.
359
+ try:
360
+ if isinstance(message, StreamEvent):
361
+ if _starts_generation(message):
362
+ if tracker.current_span_id is not None:
363
+ # The previous request never finished (it was
364
+ # retried), so its partial output is dropped.
365
+ pending_output = []
366
+ current_input, next_input = _promote_input(
367
+ current_input, next_input
368
+ )
369
+ tracker.process_stream_event(message)
370
+
371
+ # Emit $ai_generation when a turn completes
372
+ if tracker.has_completed_generation():
373
+ gen = tracker.pop_generation()
374
+ generation_index += 1
375
+ current_generation_span_id = gen.span_id
376
+ self._emit_generation(
377
+ gen,
378
+ trace_id,
379
+ generation_index,
380
+ current_input,
381
+ pending_output or None,
382
+ distinct_id_override,
383
+ extra_props,
384
+ privacy,
385
+ groups,
386
+ extra_options=extra_options,
387
+ )
388
+ current_input = None
329
389
  pending_output = []
330
- current_input, next_input = _promote_input(
331
- current_input, next_input
332
- )
333
- tracker.process_stream_event(message)
334
-
335
- # Emit $ai_generation when a turn completes
336
- if tracker.has_completed_generation():
337
- gen = tracker.pop_generation()
338
- generation_index += 1
339
- current_generation_span_id = gen.span_id
340
- self._emit_generation(
341
- gen,
342
- trace_id,
343
- generation_index,
344
- current_input,
345
- pending_output or None,
346
- distinct_id_override,
347
- extra_props,
348
- privacy,
349
- groups,
350
- extra_options=extra_options,
390
+
391
+ elif isinstance(message, AssistantMessage):
392
+ tracker.set_model(message.model)
393
+ # Use the in-progress generation's span_id as parent for tool spans.
394
+ # AssistantMessage arrives before message_stop, so current_generation_span_id
395
+ # would be stale (from the previous turn). tracker.current_span_id gives us
396
+ # the correct in-progress generation.
397
+ parent_id = (
398
+ tracker.current_span_id or current_generation_span_id
351
399
  )
352
- current_input = None
353
- pending_output = []
354
-
355
- elif isinstance(message, AssistantMessage):
356
- tracker.set_model(message.model)
357
- # Use the in-progress generation's span_id as parent for tool spans.
358
- # AssistantMessage arrives before message_stop, so current_generation_span_id
359
- # would be stale (from the previous turn). tracker.current_span_id gives us
360
- # the correct in-progress generation.
361
- parent_id = tracker.current_span_id or current_generation_span_id
362
- for block in message.content:
363
- if isinstance(block, ToolUseBlock):
364
- self._emit_tool_span(
365
- block,
400
+ for block in message.content:
401
+ if isinstance(block, ToolUseBlock):
402
+ self._start_tool_span(
403
+ pending_tools,
404
+ block,
405
+ trace_id,
406
+ parent_id,
407
+ distinct_id_override,
408
+ extra_props,
409
+ privacy,
410
+ groups,
411
+ extra_options=extra_options,
412
+ )
413
+ if not _from_subagent(message):
414
+ # A subagent's own API calls are not this query's generations.
415
+ pending_output = _append_assistant_output(
416
+ pending_output, message.content
417
+ )
418
+
419
+ elif isinstance(message, UserMessage):
420
+ # Tool results close their tool spans, subagent ones included.
421
+ for block in _tool_result_blocks(message):
422
+ self._finish_tool_span(pending_tools, block, privacy)
423
+ if not _from_subagent(message):
424
+ # UserMessages carry tool results; queue them as input
425
+ # for the *next* generation.
426
+ if not tracker.had_any_stream_events:
427
+ # Without stream events, a tool result ends the
428
+ # assistant turn; the result fallback reports the
429
+ # final turn only.
430
+ pending_output = []
431
+ next_input = _format_user_input(
432
+ message.content, self._client, next_input
433
+ )
434
+
435
+ elif isinstance(message, ResultMessage):
436
+ self._flush_tool_spans(pending_tools)
437
+ # Fallback: if no StreamEvents were received, emit a single
438
+ # generation from ResultMessage aggregate data
439
+ if not tracker.had_any_stream_events:
440
+ self._emit_generation_from_result(
441
+ message,
366
442
  trace_id,
367
- parent_id,
443
+ tracker.last_model,
444
+ query_start,
445
+ initial_input,
446
+ pending_output,
368
447
  distinct_id_override,
369
448
  extra_props,
370
449
  privacy,
371
450
  groups,
372
451
  extra_options=extra_options,
373
452
  )
374
- if not _from_subagent(message):
375
- # A subagent's own API calls are not this query's generations.
376
- pending_output = _append_assistant_output(
377
- pending_output, message.content
378
- )
379
453
 
380
- elif isinstance(message, UserMessage) and not _from_subagent(message):
381
- # UserMessages carry tool results; queue them as input for the
382
- # *next* generation.
383
- if not tracker.had_any_stream_events:
384
- # Without stream events, a tool result ends the assistant
385
- # turn; the result fallback reports the final turn only.
386
- pending_output = []
387
- next_input = _format_user_input(
388
- message.content, self._client, next_input
389
- )
390
-
391
- elif isinstance(message, ResultMessage):
392
- # Fallback: if no StreamEvents were received, emit a single
393
- # generation from ResultMessage aggregate data
394
- if not tracker.had_any_stream_events:
395
- self._emit_generation_from_result(
454
+ self._emit_trace(
396
455
  message,
397
456
  trace_id,
398
- tracker.last_model,
399
457
  query_start,
400
- initial_input,
401
- pending_output,
402
458
  distinct_id_override,
403
459
  extra_props,
404
460
  privacy,
@@ -406,22 +462,18 @@ class PostHogClaudeAgentProcessor:
406
462
  extra_options=extra_options,
407
463
  )
408
464
 
409
- self._emit_trace(
410
- message,
411
- trace_id,
412
- query_start,
413
- distinct_id_override,
414
- extra_props,
415
- privacy,
416
- groups,
417
- extra_options=extra_options,
418
- )
465
+ except Exception as e:
466
+ log.debug(f"PostHog instrumentation error (non-fatal): {e}")
419
467
 
468
+ yield message
469
+ finally:
470
+ # Tool spans still waiting for a result (the query ended
471
+ # early, failed or was cancelled) are emitted without one.
472
+ try:
473
+ self._flush_tool_spans(pending_tools)
420
474
  except Exception as e:
421
475
  log.debug(f"PostHog instrumentation error (non-fatal): {e}")
422
476
 
423
- yield message
424
-
425
477
  def _emit_generation(
426
478
  self,
427
479
  gen: _GenerationData,
@@ -573,8 +625,9 @@ class PostHogClaudeAgentProcessor:
573
625
  options=extra_options,
574
626
  )
575
627
 
576
- def _emit_tool_span(
628
+ def _start_tool_span(
577
629
  self,
630
+ pending: Dict[str, _PendingToolSpan],
578
631
  block: "ToolUseBlock",
579
632
  trace_id: str,
580
633
  parent_span_id: Optional[str],
@@ -584,8 +637,12 @@ class PostHogClaudeAgentProcessor:
584
637
  groups: Dict[str, Any],
585
638
  extra_options: Optional[Dict[str, Any]] = None,
586
639
  ) -> None:
587
- resolved_id = self._resolve_distinct_id(distinct_id)
640
+ """Hold a tool span until its ToolResultBlock arrives.
588
641
 
642
+ The span is emitted by ``_finish_tool_span`` with the result, error
643
+ flag and latency, or by ``_flush_tool_spans`` without them if the
644
+ result never arrives.
645
+ """
589
646
  properties: Dict[str, Any] = {
590
647
  "$ai_trace_id": trace_id,
591
648
  "$ai_span_id": str(uuid.uuid4()),
@@ -602,13 +659,68 @@ class PostHogClaudeAgentProcessor:
602
659
  _ensure_serializable(block.input), self._client
603
660
  )
604
661
 
662
+ span = _PendingToolSpan(
663
+ properties=properties,
664
+ start_time=time.time(),
665
+ distinct_id=distinct_id,
666
+ groups=groups,
667
+ extra_options=extra_options,
668
+ privacy=privacy,
669
+ )
670
+ tool_use_id = getattr(block, "id", None)
671
+ if tool_use_id is None:
672
+ # Nothing can match a result to this span, so emit it now.
673
+ self._capture_tool_span(span)
674
+ return
675
+ # A repeated id would orphan the earlier span; emit it first.
676
+ previous = pending.pop(tool_use_id, None)
677
+ if previous is not None:
678
+ self._capture_tool_span(previous)
679
+ pending[tool_use_id] = span
680
+
681
+ def _finish_tool_span(
682
+ self,
683
+ pending: Dict[str, _PendingToolSpan],
684
+ block: Any,
685
+ privacy: bool,
686
+ ) -> None:
687
+ """Emit the held tool span for a ToolResultBlock with its outcome."""
688
+ tool_use_id = getattr(block, "tool_use_id", None)
689
+ span = pending.pop(tool_use_id, None) if tool_use_id is not None else None
690
+ if span is None:
691
+ return
692
+ properties = span.properties
693
+ properties["$ai_latency"] = time.time() - span.start_time
694
+ is_error = bool(getattr(block, "is_error", False))
695
+ properties["$ai_is_error"] = is_error
696
+ if _with_privacy_mode(self._client, privacy, True):
697
+ content = format_tool_result_content(block, self._client)
698
+ properties["$ai_output_state"] = finalize_ai_content(content, self._client)
699
+ if is_error:
700
+ properties["$ai_error"] = _tool_error_text(content)
701
+ self._capture_tool_span(span)
702
+
703
+ def _flush_tool_spans(self, pending: Dict[str, _PendingToolSpan]) -> None:
704
+ """Emit tool spans whose result never arrived, without an outcome."""
705
+ while pending:
706
+ _, span = pending.popitem()
707
+ self._capture_tool_span(span)
708
+
709
+ def _capture_tool_span(self, span: _PendingToolSpan) -> None:
710
+ if _with_privacy_mode(self._client, span.privacy, True) is None:
711
+ # Privacy may have been enabled while the tool ran; drop content
712
+ # buffered before then.
713
+ for key in _TOOL_SPAN_CONTENT_PROPERTIES:
714
+ span.properties.pop(key, None)
715
+ resolved_id = self._resolve_distinct_id(span.distinct_id)
716
+ trace_id = span.properties["$ai_trace_id"]
605
717
  self._capture_event(
606
718
  "$ai_span",
607
- properties,
719
+ span.properties,
608
720
  resolved_id or trace_id,
609
- groups,
721
+ span.groups,
610
722
  personless=resolved_id is None,
611
- options=extra_options,
723
+ options=span.extra_options,
612
724
  )
613
725
 
614
726
  def _emit_trace(
@@ -680,6 +792,14 @@ def _from_subagent(message: Any) -> bool:
680
792
  return getattr(message, "parent_tool_use_id", None) is not None
681
793
 
682
794
 
795
+ def _tool_result_blocks(message: "UserMessage") -> List[Any]:
796
+ """The ToolResultBlocks in a UserMessage."""
797
+ content = message.content
798
+ if not isinstance(content, list):
799
+ return []
800
+ return [block for block in content if hasattr(block, "tool_use_id")]
801
+
802
+
683
803
  def _starts_generation(message: "StreamEvent") -> bool:
684
804
  """Whether a main-agent API call is starting (subagent calls are skipped)."""
685
805
  return (
@@ -8,7 +8,7 @@ except ImportError:
8
8
  import json
9
9
  import logging
10
10
  import time
11
- from dataclasses import dataclass
11
+ from dataclasses import dataclass, field
12
12
  from typing import (
13
13
  Any,
14
14
  Dict,
@@ -72,6 +72,8 @@ class SpanMetadata:
72
72
  """End time of the run."""
73
73
  input: Optional[Any]
74
74
  """Input of the run: messages, prompt variables, etc."""
75
+ span_type: Optional[str] = field(default=None, kw_only=True)
76
+ """`$ai_span_type` of the run, e.g. "tool" for tool runs."""
75
77
 
76
78
  @property
77
79
  def latency(self) -> float:
@@ -303,7 +305,7 @@ class CallbackHandler(BaseCallbackHandler):
303
305
  )
304
306
  self._set_parent_of_run(run_id, parent_run_id)
305
307
  self._set_trace_or_span_metadata(
306
- serialized, input_str, run_id, parent_run_id, **kwargs
308
+ serialized, input_str, run_id, parent_run_id, span_type="tool", **kwargs
307
309
  )
308
310
 
309
311
  def on_tool_end(
@@ -461,12 +463,17 @@ class CallbackHandler(BaseCallbackHandler):
461
463
  input: Any,
462
464
  run_id: UUID,
463
465
  parent_run_id: Optional[UUID] = None,
466
+ span_type: Optional[str] = None,
464
467
  **kwargs,
465
468
  ):
466
469
  default_name = "trace" if parent_run_id is None else "span"
467
470
  run_name = _get_langchain_run_name(serialized, **kwargs) or default_name
468
471
  self._runs[run_id] = SpanMetadata(
469
- name=run_name, input=input, start_time=time.time(), end_time=None
472
+ name=run_name,
473
+ input=input,
474
+ start_time=time.time(),
475
+ end_time=None,
476
+ span_type=span_type,
470
477
  )
471
478
 
472
479
  def _set_llm_metadata(
@@ -575,6 +582,8 @@ class CallbackHandler(BaseCallbackHandler):
575
582
  }
576
583
  if parent_run_id is not None:
577
584
  event_properties["$ai_parent_id"] = parent_run_id
585
+ if run.span_type is not None and event_name == "$ai_span":
586
+ event_properties["$ai_span_type"] = run.span_type
578
587
  if self._properties:
579
588
  event_properties.update(self._properties)
580
589
 
@@ -593,6 +602,20 @@ class CallbackHandler(BaseCallbackHandler):
593
602
  self._privacy_mode,
594
603
  finalize_ai_content(outputs, self._ph_client),
595
604
  )
605
+ if (
606
+ run.span_type == "tool"
607
+ and isinstance(outputs, ToolMessage)
608
+ and outputs.status == "error"
609
+ ):
610
+ # A tool error handled by `handle_tool_error` ends the run
611
+ # normally, with the error as a ToolMessage. Only the tool's own
612
+ # span is the error, not a chain that returns the message.
613
+ event_properties["$ai_is_error"] = True
614
+ event_properties["$ai_error"] = with_privacy_mode(
615
+ self._ph_client,
616
+ self._privacy_mode,
617
+ _tool_message_text(outputs),
618
+ )
596
619
 
597
620
  _capture_ai_event(
598
621
  self._ph_client,
@@ -847,6 +870,19 @@ def _convert_lc_tool_calls_to_oai(
847
870
  return tool_calls
848
871
 
849
872
 
873
+ def _tool_message_text(message: ToolMessage) -> str:
874
+ """The text of a ToolMessage, for `$ai_error`."""
875
+ content = message.content
876
+ if isinstance(content, str):
877
+ return content
878
+ texts = [
879
+ item if isinstance(item, str) else item.get("text", "")
880
+ for item in content
881
+ if isinstance(item, str) or isinstance(item, dict)
882
+ ]
883
+ return "\n".join(text for text in texts if text)
884
+
885
+
850
886
  def _convert_message_to_dict(message: BaseMessage) -> dict[str, Any]:
851
887
  # assistant message
852
888
  if isinstance(message, HumanMessage):
@@ -860,7 +896,15 @@ def _convert_message_to_dict(message: BaseMessage) -> dict[str, Any]:
860
896
  elif isinstance(message, SystemMessage):
861
897
  message_dict = {"role": "system", "content": message.content}
862
898
  elif isinstance(message, ToolMessage):
863
- message_dict = {"role": "tool", "content": message.content}
899
+ message_dict = {
900
+ "role": "tool",
901
+ "content": message.content,
902
+ "tool_call_id": message.tool_call_id,
903
+ }
904
+ if message.name:
905
+ message_dict["name"] = message.name
906
+ if message.status == "error":
907
+ message_dict["status"] = message.status
864
908
  elif isinstance(message, FunctionMessage):
865
909
  message_dict = {"role": "function", "content": message.content}
866
910
  else:
@@ -1011,6 +1055,31 @@ def _parse_usage_model(
1011
1055
  normalized_usage.input_tokens = max(
1012
1056
  normalized_usage.input_tokens - cache_tokens, 0
1013
1057
  )
1058
+
1059
+ # PostHog prices Gemini reasoning on top of the output count, as the Gemini
1060
+ # wrapper reports it. langchain-google-genai folds thoughts_token_count into
1061
+ # output_tokens, so take it back out. Other integrations (ChatVertexAI, older
1062
+ # langchain-google-genai) report candidates only, which the totals show:
1063
+ # input and output only reach total_tokens when reasoning is in the output.
1064
+ is_gemini = bool(model and "gemini" in model.lower())
1065
+ total_tokens = usage.get("total_tokens")
1066
+ reasoning_in_output = (
1067
+ isinstance(total_tokens, int)
1068
+ and total_tokens > 0
1069
+ and isinstance(normalized_usage.input_tokens, int)
1070
+ and isinstance(normalized_usage.output_tokens, int)
1071
+ and normalized_usage.input_tokens + normalized_usage.output_tokens
1072
+ >= total_tokens
1073
+ )
1074
+ if (
1075
+ is_gemini
1076
+ and reasoning_in_output
1077
+ and normalized_usage.output_tokens
1078
+ and normalized_usage.reasoning_tokens
1079
+ ):
1080
+ normalized_usage.output_tokens = max(
1081
+ normalized_usage.output_tokens - normalized_usage.reasoning_tokens, 0
1082
+ )
1014
1083
  return normalized_usage
1015
1084
 
1016
1085