fix the anthropic adapter#4578
Open
lvhan028 wants to merge 2 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds a new Anthropic→OpenAI message conversion helper (to_openai_messages) and updates the Anthropic /v1/messages endpoint to use OpenAI-style messages (including tool/image/thinking blocks), along with unit tests covering the conversion behavior.
Changes:
- Added
to_openai_messagesconversion logic to map Anthropic message/content blocks into OpenAI-compatible message dicts. - Updated the Anthropic messages endpoint to build a
ChatCompletionRequestusing the new conversion output. - Added comprehensive unit tests for block conversion (system blocks, images, tool_use/tool_result, thinking blocks).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| tests/test_lmdeploy/serve/anthropic/test_adapter_conversion.py | Adds unit tests for the new Anthropic→OpenAI message conversion behavior. |
| lmdeploy/serve/anthropic/endpoints/messages.py | Switches endpoint message conversion to to_openai_messages and routes through the OpenAI request/parser path. |
| lmdeploy/serve/anthropic/adapter.py | Implements to_openai_messages and supporting helpers for system/image/tool/thinking block mapping. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+275
to
+284
| if block_type == 'tool_use': | ||
| tool_calls.append( | ||
| dict( | ||
| id=_block_get(block, 'id') or f'call_{int(time.time())}', | ||
| type='function', | ||
| function=dict( | ||
| name=_block_get(block, 'name') or '', | ||
| arguments=json.dumps(_block_get(block, 'input') or {}), | ||
| ), | ||
| )) |
Comment on lines
+188
to
+191
| return _block_get(source, 'url', '') | ||
| media_type = _block_get(source, 'media_type', 'image/jpeg') | ||
| data = _block_get(source, 'data', '') | ||
| return f'data:{media_type};base64,{data}' |
CUHKSZzxy
approved these changes
May 12, 2026
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.
No description provided.