| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | """Claude Code's attribution header in chat completions system messages.""" |
| 4 | |
| 5 | from unittest.mock import MagicMock |
| 6 | |
| 7 | import pytest |
| 8 | |
| 9 | from vllm.entrypoints.chat_utils import _parse_chat_message_content |
| 10 | |
| 11 | HEADER = ( |
| 12 | "x-anthropic-billing-header: cc_version=2.1.220.300; " |
| 13 | "cc_entrypoint=claude-desktop; cch=7adab;" |
| 14 | ) |
| 15 | |
| 16 | |
| 17 | def _parse(message, content_format="openai"): |
| 18 | tracker = MagicMock() |
| 19 | tracker.create_parser.return_value.mm_placeholder_storage.return_value = {} |
| 20 | return _parse_chat_message_content( |
| 21 | message, tracker, content_format, interleave_strings=False |
| 22 | ) |
| 23 | |
| 24 | |
| 25 | @pytest.mark.skip_global_cleanup |
| 26 | @pytest.mark.parametrize( |
| 27 | "header", |
| 28 | [ |
| 29 | HEADER, |
| 30 | "x-anthropic-billing-header: cc_version=2.1.286.99b; cc_entrypoint=sdk-cli;", |
| 31 | ], |
| 32 | ) |
| 33 | def test_system_header_part_is_dropped(header): |
| 34 | (msg,) = _parse( |
| 35 | { |
| 36 | "role": "system", |
| 37 | "content": [ |
| 38 | {"type": "text", "text": header}, |
| 39 | {"type": "text", "text": "You are a Claude agent."}, |
| 40 | ], |
| 41 | } |
| 42 | ) |
| 43 | assert msg["content"] == [{"type": "text", "text": "You are a Claude agent."}] |
| 44 | |
| 45 | |
| 46 | @pytest.mark.skip_global_cleanup |
| 47 | def test_system_header_line_is_stripped_from_string(): |
| 48 | (msg,) = _parse( |
| 49 | {"role": "system", "content": HEADER + "\nYou are a Claude agent."}, |
| 50 | content_format="string", |
| 51 | ) |
| 52 | assert msg["content"] == "You are a Claude agent." |
| 53 | |
| 54 | |
| 55 | @pytest.mark.skip_global_cleanup |
| 56 | def test_requests_differing_only_in_cch_parse_identically(): |
| 57 | def system(cch): |
| 58 | return { |
| 59 | "role": "system", |
| 60 | "content": HEADER.replace("7adab", cch) + "\nYou are a Claude agent.", |
| 61 | } |
| 62 | |
| 63 | assert _parse(system("7adab")) == _parse(system("986dd")) |
| 64 | |
| 65 | |
| 66 | @pytest.mark.skip_global_cleanup |
| 67 | @pytest.mark.parametrize( |
| 68 | "message", |
| 69 | [ |
| 70 | {"role": "system", "content": "You are a Claude agent."}, |
| 71 | {"role": "system", "content": "Quote: " + HEADER}, |
| 72 | {"role": "user", "content": HEADER + "Hello"}, |
| 73 | ], |
| 74 | ) |
| 75 | def test_other_content_is_unchanged(message): |
| 76 | (msg,) = _parse(message, content_format="string") |
| 77 | assert msg["content"] == message["content"] |