|
| 1 | +"""Tests for the Anthropic VLM provider.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import types |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +import pytest |
| 9 | +from PIL import Image |
| 10 | + |
| 11 | +from paperbanana.providers.vlm.anthropic import AnthropicVLM |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.asyncio |
| 15 | +async def test_generate_text_only(monkeypatch: pytest.MonkeyPatch) -> None: |
| 16 | + """AnthropicVLM.generate should send a basic text-only request and return text.""" |
| 17 | + captured: dict[str, Any] = {} |
| 18 | + |
| 19 | + class _FakeMessages: |
| 20 | + async def create(self, **kwargs: Any) -> Any: # type: ignore[override] |
| 21 | + captured.update(kwargs) |
| 22 | + block = types.SimpleNamespace(type="text", text="hello world") |
| 23 | + resp = types.SimpleNamespace(content=[block], usage=None) |
| 24 | + return resp |
| 25 | + |
| 26 | + class _FakeClient: |
| 27 | + def __init__(self, *args: Any, **kwargs: Any) -> None: # noqa: D401 |
| 28 | + self.messages = _FakeMessages() |
| 29 | + |
| 30 | + # Patch anthropic.AsyncAnthropic before the provider imports it. |
| 31 | + fake_mod = types.ModuleType("anthropic") |
| 32 | + fake_mod.AsyncAnthropic = _FakeClient # type: ignore[attr-defined] |
| 33 | + |
| 34 | + import sys |
| 35 | + |
| 36 | + monkeypatch.setitem(sys.modules, "anthropic", fake_mod) |
| 37 | + |
| 38 | + vlm = AnthropicVLM(api_key="test-key", model="claude-3-5-sonnet-20251023") |
| 39 | + text = await vlm.generate("Hi Claude") |
| 40 | + |
| 41 | + assert text == "hello world" |
| 42 | + assert captured["model"] == vlm.model_name |
| 43 | + assert captured["max_tokens"] == 4096 |
| 44 | + assert isinstance(captured["messages"], list) |
| 45 | + assert captured["messages"][0]["role"] == "user" |
| 46 | + |
| 47 | + |
| 48 | +@pytest.mark.asyncio |
| 49 | +async def test_generate_with_images_and_json(monkeypatch: pytest.MonkeyPatch) -> None: |
| 50 | + """AnthropicVLM.generate should inline images and enable JSON mode when requested.""" |
| 51 | + captured: dict[str, Any] = {} |
| 52 | + |
| 53 | + class _FakeMessages: |
| 54 | + async def create(self, **kwargs: Any) -> Any: # type: ignore[override] |
| 55 | + captured.update(kwargs) |
| 56 | + block = types.SimpleNamespace(type="text", text="{}") |
| 57 | + resp = types.SimpleNamespace(content=[block], usage=None) |
| 58 | + return resp |
| 59 | + |
| 60 | + class _FakeClient: |
| 61 | + def __init__(self, *args: Any, **kwargs: Any) -> None: # noqa: D401 |
| 62 | + self.messages = _FakeMessages() |
| 63 | + |
| 64 | + fake_mod = types.ModuleType("anthropic") |
| 65 | + fake_mod.AsyncAnthropic = _FakeClient # type: ignore[attr-defined] |
| 66 | + |
| 67 | + import sys |
| 68 | + |
| 69 | + monkeypatch.setitem(sys.modules, "anthropic", fake_mod) |
| 70 | + |
| 71 | + # Avoid depending on real base64 implementation details. |
| 72 | + def _fake_image_to_base64(_img: Image.Image) -> str: |
| 73 | + return "base64-image-data" |
| 74 | + |
| 75 | + monkeypatch.setattr( |
| 76 | + "paperbanana.providers.vlm.anthropic.image_to_base64", |
| 77 | + _fake_image_to_base64, |
| 78 | + ) |
| 79 | + |
| 80 | + vlm = AnthropicVLM(api_key="test-key", model="claude-3-5-sonnet-20251023") |
| 81 | + img = Image.new("RGB", (4, 4)) |
| 82 | + |
| 83 | + await vlm.generate("Hi with image", images=[img], response_format="json") |
| 84 | + |
| 85 | + assert captured["model"] == vlm.model_name |
| 86 | + msg = captured["messages"][0] |
| 87 | + assert msg["role"] == "user" |
| 88 | + content = msg["content"] |
| 89 | + assert content[0]["type"] == "image" |
| 90 | + assert content[0]["source"]["data"] == "base64-image-data" |
| 91 | + assert content[-1]["type"] == "text" |
| 92 | + assert content[-1]["text"] == "Hi with image" |
| 93 | + |
| 94 | + output_config = captured["output_config"] |
| 95 | + fmt = output_config["format"] |
| 96 | + assert fmt["type"] == "json_schema" |
| 97 | + assert isinstance(fmt["schema"], dict) |
0 commit comments