|
| 1 | +"""Regression tests for Anthropic thinking-block compatibility in LFX.""" |
| 2 | + |
| 3 | +from langchain_core.messages import AIMessage, HumanMessage, ToolMessage |
| 4 | +from lfx.base.models.anthropic_chat_model import ChatAnthropicThinkingCompat, _ensure_thinking_field |
| 5 | +from lfx.base.models.unified_models.class_registry import get_model_class |
| 6 | + |
| 7 | + |
| 8 | +def _malformed_history() -> list: |
| 9 | + """Return a tool-use history whose thinking block is missing `thinking`.""" |
| 10 | + ai_message = AIMessage( |
| 11 | + content=[ |
| 12 | + {"type": "thinking", "signature": "sig==", "index": 0}, |
| 13 | + { |
| 14 | + "type": "tool_use", |
| 15 | + "id": "toolu_01", |
| 16 | + "name": "echo", |
| 17 | + "input": {"text": "hello"}, |
| 18 | + "index": 1, |
| 19 | + }, |
| 20 | + ], |
| 21 | + tool_calls=[{"name": "echo", "args": {"text": "hello"}, "id": "toolu_01", "type": "tool_call"}], |
| 22 | + ) |
| 23 | + return [ |
| 24 | + HumanMessage(content="Use the 'echo' tool to echo: hello"), |
| 25 | + ai_message, |
| 26 | + ToolMessage(content="hello", tool_call_id="toolu_01"), |
| 27 | + ] |
| 28 | + |
| 29 | + |
| 30 | +def _thinking_blocks(payload: dict) -> list[dict]: |
| 31 | + blocks = [] |
| 32 | + for message in payload.get("messages", []): |
| 33 | + content = message.get("content") |
| 34 | + if isinstance(content, list): |
| 35 | + blocks.extend(block for block in content if isinstance(block, dict) and block.get("type") == "thinking") |
| 36 | + return blocks |
| 37 | + |
| 38 | + |
| 39 | +def test_payload_thinking_blocks_always_carry_thinking_field(): |
| 40 | + model = ChatAnthropicThinkingCompat( |
| 41 | + model="claude-sonnet-5", |
| 42 | + api_key="test-key", # pragma: allowlist secret |
| 43 | + max_tokens=1024, |
| 44 | + ) |
| 45 | + |
| 46 | + blocks = _thinking_blocks(model._get_request_payload(_malformed_history())) |
| 47 | + |
| 48 | + assert blocks |
| 49 | + assert all(block.get("thinking") == "" for block in blocks) |
| 50 | + |
| 51 | + |
| 52 | +def test_payload_preserves_existing_thinking_text(): |
| 53 | + model = ChatAnthropicThinkingCompat( |
| 54 | + model="claude-sonnet-5", |
| 55 | + api_key="test-key", # pragma: allowlist secret |
| 56 | + max_tokens=1024, |
| 57 | + ) |
| 58 | + history = _malformed_history() |
| 59 | + history[1].content[0] = { |
| 60 | + "type": "thinking", |
| 61 | + "thinking": "let me reason", |
| 62 | + "signature": "sig==", |
| 63 | + "index": 0, |
| 64 | + } |
| 65 | + |
| 66 | + blocks = _thinking_blocks(model._get_request_payload(history)) |
| 67 | + |
| 68 | + assert blocks |
| 69 | + assert blocks[0]["thinking"] == "let me reason" |
| 70 | + |
| 71 | + |
| 72 | +def test_agent_registry_resolves_compat_class(): |
| 73 | + assert get_model_class("ChatAnthropic") is ChatAnthropicThinkingCompat |
| 74 | + |
| 75 | + |
| 76 | +def test_ensure_thinking_field_handles_missing_and_none(): |
| 77 | + payload = { |
| 78 | + "messages": [ |
| 79 | + { |
| 80 | + "role": "assistant", |
| 81 | + "content": [ |
| 82 | + {"type": "thinking", "signature": "a=="}, |
| 83 | + {"type": "thinking", "thinking": None, "signature": "b=="}, |
| 84 | + {"type": "thinking", "thinking": "kept", "signature": "c=="}, |
| 85 | + {"type": "text", "text": "hi"}, |
| 86 | + ], |
| 87 | + }, |
| 88 | + {"role": "user", "content": "plain string content"}, |
| 89 | + ] |
| 90 | + } |
| 91 | + |
| 92 | + _ensure_thinking_field(payload) |
| 93 | + |
| 94 | + blocks = payload["messages"][0]["content"] |
| 95 | + assert blocks[0]["thinking"] == "" |
| 96 | + assert blocks[1]["thinking"] == "" |
| 97 | + assert blocks[2]["thinking"] == "kept" |
| 98 | + assert payload["messages"][1]["content"] == "plain string content" |
0 commit comments