Skip to content

fix(anthropic): build message params as plain dicts so anthropic 2.x works - #1955

Merged
Yunnglin merged 1 commit into
modelscope:mainfrom
JOhnsonKC201:fix-anthropic-2x-message-params
Oct 27, 2026
Merged

Yunnglin merged 1 commit into
modelscope:mainfrom
JOhnsonKC201:fix-anthropic-2x-message-params

Conversation

@JOhnsonKC201

Copy link
Copy Markdown
Contributor

anthropic 2.0.0 (2026-10-21) changed anthropic.types.MessageParam into a
Union[UserMessageParam, AssistantMessageParam, SystemMessageParam], so calling it now raises

TypeError: Cannot instantiate typing.Union

evalscope doesn't pin anthropic, so anyone with a current install hits this on every
--eval-type anthropic_api run, before any request goes out. The calls are all in
evalscope/models/utils/anthropic.py (anthropic_message_param and _combine_messages).

This swaps the 7 MessageParam(...) calls for dict literals. The TypedDict "constructor" only
ever returned a dict, so the output is the same on older SDKs.

I checked the sampling params too: on 2.x messages.create no longer has temperature, top_p
or top_k, but validate_request_params already moves anything not in the signature into
extra_body, so those still get sent and don't raise. I left that alone.

Testing (Python 3.13, Windows):

  • tests/models/test_anthropic_*.py + tests/agent/external/test_anthropic_bridge_prompt_cache.py
    • main + anthropic 2.3.0: 17 failed, 27 passed
    • this branch + anthropic 2.3.0 / 1.17.0 / 0.75.0: 44 passed each
  • AnthropicCompatibleAPI.generate() with temperature=0.0 against a dead local port:
    main fails with the TypeError, this branch gets to the HTTP request (connection error, as expected)
  • ruff check and ruff format --check clean

anthropic 2.0 made MessageParam a Union of the user, assistant and
system param types, so calling it raises "TypeError: Cannot
instantiate typing.Union" and every anthropic_api eval fails before
sending a request. The TypedDicts only ever produced dicts, so dict
literals keep the same output on 0.x, 1.x and 2.x.

@Yunnglin Yunnglin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Yunnglin
Yunnglin merged commit 1067d51 into modelscope:main Oct 27, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants