-
Notifications
You must be signed in to change notification settings - Fork 81
Open
Conversation
gouveags
commented
Sep 11, 2026
Contributor
Author
@greptileai review
Contributor
Prompt To Fix All With AI
### Issue 1 posthog/ai/langchain/middleware.py:100 **Recovered failures lose parents** When an outer retry or recovery middleware handles a model exception and the agent later completes, this call captures the failed attempt without `$ai_parent_id` even though it retains the root trace ID. The failed attempt therefore appears as root-level work instead of a child of the agent. The same issue affects async model failures and sync or async tool failures. Preserve the root relationship for recovered failures, and suppress it only when the invocation is known to have terminated. ### Issue 2 posthog/ai/langchain/middleware.py:114-116 **Async capture blocks execution** If the supplied client uses `sync_mode=True`, this async wrapper calls synchronous telemetry capture on the event-loop thread after awaiting the model handler. That capture performs HTTP delivery inline, so every model event can block other coroutines; the same applies to agent and tool completion hooks. Run blocking capture work off the event-loop thread or provide an async capture path. The repository directive requires blocking synchronous HTTP calls in async Python to be flagged and avoided. --- For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
Reviews (1): Last reviewed commit: "feat(ai): add LangChain v1 agent middlew..." | Re-trigger Greptile
gouveags
marked this pull request as ready for review
September 11, 2026 22:19
gouveags
commented
Sep 11, 2026
Contributor
Author
@greptileai review
Contributor
Reviews (2): Last reviewed commit: "fix(ai): avoid blocking async LangChain ..." | Re-trigger Greptile
gouveags
commented
Sep 11, 2026
Contributor
Author
@greptileai review
Contributor
Reviews (3): Last reviewed commit: "fix(ai): avoid blocking async LangChain ..." | Re-trigger Greptile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
💡 Motivation and Context
LangChain v1 supports middleware as the main way to package agent integrations, but the Python SDK currently only supports callbacks.
This adds
posthog.ai.langchain.middleware.PostHogMiddleware, the Python counterpart to PostHog/posthog-js#4556.Closes #901.
What changed
langchain.Client(sync_mode=True).Use either
PostHogMiddlewareorCallbackHandlerfor an agent invocation, not both. PutPostHogMiddlewarelast in the middleware list so it sees the final model selection and each retry attempt.On terminal agent failure, LangChain does not call
after_agent. In that case, the failed model or tool call is captured without a parent, and no dangling root trace is emitted.💚 How did you test it?
langchain==1.3.9The middleware tests cover sync and async agent runs, non-blocking async capture, model retries, model and tool failures, returned tool errors,
Commandresults, concurrent invocations, privacy mode, checkpoint cleanup, tool-schema fallback, capture failures, and compatibility with the existing callback integration.📝 Checklist
If releasing new changes
🤖 Agent context
Autonomy: Human-driven (agent-assisted)
Gabriel directed the scope and is the DRI (
@gouveags). Codex assisted with implementation, tests, validation, and independent review. No shareable session transcript is available.The main design choice was to keep middleware support optional and reuse the existing callback capture logic instead of maintaining a second telemetry implementation. Human review is required.