| 1 |
from typing import Any, List, cast |
| 2 |
|
| 3 |
import pytest |
| 4 |
from pydantic import BaseModel |
| 5 |
from inline_snapshot import external, snapshot |
| 6 |
|
| 7 |
from anthropic import Anthropic, AsyncAnthropic, _compat |
| 8 |
|
| 9 |
|
| 10 |
class OrderItem(BaseModel): |
| 11 |
product_name: str |
| 12 |
price: float |
| 13 |
quantity: int |
| 14 |
|
| 15 |
|
| 16 |
class OrderDetails(BaseModel): |
| 17 |
items: List[OrderItem] |
| 18 |
total: float |
| 19 |
|
| 20 |
|
| 21 |
@pytest.mark.skipif(_compat.PYDANTIC_V1, reason="structured outputs not supported with pydantic v1") |
| 22 |
class TestSyncParse: |
| 23 |
@pytest.mark.parametrize( |
| 24 |
"http_snapshot", |
| 25 |
[ |
| 26 |
cast(Any, external("uuid:c11eefc7-fff0-4466-8453-42eef73b8876.json")), |
| 27 |
], |
| 28 |
) |
| 29 |
def test_parse_with_pydantic_model(self, snapshot_client: Anthropic) -> None: |
| 30 |
"""Test sync messages.parse() with a Pydantic model output_format.""" |
| 31 |
|
| 32 |
response = snapshot_client.messages.parse( |
| 33 |
model="claude-sonnet-4-5", |
| 34 |
messages=[ |
| 35 |
{ |
| 36 |
"role": "user", |
| 37 |
"content": "Extract: I want to order 2 Green Tea at $5.50 each", |
| 38 |
} |
| 39 |
], |
| 40 |
output_format=OrderItem, |
| 41 |
max_tokens=1024, |
| 42 |
) |
| 43 |
|
| 44 |
assert response.parsed_output is not None |
| 45 |
assert isinstance(response.parsed_output, OrderItem) |
| 46 |
assert response.parsed_output.product_name == "Green Tea" |
| 47 |
assert response.parsed_output.price == 5.5 |
| 48 |
assert response.parsed_output.quantity == 2 |
| 49 |
|
| 50 |
@pytest.mark.parametrize( |
| 51 |
"http_snapshot", |
| 52 |
[ |
| 53 |
cast(Any, external("uuid:bd7029d5-d8d4-4e06-be3a-2ac9c60803a6.json")), |
| 54 |
], |
| 55 |
) |
| 56 |
def test_parse_with_nested_pydantic_model(self, snapshot_client: Anthropic) -> None: |
| 57 |
"""Test sync messages.parse() with nested Pydantic models.""" |
| 58 |
|
| 59 |
response = snapshot_client.messages.parse( |
| 60 |
model="claude-sonnet-4-5", |
| 61 |
messages=[ |
| 62 |
{ |
| 63 |
"role": "user", |
| 64 |
"content": "Extract order: 2 Green Tea at $5.50 and 1 Coffee at $3.00. Total $14.", |
| 65 |
} |
| 66 |
], |
| 67 |
output_format=OrderDetails, |
| 68 |
max_tokens=1024, |
| 69 |
) |
| 70 |
|
| 71 |
assert response.parsed_output is not None |
| 72 |
assert isinstance(response.parsed_output, OrderDetails) |
| 73 |
assert len(response.parsed_output.items) == 2 |
| 74 |
assert response.parsed_output.items[0].product_name == "Green Tea" |
| 75 |
assert response.parsed_output.total == 14.0 |
| 76 |
|
| 77 |
|
| 78 |
@pytest.mark.skipif(_compat.PYDANTIC_V1, reason="structured outputs not supported with pydantic v1") |
| 79 |
class TestAsyncParse: |
| 80 |
@pytest.mark.parametrize( |
| 81 |
"http_snapshot", |
| 82 |
[ |
| 83 |
cast(Any, external("uuid:924cfccc-c863-44ed-9838-c36b37416eaf.json")), |
| 84 |
], |
| 85 |
) |
| 86 |
async def test_parse_with_pydantic_model(self, async_snapshot_client: AsyncAnthropic) -> None: |
| 87 |
"""Test async messages.parse() with a Pydantic model output_format.""" |
| 88 |
|
| 89 |
response = await async_snapshot_client.messages.parse( |
| 90 |
model="claude-sonnet-4-5", |
| 91 |
messages=[ |
| 92 |
{ |
| 93 |
"role": "user", |
| 94 |
"content": "Extract: I want to order 2 Green Tea at $5.50 each", |
| 95 |
} |
| 96 |
], |
| 97 |
output_format=OrderItem, |
| 98 |
max_tokens=1024, |
| 99 |
) |
| 100 |
|
| 101 |
assert response.parsed_output is not None |
| 102 |
assert isinstance(response.parsed_output, OrderItem) |
| 103 |
assert response.parsed_output.product_name == "Green Tea" |
| 104 |
assert response.parsed_output.price == 5.5 |
| 105 |
assert response.parsed_output.quantity == 2 |
| 106 |
|
| 107 |
@pytest.mark.parametrize( |
| 108 |
"http_snapshot", |
| 109 |
[ |
| 110 |
cast(Any, external("uuid:d743d628-16aa-4c9f-ae78-51deb578f746.json")), |
| 111 |
], |
| 112 |
) |
| 113 |
async def test_parse_with_nested_pydantic_model(self, async_snapshot_client: AsyncAnthropic) -> None: |
| 114 |
"""Test async messages.parse() with nested Pydantic models.""" |
| 115 |
|
| 116 |
response = await async_snapshot_client.messages.parse( |
| 117 |
model="claude-sonnet-4-5", |
| 118 |
messages=[ |
| 119 |
{ |
| 120 |
"role": "user", |
| 121 |
"content": "Extract order: 2 Green Tea at $5.50 and 1 Coffee at $3.00. Total $14.", |
| 122 |
} |
| 123 |
], |
| 124 |
output_format=OrderDetails, |
| 125 |
max_tokens=1024, |
| 126 |
) |
| 127 |
|
| 128 |
assert response.parsed_output is not None |
| 129 |
assert isinstance(response.parsed_output, OrderDetails) |
| 130 |
assert len(response.parsed_output.items) == 2 |
| 131 |
assert response.parsed_output.items[0].product_name == "Green Tea" |
| 132 |
assert response.parsed_output.total == 14.0 |
| 133 |
|
| 134 |
|
| 135 |
@pytest.mark.skipif(_compat.PYDANTIC_V1, reason="structured outputs not supported with pydantic v1") |
| 136 |
class TestAsyncStream: |
| 137 |
@pytest.mark.parametrize( |
| 138 |
"http_snapshot", |
| 139 |
[ |
| 140 |
cast(Any, external("uuid:a8b7dbb8-2321-4624-abdb-7ba2136b5c38.json")), |
| 141 |
], |
| 142 |
) |
| 143 |
async def test_stream_with_raw_schema(self, async_snapshot_client: AsyncAnthropic) -> None: |
| 144 |
"""Test async messages.stream() with raw JSON schema via output_config.""" |
| 145 |
|
| 146 |
async with async_snapshot_client.messages.stream( |
| 147 |
model="claude-sonnet-4-5", |
| 148 |
messages=[ |
| 149 |
{ |
| 150 |
"role": "user", |
| 151 |
"content": "Extract order IDs from the following text:\n\nOrder 12345\nOrder 67890", |
| 152 |
} |
| 153 |
], |
| 154 |
output_config={ |
| 155 |
"format": { |
| 156 |
"type": "json_schema", |
| 157 |
"schema": { |
| 158 |
"type": "array", |
| 159 |
"items": {"type": "integer"}, |
| 160 |
}, |
| 161 |
}, |
| 162 |
}, |
| 163 |
max_tokens=1024, |
| 164 |
) as stream: |
| 165 |
response = await stream.get_final_message() |
| 166 |
|
| 167 |
content_block = response.content[0] |
| 168 |
assert content_block.type == "text" |
| 169 |
assert content_block.text == snapshot("[12345,67890]") |