improve(autotest): replace APIClient with OpenAI SDK and strict param checks - #4887
Open
littlegy wants to merge 7 commits into
Open
improve(autotest): replace APIClient with OpenAI SDK and strict param checks#4887littlegy wants to merge 7 commits into
littlegy wants to merge 7 commits into
Conversation
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
A few introduced helper behaviors can produce incorrect test outcomes or mask failures (notably completion-token capping and health-check success criteria).
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR modernizes the RESTful autotest harness by migrating off the internal APIClient in favor of the OpenAI Python SDK (and newer Anthropic SDK patterns), while tightening request-parameter validation tests and making “large payload”/max_tokens tests more session-length aware.
Changes:
- Replace
lmdeploy.serve.openai.api_client.APIClientusage in multiple RESTful tests/utilities with OpenAI SDK calls and a sharedget_client_and_model()helper. - Add stricter negative tests/assertions for invalid request parameters (HTTP 400 + OpenAI error envelope checks).
- Introduce session-length-aware helpers for sizing prompts/payloads and capping
max_tokensto avoid context overflows in follow-up tests.
File summaries
| File | Description |
|---|---|
| requirements/test.txt | Bump Anthropic SDK minimum version to >=1.0.0 for updated SDK behavior. |
| autotest/utils/toolkit.py | Extend encode_text() to support add_special_tokens control for token counting/ID generation. |
| autotest/utils/tool_reasoning_definitions.py | Centralize client/model discovery by importing get_client_and_model(). |
| autotest/utils/run_restful_chat.py | Replace APIClient calls with OpenAI SDK and shared client/model helper; add helper for required server availability. |
| autotest/utils/restful_return_check.py | Add OpenAI error-envelope assertion helper and session-length-aware payload/max_tokens sizing utilities. |
| autotest/utils/anthropic_messages.py | Add anthropic_extra_body() helper and use shared model discovery for Anthropic SDK setup. |
| autotest/interface/restful/tool_parser/test_tool_call_anthropic_sdk.py | Replace APIClient model lookup with cached deployed model name via shared helper; adapt SDK calls using extra_body. |
| autotest/interface/restful/tool_parser/test_tool_call_advanced.py | Use session-length-aware max_tokens capping for routed-experts follow-up regression test. |
| autotest/interface/restful/test_restful_generate.py | Tighten expected status codes and use session-length-aware max_tokens capping; replace server-side encoding dependency for stop token IDs. |
| autotest/interface/restful/test_restful_completions_v1.py | Migrate to OpenAI SDK, add raw HTTP negative tests for invalid params with OpenAI error-envelope assertions. |
| autotest/interface/restful/test_restful_chat_completions_v1.py | Large refactor: migrate to OpenAI SDK, add strict invalid-param negative tests, and session-length-aware oversize payload generation. |
| autotest/interface/restful/test_restful_anthropic_v1.py | Use shared session-sized payload builder for “large payload” regression guard and shared model discovery. |
| autotest/interface/restful/test_restful_anthropic_sdk_messages.py | Update Anthropic SDK calls to pass unsupported fields via extra_body. |
Review details
- Files reviewed: 13/13 changed files
- Comments generated: 3
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+303
to
+307
| session_len = resolve_effective_session_len(config, model_id) | ||
| model_path = get_model_path_from_config(config, model_id) | ||
| prompt_tokens = len(encode_text(model_path, prompt_text, add_special_tokens=False)) | ||
| available = session_len - prompt_tokens - reserve | ||
| return min(default_cap, max(min_cap, available)) |
| messages=[{'role': 'user', 'content': '你好'}], | ||
| extra_body={'top_k': 1}, | ||
| ) | ||
| return model_name == model_name_current and response.choices is not None |
Comment on lines
+252
to
+256
| try: | ||
| client, model_name = get_client_and_model(url) | ||
| except Exception: | ||
| assert False, 'server not start correctly' | ||
| return client, model_name, model_name.split('/')[-1] |
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.
… checks
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