diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py index 15fc482b34e..5916d477a25 100644 --- a/litellm/llms/anthropic/chat/transformation.py +++ b/litellm/llms/anthropic/chat/transformation.py @@ -1981,8 +1981,8 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): data: Final = { "model": model, - "messages": anthropic_messages, **optional_params, + "messages": anthropic_messages, } self._apply_output_config(data=data, model=model, optional_params=optional_params) diff --git a/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.py b/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.py index 25e2c3cda80..81955e4a9a8 100644 --- a/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.py +++ b/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.py @@ -1212,6 +1212,35 @@ def test_anthropic_chat_transform_request_includes_context_management(): assert result["context_management"] == _sample_context_management_payload() +def test_anthropic_chat_transform_request_orders_messages_last(): + config = AnthropicConfig() + result = config.transform_request( + model="claude-sonnet-4-20250514", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Hello"}, + ], + optional_params={ + "tools": [ + { + "name": "get_weather", + "input_schema": {"type": "object", "properties": {}}, + } + ], + "max_tokens": 256, + }, + litellm_params={}, + headers={}, + ) + + keys = list(result.keys()) + assert "system" in keys + assert "tools" in keys + assert keys.index("messages") > keys.index("system") + assert keys.index("messages") > keys.index("tools") + assert keys[-1] == "messages" + + def test_anthropic_structured_output_beta_header(): from litellm.types.utils import CallTypes from litellm.utils import return_raw_request