Debian Codegraph @

tests/lib/test_output_format_helpers.py

raw
1 """The typed `output_format=` argument of the hand-written helpers on the non-beta client."""
2
3 from __future__ import annotations
4
5 import json
6
7 import httpx2
8 import pytest
9 from respx import MockRouter
10 from pydantic import BaseModel
11
12 from anthropic import Anthropic, AsyncAnthropic, _compat
13
14
15 class Answer(BaseModel):
16 text: str
17
18
19 def _count(respx_mock: MockRouter) -> None:
20 respx_mock.post("/v1/messages/count_tokens").mock(return_value=httpx2.Response(200, json={"input_tokens": 7}))
21
22
23 def _last_body(respx_mock: MockRouter) -> dict[str, object]:
24 return json.loads(respx_mock.calls.last.request.content)
25
26
27 @pytest.mark.skipif(_compat.PYDANTIC_V1, reason="schema generation from models requires Pydantic v2")
28 def test_count_tokens_accepts_a_type(client: Anthropic, respx_mock: MockRouter) -> None:
29 _count(respx_mock)
30
31 client.messages.count_tokens(
32 model="claude-sonnet-4-5",
33 messages=[{"role": "user", "content": "hi"}],
34 output_format=Answer,
35 )
36
37 body = _last_body(respx_mock)
38 assert "output_format" not in body
39 fmt = body["output_config"]["format"] # type: ignore[index]
40 assert fmt["type"] == "json_schema"
41 assert fmt["schema"]["properties"]["text"]["type"] == "string"
42
43
44 @pytest.mark.skipif(_compat.PYDANTIC_V1, reason="schema generation from models requires Pydantic v2")
45 def test_count_tokens_merges_the_type_into_an_existing_output_config(client: Anthropic, respx_mock: MockRouter) -> None:
46 _count(respx_mock)
47
48 client.messages.count_tokens(
49 model="claude-sonnet-4-5",
50 messages=[{"role": "user", "content": "hi"}],
51 output_config={"effort": "high"},
52 output_format=Answer,
53 )
54
55 output_config = _last_body(respx_mock)["output_config"]
56 assert output_config["effort"] == "high" # type: ignore[index]
57 assert output_config["format"]["type"] == "json_schema" # type: ignore[index]
58
59
60 def test_count_tokens_rejects_a_schema_dict(client: Anthropic) -> None:
61 with pytest.raises(TypeError, match="output_config"):
62 client.messages.count_tokens(
63 model="claude-sonnet-4-5",
64 messages=[{"role": "user", "content": "hi"}],
65 output_format={"type": "json_schema", "schema": {"type": "object"}}, # type: ignore[arg-type]
66 )
67
68
69 def test_stream_rejects_a_schema_dict(client: Anthropic) -> None:
70 with pytest.raises(TypeError, match="output_config"):
71 client.messages.stream(
72 max_tokens=16,
73 model="claude-sonnet-4-5",
74 messages=[{"role": "user", "content": "hi"}],
75 output_format={"type": "json_schema", "schema": {"type": "object"}}, # type: ignore[arg-type]
76 )
77
78
79 def test_count_tokens_output_config_dict_still_works(client: Anthropic, respx_mock: MockRouter) -> None:
80 _count(respx_mock)
81
82 client.messages.count_tokens(
83 model="claude-sonnet-4-5",
84 messages=[{"role": "user", "content": "hi"}],
85 output_config={"format": {"type": "json_schema", "schema": {"type": "object"}}},
86 )
87
88 assert _last_body(respx_mock)["output_config"] == {"format": {"type": "json_schema", "schema": {"type": "object"}}}
89
90
91 @pytest.mark.skipif(_compat.PYDANTIC_V1, reason="schema generation from models requires Pydantic v2")
92 async def test_async_count_tokens_accepts_a_type(async_client: AsyncAnthropic, respx_mock: MockRouter) -> None:
93 _count(respx_mock)
94
95 await async_client.messages.count_tokens(
96 model="claude-sonnet-4-5",
97 messages=[{"role": "user", "content": "hi"}],
98 output_format=Answer,
99 )
100
101 assert _last_body(respx_mock)["output_config"]["format"]["type"] == "json_schema" # type: ignore[index]
102
103
104 async def test_async_count_tokens_rejects_a_schema_dict(async_client: AsyncAnthropic) -> None:
105 with pytest.raises(TypeError, match="output_config"):
106 await async_client.messages.count_tokens(
107 model="claude-sonnet-4-5",
108 messages=[{"role": "user", "content": "hi"}],
109 output_format={"type": "json_schema", "schema": {"type": "object"}}, # type: ignore[arg-type]
110 )