Skip to content

refactor(serve): split chat_completions endpoint into a package - #4840

Open
lvhan028 wants to merge 1 commit into
InternLM:mainfrom
lvhan028:refactor/chat-completions-package
Open

refactor(serve): split chat_completions endpoint into a package#4840
lvhan028 wants to merge 1 commit into
InternLM:mainfrom
lvhan028:refactor/chat-completions-package

Conversation

@lvhan028

@lvhan028 lvhan028 commented Aug 9, 2026

Copy link
Copy Markdown
Collaborator

Motivation

Improve the maintainence of v1/chat/completions

Modification

  • Splits chat_completions.py into protocol/validation/logprobs/logits_processors/ serving modules.

  • Chat-specific models move to endpoints/chat_completions/protocol.py; shared models stay in the top-level protocol.py with backward-compat re-exports.

Copilot AI lite review requested due to automatic review settings August 9, 2026 09:42

Copilot AI 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.

Pull request overview

Refactors the OpenAI /v1/chat/completions endpoint implementation from a single large module into a dedicated chat_completions package, aligning its layout with the existing responses/ package and adding migration tests to enforce backward-compatible import paths.

Changes:

  • Introduces lmdeploy.serve.openai.endpoints.chat_completions as a package with split-out protocol, validation, logprobs, logits_processors, and serving modules.
  • Moves chat-specific Pydantic models into endpoints/chat_completions/protocol.py while keeping shared models in lmdeploy.serve.openai.protocol.
  • Adds migration equivalence tests to assert package structure and re-export/class-identity invariants.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/test_chat_completions_package_migration.py Adds structural migration tests for package layout + model re-export identity.
lmdeploy/serve/openai/protocol.py Removes chat-specific models and re-exports them for backward compatibility.
lmdeploy/serve/openai/endpoints/chat_completions/validation.py Extracts request validation logic for chat completions.
lmdeploy/serve/openai/endpoints/chat_completions/serving.py Updates chat completions serving module to use the new split modules.
lmdeploy/serve/openai/endpoints/chat_completions/protocol.py New canonical home for chat-specific Pydantic models.
lmdeploy/serve/openai/endpoints/chat_completions/logprobs.py Extracts logprobs construction helpers.
lmdeploy/serve/openai/endpoints/chat_completions/logits_processors.py Extracts logit-bias logits processor.
lmdeploy/serve/openai/endpoints/chat_completions/init.py Adds lazy register export to avoid circular imports.
lmdeploy/serve/openai/endpoints/init.py Adds lazy create_openai_router export to avoid circular imports.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +400 to +420
# Re-export chat completion models for backward compatibility.
# Canonical home is now endpoints/chat_completions/protocol.py. This trailing
# import is deliberately placed at the end of the module so that all shared
# models above are fully defined before chat_completions.protocol imports them.
from lmdeploy.serve.openai.endpoints.chat_completions.protocol import ( # noqa: E402,F401
ChatCompletionRequest,
ChatCompletionResponse,
ChatCompletionResponseChoice,
ChatCompletionResponseStreamChoice,
ChatCompletionStreamResponse,
ChatCompletionTokenLogprob,
ChatMessage,
ChoiceLogprobs,
DeltaFunctionCall,
DeltaMessage,
DeltaToolCall,
ExtractedToolCallInformation,
FunctionCall,
ToolCall,
TopLogprob,
)

Copilot AI 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.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated no new comments.

Suppressed comments (2)

lmdeploy/serve/openai/protocol.py:428

  • __getattr__ is newly introduced here but lacks a return type annotation, unlike the similar lazy exports added in endpoints/__init__.py and chat_completions/__init__.py. Adding -> Any improves consistency and static analysis without changing runtime behavior.
def __getattr__(name: str):

tests/test_chat_completions_package_migration.py:18

  • The NOTE claims import lmdeploy.serve.openai.endpoints.chat_completions as pkg breaks because lmdeploy/__init__.py rebinds serve. Since lmdeploy/serve is a real subpackage, importing a submodule will overwrite the parent attribute, so this explanation is likely misleading. Consider updating the comment to reflect the actual caveat (e.g., that from lmdeploy import serve resolves to the deprecated function) or remove the claim about dotted imports being broken.
# NOTE: We use importlib.import_module rather than the dotted-`as` form
# ``import lmdeploy.serve.openai.endpoints.chat_completions as pkg`` because
# lmdeploy/__init__.py rebinds the ``serve`` attribute to a deprecated
# function (lmdeploy.api.serve), which shadows the ``lmdeploy.serve``
# subpackage attribute and breaks dotted-`as` imports. The submodule itself

Aligns with the responses/ package layout. Splits the 633-line
chat_completions.py into protocol/validation/logprobs/logits_processors/
serving modules. Chat-specific models move to
endpoints/chat_completions/protocol.py; shared models stay in the
top-level protocol.py with backward-compat re-exports. No behavior change.

Co-Authored-By: Claude <noreply@anthropic.com>
@lvhan028
lvhan028 force-pushed the refactor/chat-completions-package branch from 2cc6f0f to 59f2856 Compare August 10, 2026 03:02
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