Skip to content

Python: freeze first service_session_id for stable gen_ai.conversation.id across turns (fixes #5992)#6195

Open
hanhan761 wants to merge 4 commits into
microsoft:mainfrom
hanhan761:fix-5992-stable-conversation-id
Open

Python: freeze first service_session_id for stable gen_ai.conversation.id across turns (fixes #5992)#6195
hanhan761 wants to merge 4 commits into
microsoft:mainfrom
hanhan761:fix-5992-stable-conversation-id

Conversation

@hanhan761
Copy link
Copy Markdown

Summary

Freeze the first non-None service_session_id on AgentSession so that gen_ai.conversation.id remains stable across multi-turn conversations when using the Responses API.

Root Cause

gen_ai.conversation.id was set from session.service_session_id. For the Responses API, service_session_id rotates on every turn (each response returns a new ID used as previous_response_id for the next call). This caused each invoke_agent span to have a different gen_ai.conversation.id, breaking conversation grouping in Application Insights.

Fix

Added a telemetry_conversation_id property to AgentSession that freezes the first non-None service_session_id value. The observability layer now uses this frozen value instead of the current service_session_id, ensuring:

  • First turn's service_session_id is preserved for telemetry correlation
  • Server-side correlation is maintained (the frozen value is a real server-generated ID)
  • Subsequent turn-to-turn ID rotation (Responses API) no longer fragments conversation spans

Issue

Fixes #5992

Verification

  • AgentSession.telemetry_conversation_id returns the first non-None service_session_id
  • Subsequent service_session_id updates do not change telemetry_conversation_id
  • serialization roundtrip preserves service_session_id
  • All existing TestAgentSession tests pass (8/8)

Copilot AI review requested due to automatic review settings May 30, 2026 07:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: gen_ai.conversation.id is unstable across turns when using Responses API

2 participants