| 1 |
import json |
| 2 |
from typing import Any, cast |
| 3 |
|
| 4 |
import pytest |
| 5 |
from pydantic import BaseModel |
| 6 |
from inline_snapshot import external, snapshot |
| 7 |
|
| 8 |
from anthropic import AnthropicError, AsyncAnthropic, _compat |
| 9 |
from anthropic.types.beta.parsed_beta_message import ParsedBetaMessage |
| 10 |
|
| 11 |
|
| 12 |
@pytest.mark.skipif(_compat.PYDANTIC_V1, reason="tool runner not supported with pydantic v1") |
| 13 |
class TestAsyncMessages: |
| 14 |
@pytest.mark.parametrize( |
| 15 |
"http_snapshot", |
| 16 |
[ |
| 17 |
cast(Any, external("uuid:9381e2b7-7fa1-46f7-9f78-46dc8431ea9d.json")), |
| 18 |
], |
| 19 |
) |
| 20 |
async def test_stream_with_raw_schema(self, async_snapshot_client: AsyncAnthropic) -> None: |
| 21 |
async def async_stream_parse(client: AsyncAnthropic) -> ParsedBetaMessage[None]: |
| 22 |
async with client.beta.messages.stream( |
| 23 |
model="claude-sonnet-4-5", |
| 24 |
messages=[ |
| 25 |
{ |
| 26 |
"role": "user", |
| 27 |
"content": "Extract order IDs from the following text:\n\nOrder 12345\nOrder 67890", |
| 28 |
} |
| 29 |
], |
| 30 |
output_config={ |
| 31 |
"format": { |
| 32 |
"type": "json_schema", |
| 33 |
"schema": { |
| 34 |
"type": "array", |
| 35 |
"items": {"type": "integer"}, |
| 36 |
}, |
| 37 |
}, |
| 38 |
}, |
| 39 |
betas=["structured-outputs-2025-12-15"], |
| 40 |
max_tokens=1024, |
| 41 |
) as stream: |
| 42 |
return await stream.get_final_message() |
| 43 |
|
| 44 |
response = await async_stream_parse(async_snapshot_client) |
| 45 |
|
| 46 |
assert response.content[0].type == "text" |
| 47 |
assert response.content[0].text == snapshot("[12345,67890]") |
| 48 |
|
| 49 |
@pytest.mark.parametrize( |
| 50 |
"http_snapshot", |
| 51 |
[ |
| 52 |
cast(Any, external("uuid:d7092a7d-b723-4470-8fb0-da138cd103a1.json")), |
| 53 |
], |
| 54 |
) |
| 55 |
async def test_parse_uses_output_config(self, async_snapshot_client: AsyncAnthropic) -> None: |
| 56 |
class User(BaseModel): |
| 57 |
name: str |
| 58 |
age: int |
| 59 |
|
| 60 |
response = await async_snapshot_client.beta.with_raw_response.messages.parse( |
| 61 |
model="claude-sonnet-4-5", |
| 62 |
messages=[ |
| 63 |
{ |
| 64 |
"role": "user", |
| 65 |
"content": "Extract the user's name and age from the following text:\n\nMy name is John Doe and I am 30 years old.", |
| 66 |
} |
| 67 |
], |
| 68 |
output_format=User, |
| 69 |
max_tokens=1024, |
| 70 |
) |
| 71 |
|
| 72 |
request_json = json.loads(response.http_request.content) |
| 73 |
assert request_json == snapshot( |
| 74 |
{ |
| 75 |
"max_tokens": 1024, |
| 76 |
"messages": [ |
| 77 |
{ |
| 78 |
"role": "user", |
| 79 |
"content": """\ |
| 80 |
Extract the user's name and age from the following text: |
| 81 |
|
| 82 |
My name is John Doe and I am 30 years old.\ |
| 83 |
""", |
| 84 |
} |
| 85 |
], |
| 86 |
"model": "claude-sonnet-4-5", |
| 87 |
"output_config": { |
| 88 |
"format": { |
| 89 |
"schema": { |
| 90 |
"type": "object", |
| 91 |
"title": "User", |
| 92 |
"properties": { |
| 93 |
"name": {"type": "string", "title": "Name"}, |
| 94 |
"age": {"type": "integer", "title": "Age"}, |
| 95 |
}, |
| 96 |
"additionalProperties": False, |
| 97 |
"required": ["name", "age"], |
| 98 |
}, |
| 99 |
"type": "json_schema", |
| 100 |
} |
| 101 |
}, |
| 102 |
} |
| 103 |
) |
| 104 |
|
| 105 |
async def test_rejects_both_output_format_and_config(self, async_client: AsyncAnthropic) -> None: |
| 106 |
class User(BaseModel): |
| 107 |
name: str |
| 108 |
age: int |
| 109 |
|
| 110 |
with pytest.raises(AnthropicError, match="Both output_format and output_config.format were provided"): |
| 111 |
await async_client.beta.messages.parse( |
| 112 |
model="claude-sonnet-4-5", |
| 113 |
messages=[ |
| 114 |
{ |
| 115 |
"role": "user", |
| 116 |
"content": "Extract the user's name and age.", |
| 117 |
} |
| 118 |
], |
| 119 |
output_format=User, |
| 120 |
output_config={ |
| 121 |
"format": { |
| 122 |
"type": "json_schema", |
| 123 |
"schema": {"type": "object"}, |
| 124 |
} |
| 125 |
}, |
| 126 |
max_tokens=1024, |
| 127 |
) |