Skip to content

Map user-input session_id to internal session_id to maintain session identity#4523

Open
lvhan028 wants to merge 4 commits intoInternLM:mainfrom
lvhan028:session-id-map
Open

Map user-input session_id to internal session_id to maintain session identity#4523
lvhan028 wants to merge 4 commits intoInternLM:mainfrom
lvhan028:session-id-map

Conversation

@lvhan028
Copy link
Copy Markdown
Collaborator

No description provided.

Copilot AI review requested due to automatic review settings April 14, 2026 02:58
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces a mapping layer between user-provided session_id values and internally managed session IDs, aiming to preserve session identity while preventing direct access to internal session IDs.

Changes:

  • Add VariableInterface.user_session_id_map plus create_session() / find_session() to translate user session IDs to internal session IDs.
  • Update OpenAI endpoints to use create_session() and update abort behavior to look up sessions via the user→internal mapping.
  • Extend SessionManager.get() with a create_if_not_exists flag and adjust session-id generation behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
lmdeploy/serve/openai/api_server.py Adds user→internal session-id mapping, updates session creation/lookup, and changes abort behavior to use mapped sessions.
lmdeploy/serve/managers/session_manager.py Adjusts session-id generation and adds optional “lookup-only” behavior to SessionManager.get().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lmdeploy/serve/openai/api_server.py Outdated
Comment thread lmdeploy/serve/openai/api_server.py Outdated
Comment thread lmdeploy/serve/openai/api_server.py
Comment thread lmdeploy/serve/managers/session_manager.py
Comment thread lmdeploy/serve/managers/session_manager.py
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +210 to +217
def map_user_session_id(self, user_session_id: int) -> int:
"""Map a user_session_id to a session_id."""
if user_session_id in self.user_session_id_map:
raise ValueError(f'User session id {user_session_id} already exists')
session_id = next(self.session_id_generator)
self.user_session_id_map[user_session_id] = session_id
self.session_id_map[session_id] = user_session_id
return session_id
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

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

map_user_session_id() currently raises if user_session_id is already present. Given the API server expects the same user-provided session_id to be reusable across requests (and create_session() calls this on every request), this should be idempotent: return the existing mapped session_id when present, and only allocate a new internal session_id when absent. Otherwise, repeated calls with the same user_session_id will crash the request flow.

Copilot uses AI. Check for mistakes.
if VariableInterface.tool_parser is not None:
request = VariableInterface.tool_parser.adjust_request(request)
session = VariableInterface.get_session(request.session_id)
session = VariableInterface.create_session(request.session_id)
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

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

chat_completions_v1 now passes a mapped/internal session via create_session(). Later in this handler the response id is derived from session.session_id, which will now be the internal id (not the user-provided session_id) and can effectively expose internal ids to clients. Consider switching response ids to use the user-provided session id (or a separate request UUID) so internal ids remain an implementation detail, and keep behavior consistent with /v1/completions.

Copilot uses AI. Check for mistakes.
Comment thread lmdeploy/serve/openai/api_server.py Outdated
Comment thread lmdeploy/serve/openai/api_server.py Outdated
Comment on lines +116 to +117
session_id = session_mgr.map_user_session_id(user_session_id)
logger.info(f'created session {session_id} for user_session_id {user_session_id}')
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

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

create_session() calls session_mgr.map_user_session_id(user_session_id) unconditionally. As implemented, map_user_session_id raises if the user_session_id already exists, so repeated requests (or interactive continuation) with the same session_id will fail with an unhandled ValueError (500) instead of reusing the existing session mapping. Make the mapping lookup idempotent (return the existing mapped session_id when present) or check user_session_id_map first and only create a new mapping when it doesn't exist.

Suggested change
session_id = session_mgr.map_user_session_id(user_session_id)
logger.info(f'created session {session_id} for user_session_id {user_session_id}')
session_id = session_mgr.user_session_id_map.get(user_session_id)
if session_id is None:
session_id = session_mgr.map_user_session_id(user_session_id)
logger.info(f'created session {session_id} for user_session_id {user_session_id}')

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants