refactor(serving): share chat serving runner - #4876
Open
lvhan028 wants to merge 6 commits into
Open
Conversation
lvhan028
force-pushed
the
refactor/chat-runner
branch
from
August 18, 2026 04:31
cc2de8e to
41c0301
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors the serving stack to introduce a shared, protocol-neutral ChatRunner that centralizes request parsing, generation config construction, streaming chunk normalization, and cleanup across OpenAI Chat Completions, OpenAI Responses, and Anthropic Messages endpoints.
Changes:
- Add
lmdeploy.serve.core.chat_runner.ChatRunnerand migrate OpenAI Chat Completions / Responses and Anthropic Messages endpoints to use it for both streaming and non-streaming flows. - Refactor OpenAI Responses and Anthropic streaming helpers to consume a parsed stream of
ChatStreamChunkitems instead of raw engine yields + parser objects. - Tighten OpenAI
ChatCompletionRequest.messagesto require a list of message dicts (removing string-prompt support) and update tests accordingly.
Reviewed changes
Copilot reviewed 20 out of 20 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/test_lmdeploy/test_messages.py | Adds validation test ensuring messages must be a list. |
| tests/test_lmdeploy/serve/test_generation_config.py | Updates tests to pass structured messages inputs consistent with new schema. |
| tests/test_lmdeploy/serve/openai/responses/test_streaming.py | Updates streaming tests to provide parsed chunks into stream_response. |
| tests/test_lmdeploy/serve/openai/responses/test_serving.py | Adds coverage for tool parser requirement, boolean default forwarding, and engine error behavior. |
| tests/test_lmdeploy/serve/openai/responses/conftest.py | Extends fake engine/parser fixtures to satisfy new runner/streaming metadata needs. |
| tests/test_lmdeploy/serve/core/test_chat_runner.py | New test suite for shared ChatRunner behavior (parsing, mapping, cleanup, error handling). |
| tests/test_lmdeploy/serve/anthropic/test_endpoints.py | Updates Anthropic endpoint tests for parsed streaming + tool parser requirement. |
| tests/test_lmdeploy/serve/anthropic/test_adapter_conversion.py | Removes generation-config mapping test now superseded by shared runner path. |
| lmdeploy/serve/openai/responses/streaming.py | Changes stream_response to accept a parsed stream and rely on ChatStreamChunk fields. |
| lmdeploy/serve/openai/responses/serving.py | Migrates /v1/responses serving to ChatRunner + centralized request validation. |
| lmdeploy/serve/openai/responses/request.py | Adds ResponsesRequestContext and check_request to validate/adapt Responses requests. |
| lmdeploy/serve/openai/protocol.py | Makes ChatCompletionRequest.messages strictly a list of message dicts; removes do_preprocess. |
| lmdeploy/serve/openai/chat_completions/validation.py | Simplifies “messages empty” detection given updated schema expectations. |
| lmdeploy/serve/openai/chat_completions/serving.py | Migrates /v1/chat/completions serving to ChatRunner for generation + streaming. |
| lmdeploy/serve/openai/api_client.py | Updates client method signature/docs to require structured messages. |
| lmdeploy/serve/core/chat_runner.py | New shared runner implementation (prepare/stream/collect/cleanup + chunk/result dataclasses). |
| lmdeploy/serve/anthropic/streaming.py | Refactors stream converter to accept parsed chunks and a ChatCompletionRequest wrapper. |
| lmdeploy/serve/anthropic/endpoints/validation.py | New centralized validation for Anthropic /v1/messages requests. |
| lmdeploy/serve/anthropic/endpoints/messages.py | Migrates Anthropic /v1/messages endpoint to ChatRunner + new validation helper. |
| lmdeploy/serve/anthropic/adapter.py | Removes deprecated/temporary tool-related guards and generation-config mapping helper. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| return_logprob=bool(request.return_logprob), | ||
| return_token_ids=bool(request.return_token_ids), | ||
| return_routed_experts=bool(request.return_routed_experts), | ||
| include_stop_str_in_output=bool(request.include_stop_str_in_output), |
| model: str | ||
|
|
||
| messages: str | list[dict[str, Any]] = Field(examples=[[{'role': 'user', 'content': 'hi'}]]) | ||
| messages: list[dict[str, Any]] = Field(examples=[[{'role': 'user', 'content': 'hi'}]]) |
Co-authored-by: Cursor <cursoragent@cursor.com>
Keep explicit response lifecycle cleanup while removing unused direct generator and session ownership. Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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