Skip to content

fix the anthropic adapter#4578

Open
lvhan028 wants to merge 2 commits into
InternLM:mainfrom
lvhan028:fix-anthropic
Open

fix the anthropic adapter#4578
lvhan028 wants to merge 2 commits into
InternLM:mainfrom
lvhan028:fix-anthropic

Conversation

@lvhan028
Copy link
Copy Markdown
Collaborator

@lvhan028 lvhan028 commented May 9, 2026

No description provided.

Copilot AI review requested due to automatic review settings May 9, 2026 14:56
@lvhan028 lvhan028 added the Bug:P0 label May 9, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 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_messages conversion logic to map Anthropic message/content blocks into OpenAI-compatible message dicts.
  • Updated the Anthropic messages endpoint to build a ChatCompletionRequest using 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 thread lmdeploy/serve/anthropic/endpoints/messages.py Outdated
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}'
@lvhan028 lvhan028 requested review from CUHKSZzxy and lzhangzz May 12, 2026 03:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants