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

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.

Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily receiving feedbacks. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers.

Motivation

Please describe the motivation of this PR and the goal you want to achieve through this PR.

Modification

Please briefly describe what modification is made in this PR.

BC-breaking (Optional)

Does the modification introduce changes that break the backward-compatibility of the downstream repositories?
If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR.

Use cases (Optional)

If this PR introduces a new feature, it is better to list some use cases here, and update the documentation.

Checklist

  1. Pre-commit or other linting tools are used to fix the potential lint issues.
  2. The modification is covered by complete unit tests. If not, please add more unit tests to ensure the correctness.
  3. If the modification has a dependency on downstream projects of a newer version, this PR should be tested with all supported versions of downstream projects.
  4. The documentation has been modified accordingly, like docstring or example tutorials.

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,
)
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 39a7a3e to 3fb7c3a Compare August 10, 2026 01:37
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