Skip to content

feat(celery): propagate context options to workers - #1207

Merged
eli-r-ph merged 3 commits into
eli/v1-mcp-event-optionsfrom
eli/v1-celery-context-options
Nov 9, 2026
Merged

eli-r-ph merged 3 commits into
eli/v1-mcp-event-optionsfrom
eli/v1-celery-context-options

Conversation

@eli-r-ph

@eli-r-ph eli-r-ph commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

The Celery integration carried the distinct ID, session ID and tags to workers, but not context options. Options set with set_context_option before publishing a task were lost on the worker.

Stacked on #1206.

  • Publish writes context options as JSON in an X-POSTHOG-CONTEXT-OPTIONS header, top level and nested, like the tags header.
  • The worker reads it with the tags parser, now shared, and sets each entry with set_context_option after the tags and before celery task started. Missing, invalid or non-object JSON gives no options.
  • propagate_context=False sends no options, like the other headers.
  • Docs: the module and propagate_context docstrings list context options. The migration guide notes the propagation, that a propagated process_person_profile: True creates a person for each worker event without a distinct ID, and that eager mode publishes nothing.
  • Minor changeset.

💚 How did you test it?

  • Extended the publish, round-trip, propagate_context and invalid-JSON tests.
  • New wire-level tests use a real Client and the v1 payload. They cover all four options, an option against a legacy tag on the same key, legacy tags alone, context options against worker super_options, a personless process_person_profile: True, an override inside the task, missing, invalid, non-object and bytes headers, and propagate_context=False.
  • 9 tests fail without the source change and pass with it. The others guard behavior that must not change.
  • ruff, mypy baseline and the public API check pass. The integration tests and test_client.py pass. The full suite has three fork tests that also fail on macOS without this change.

📝 Checklist

  • All commits are signed (see Commit signing)
  • 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 (minor changeset written by hand)

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Claude Code, Claude Opus 5.5. The header name, placement and precedence were set by the maintainer. Options are propagated unfiltered, like tags.

🤖 Generated with Claude Code

Publish context options in an X-POSTHOG-CONTEXT-OPTIONS task header,
next to the tags header, and set them as context options on the worker
before the task started event. propagate_context=False sends none.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@eli-r-ph eli-r-ph self-assigned this Nov 8, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@eli-r-ph
eli-r-ph marked this pull request as ready for review November 8, 2026 03:20
@eli-r-ph
eli-r-ph requested a review from a team as a code owner November 8, 2026 03:20

@marandaneto marandaneto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good; no qualifying findings. Verdict: correct.

* feat(ai): take capture options in the AI integrations

Every AI wrapper method that takes posthog_properties also takes
posthog_options, and the Gemini clients take it as a per-client
default. LangChain's CallbackHandler and PostHogMiddleware, and the
OpenAI Agents and Claude Agent SDK instrument() and processors, take
options=. LangChain also reads posthog_options from run metadata.

The wrappers never pass posthog_options to the provider. The options
are sent as the event's own options, below the personless
process_person_profile: False. A value that is not a dict is logged
and ignored, and no options argument is passed when none are set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(ai): send LangChain exception events with the run's options

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(migration): simplify the 8.0 checklist and MCP hook note

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(ai): pass exception options as a typed keyword

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@eli-r-ph
eli-r-ph merged commit 2e020ed into eli/v1-mcp-event-options Nov 9, 2026
@eli-r-ph
eli-r-ph deleted the eli/v1-celery-context-options branch November 9, 2026 23:40
eli-r-ph added a commit that referenced this pull request Nov 9, 2026
* feat(mcp): add event_options to MCP capture calls and instrument

The PostHogMCP capture_* methods and McpAnalytics.capture take a
keyword-only event_options argument, and MCPAnalyticsOptions gains an
event_options callback, called for the same events as event_properties.
Per-call options win over the callback's key by key, and an event
without an identity still always sends process_person_profile: False.
A non-dict value is logged and ignored.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(mcp): document MCP event_options

Covers the per-call argument and the callback, why context options set
inside a tool handler miss $mcp_tool_call, copying options in MCP
before_send, and the options vs event_options split on PostHogMCP.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(celery): propagate context options to workers (#1207)

* feat(celery): propagate context options to workers

Publish context options in an X-POSTHOG-CONTEXT-OPTIONS task header,
next to the tags header, and set them as context options on the worker
before the task started event. propagate_context=False sends none.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(celery): point eager tasks to explicit PostHog headers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(ai): take capture options in the AI integrations (#1208)

* feat(ai): take capture options in the AI integrations

Every AI wrapper method that takes posthog_properties also takes
posthog_options, and the Gemini clients take it as a per-client
default. LangChain's CallbackHandler and PostHogMiddleware, and the
OpenAI Agents and Claude Agent SDK instrument() and processors, take
options=. LangChain also reads posthog_options from run metadata.

The wrappers never pass posthog_options to the provider. The options
are sent as the event's own options, below the personless
process_person_profile: False. A value that is not a dict is logged
and ignored, and no options argument is passed when none are set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(ai): send LangChain exception events with the run's options

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(migration): simplify the 8.0 checklist and MCP hook note

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(ai): pass exception options as a typed keyword

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
eli-r-ph added a commit that referenced this pull request Nov 9, 2026
…y context options, AI options (#1202, #1206, #1207, #1208)

Squash of the per-event $lib stack: #1202 (per-event $lib/$lib_version), #1206 (MCP event_options), #1207 (Celery context options to workers) and #1208 (capture options in the AI integrations).
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.

2 participants