Skip to content

refactor(serving): share chat serving runner - #4876

Open
lvhan028 wants to merge 6 commits into
InternLM:mainfrom
lvhan028:refactor/chat-runner
Open

refactor(serving): share chat serving runner#4876
lvhan028 wants to merge 6 commits into
InternLM:mainfrom
lvhan028:refactor/chat-runner

Conversation

@lvhan028

Copy link
Copy Markdown
Collaborator

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 18, 2026 04:26
@lvhan028
lvhan028 force-pushed the refactor/chat-runner branch from cc2de8e to 41c0301 Compare August 18, 2026 04:31

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

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.ChatRunner and 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 ChatStreamChunk items instead of raw engine yields + parser objects.
  • Tighten OpenAI ChatCompletionRequest.messages to 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'}]])
lvhan028 and others added 2 commits August 24, 2026 12:21
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>
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