Skip to content

fix(langfuse): prefer gateway_session_key for session grouping - #71608

Open
Slobaka wants to merge 1 commit into
NousResearch:mainfrom
Slobaka:fix/langfuse-prefer-gateway-session-key
Open

fix(langfuse): prefer gateway_session_key for session grouping#71608
Slobaka wants to merge 1 commit into
NousResearch:mainfrom
Slobaka:fix/langfuse-prefer-gateway-session-key

Conversation

@Slobaka

@Slobaka Slobaka commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Langfuse plugin now prefers gateway_session_key (X-Hermes-Session-Key) over ephemeral session_id when setting Langfuse session grouping
  • Core LLM/API hooks forward agent._gateway_session_key so the plugin can see it
  • Regression: two /v1/responses-style turns with different UUIDs but the same gateway key open one Langfuse session

Fixes #71556

Why

Open WebUI Responses mode often does not chain previous_response_id. The API server then mints a fresh UUID session_id per request while the stable channel key lives only on gateway_session_key. Langfuse previously keyed solely on session_id, so each turn became a disconnected session.

Test plan

  • bash scripts/run_tests.sh tests/plugins/test_langfuse_plugin.py -- -k 'GatewaySession or TurnTrace or langfuse_session'

@alt-glitch alt-glitch added type/bug Something isn't working comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint comp/plugins Plugin system and bundled plugins area/sessions Session lifecycle, resume, persistence, history P3 Low — cosmetic, nice to have labels Jul 25, 2026

@teknium1 teknium1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for targeting the right separation: gateway_session_key is already the documented stable channel identity, while /v1/responses creates a fresh UUID when no previous_response_id is available (gateway/platforms/api_server.py:4974-4977).

Problems

  • Current main routes the Langfuse request tracer through pre_api_request (plugins/observability/langfuse/__init__.py:1128-1135), dispatched at agent/conversation_loop.py:2147. Its payload currently contains session_id (agent/conversation_loop.py:2151) but not gateway_session_key. Salvage the forwarding change onto that current hook seam; otherwise the plugin cannot receive the stable key on the active tracing path.
  • The proposed regression test calls the plugin handler directly, so it does not prove the API-server/agent/hook propagation chain required for this bug. Add a focused integration test for that boundary.

Suggested changes

  • Forward agent._gateway_session_key in the current pre_api_request payload and preserve the plugin’s existing session_id fallback.
  • Test two distinct unchained Responses-style session IDs sharing one gateway key through the hook boundary.

Automated hermes-sweeper review.

@@ -1891,6 +1892,7 @@ def run_conversation(
turn_id=turn_id,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please carry this forwarding change onto current main’s active pre_api_request dispatch as well. Langfuse registers its request tracer on pre_api_request (plugins/observability/langfuse/__init__.py:1132), and current agent/conversation_loop.py:2147-2155 does not yet include gateway_session_key.

def test_langfuse_session_id_prefers_gateway_key(self):
mod = self._fresh_plugin()
assert mod._langfuse_session_id("uuid-ephemeral", "agent:main:api:chat:1") == (
"agent:main:api:chat:1"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This validates the plugin helper directly, but not the AIAgent → pre_api_request hook propagation. Please add a focused integration-level test that captures the active hook payload from an agent initialized with gateway_session_key.

@teknium1 teknium1 added sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state sweeper:risk-compatibility Sweeper risk: may break existing users, config, migrations, defaults, or upgrades sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform labels Jul 30, 2026
@GottZ

GottZ commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

This was generated by AI during triage.

Summary

One PR addresses issue #71556. #71608 implements the correct stable-key preference in Langfuse, but its current diff does not forward gateway_session_key through the active pre_api_request dispatch and therefore does not yet complete the reported propagation path.

Related pull requests

Suggested consolidation

Keep #71608 open with a salvage path: forward agent._gateway_session_key in the active pre_api_request payload identified at agent/conversation_loop.py:2147-2155, retain the existing session_id fallback, and add the requested integration-level test capturing that hook payload for distinct unchained sessions sharing one gateway key. It remains the recorded best fix and sole candidate, but these contributor-requested changes are required before consolidation can advance.

Complex graph

flowchart LR
    classDef open fill:#dbeafe,stroke:#1d4ed8,color:#1e3a8a
    classDef merged fill:#dcfce7,stroke:#15803d,color:#14532d
    classDef closed fill:#e5e7eb,stroke:#6b7280,color:#1f2937
    classDef unverified fill:#f3f4f6,stroke:#9ca3af,color:#374151
    classDef best stroke-width:3px,stroke:#b45309
    classDef target stroke-width:3px,stroke:#4338ca
    I71556(["issue #71556 (open)"])
    P71608["PR #71608 (open)"]
    P71608 -->|best fix| I71556
    class I71556 open
    class P71608 open
    class P71608 best
    class P71608 target
    click I71556 "https://github.com/NousResearch/hermes-agent/issues/71556"
    click P71608 "https://github.com/NousResearch/hermes-agent/pull/71608"
Loading

Graph: solid arrow = fixes / best fix, dashed arrow = partial or unverified (see edge label); boxed group = PRs duplicating each other; amber border = best fix; indigo border = target; gray node = closed (state tag in the node label).

Cross-PR triage: Reviewed 1 pull request and 1 issue in this complex. Each diff was read against this issue; Assessment working set: 11 kB of PR diffs, 5 kB of issue/PR text, 3 kB of discussion (5 comments), 3 verify verdicts. verdicts reflect diff content, not PR titles. Part of an automated triage batch.

Forward agent._gateway_session_key on pre_api_request and sibling hooks,
and group Langfuse traces by that stable channel key when present.

Fixes NousResearch#71556
@Slobaka
Slobaka force-pushed the fix/langfuse-prefer-gateway-session-key branch from 6e2dbea to 13f1f5c Compare August 4, 2026 23:24
@Slobaka

Slobaka commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

Addressed review feedback on current main (13f1f5c):

  • Forwarded gateway_session_key on active pre_api_request dispatch (and sibling hooks)
  • Langfuse prefers gateway key for session grouping with session_id fallback
  • Added regression + pre_api_request forward checks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/sessions Session lifecycle, resume, persistence, history comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint comp/plugins Plugin system and bundled plugins P3 Low — cosmetic, nice to have sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform sweeper:risk-compatibility Sweeper risk: may break existing users, config, migrations, defaults, or upgrades sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Langfuse plugin uses random session_id from /v1/responses instead of stable gateway_session_key - traces do not chain under Open WebUI

4 participants