-
Notifications
You must be signed in to change notification settings - Fork 547
Expand file tree
/
Copy pathtest_parse_text.py
More file actions
254 lines (214 loc) · 9.97 KB
/
test_parse_text.py
File metadata and controls
254 lines (214 loc) · 9.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
"""Unit tests for parse_text() and parse_response() edge-cases.
Covers issue #1204: structured output + thinking + tool use crashes.
Bug 1 – empty text block: when the model returns stop_reason="end_turn" with a
thinking block and an empty text block, parse_text("") used to crash
because validate_json("") raises ValidationError.
Bug 2 – malformed JSON prefix: when the model prefixes the JSON payload with
reasoning text, validate_json() raises ValidationError on the full
string. parse_text() now tries to salvage the last JSON object from
the text before re-raising.
"""
from __future__ import annotations
import pytest
from pydantic import BaseModel, ValidationError
from anthropic import _compat
from anthropic.lib._parse._response import _extract_last_json, parse_text
from anthropic.lib._parse._response import parse_response, parse_beta_response
from anthropic._models import construct_type_unchecked
from anthropic.types.message import Message
from anthropic.types.beta.beta_message import BetaMessage
# ---------------------------------------------------------------------------
# Shared model used across tests
# ---------------------------------------------------------------------------
class Location(BaseModel):
city: str
country: str
# ---------------------------------------------------------------------------
# parse_text() — unit tests
# ---------------------------------------------------------------------------
@pytest.mark.skipif(_compat.PYDANTIC_V1, reason="structured outputs not supported with pydantic v1")
class TestParseText:
def test_returns_none_when_no_output_format(self) -> None:
from anthropic._types import NOT_GIVEN
result = parse_text("anything", NOT_GIVEN)
assert result is None
def test_empty_string_returns_none(self) -> None:
result = parse_text("", Location)
assert result is None
def test_whitespace_only_returns_none(self) -> None:
result = parse_text(" \n\t ", Location)
assert result is None
def test_valid_json_parses_correctly(self) -> None:
result = parse_text('{"city": "Paris", "country": "France"}', Location)
assert isinstance(result, Location)
assert result.city == "Paris"
assert result.country == "France"
def test_malformed_prefix_recovers_last_json(self) -> None:
"""Model prefixed the JSON with thinking text — recovery should work."""
text = 'partial garbage\n\n{"city": "Tokyo", "country": "Japan"}'
result = parse_text(text, Location)
assert isinstance(result, Location)
assert result.city == "Tokyo"
assert result.country == "Japan"
def test_malformed_prefix_with_partial_object_recovers_last_json(self) -> None:
"""Prefix contains a broken JSON object before the final valid payload."""
text = '{"city": "Unfinished\ntail garbage\n{"city": "Berlin", "country": "Germany"}'
result = parse_text(text, Location)
assert isinstance(result, Location)
assert result.city == "Berlin"
assert result.country == "Germany"
def test_completely_invalid_text_raises_validation_error(self) -> None:
with pytest.raises(ValidationError):
parse_text("this is not json at all", Location)
def test_recovery_fails_gracefully_on_bad_json(self) -> None:
"""Even if we find a JSON-like fragment it must still validate against the schema."""
text = 'prefix text {"wrong_field": 123}'
with pytest.raises(ValidationError):
parse_text(text, Location)
# ---------------------------------------------------------------------------
# _extract_last_json() — unit tests
# ---------------------------------------------------------------------------
class TestExtractLastJson:
def test_plain_json_object(self) -> None:
assert _extract_last_json('{"a": 1}') == '{"a": 1}'
def test_json_with_prefix(self) -> None:
assert _extract_last_json('some prefix {"a": 1}') == '{"a": 1}'
def test_json_array(self) -> None:
assert _extract_last_json('[1, 2, 3]') == '[1, 2, 3]'
def test_no_json_returns_none(self) -> None:
assert _extract_last_json("no json here") is None
def test_nested_objects(self) -> None:
text = 'noise {"outer": {"inner": 42}}'
result = _extract_last_json(text)
assert result == '{"outer": {"inner": 42}}'
def test_string_containing_braces(self) -> None:
text = 'noise {"key": "value with } brace"}'
result = _extract_last_json(text)
assert result == '{"key": "value with } brace"}'
# ---------------------------------------------------------------------------
# parse_response() — integration-style tests using constructed Message objects
# ---------------------------------------------------------------------------
def _make_message(stop_reason: str, content_dicts: list) -> Message:
"""Build a minimal Message object via construct_type_unchecked."""
return construct_type_unchecked(
type_=Message,
value={
"id": "msg_test",
"type": "message",
"role": "assistant",
"model": "claude-test",
"stop_reason": stop_reason,
"stop_sequence": None,
"usage": {"input_tokens": 10, "output_tokens": 20},
"content": content_dicts,
},
)
def _make_beta_message(stop_reason: str, content_dicts: list) -> BetaMessage:
return construct_type_unchecked(
type_=BetaMessage,
value={
"id": "msg_test",
"type": "message",
"role": "assistant",
"model": "claude-test",
"stop_reason": stop_reason,
"stop_sequence": None,
"usage": {"input_tokens": 10, "output_tokens": 20},
"content": content_dicts,
},
)
@pytest.mark.skipif(_compat.PYDANTIC_V1, reason="structured outputs not supported with pydantic v1")
class TestParseResponse:
def test_end_turn_with_valid_json_parses(self) -> None:
msg = _make_message(
stop_reason="end_turn",
content_dicts=[
{"type": "text", "text": '{"city": "Berlin", "country": "Germany"}'},
],
)
parsed = parse_response(output_format=Location, response=msg)
assert parsed.stop_reason == "end_turn"
text_block = parsed.content[0]
assert text_block.type == "text"
assert isinstance(text_block.parsed_output, Location)
assert text_block.parsed_output.city == "Berlin"
def test_tool_use_turn_does_not_parse_text(self) -> None:
"""stop_reason=tool_use: text blocks must not be parsed as structured output."""
msg = _make_message(
stop_reason="tool_use",
content_dicts=[
{"type": "thinking", "thinking": "I need to call a tool.", "signature": "sig123"},
{"type": "text", "text": ""},
{
"type": "tool_use",
"id": "tool_abc",
"name": "get_weather",
"input": {"location": "London"},
},
],
)
# Must not raise even though empty text block is present
parsed = parse_response(output_format=Location, response=msg)
assert parsed.stop_reason == "tool_use"
text_block = next(b for b in parsed.content if b.type == "text")
assert text_block.parsed_output is None
def test_end_turn_with_empty_text_block_does_not_crash(self) -> None:
"""stop_reason=end_turn with thinking block + empty text: no crash."""
msg = _make_message(
stop_reason="end_turn",
content_dicts=[
{"type": "thinking", "thinking": "Hmm.", "signature": "sig456"},
{"type": "text", "text": ""},
],
)
parsed = parse_response(output_format=Location, response=msg)
text_block = next(b for b in parsed.content if b.type == "text")
assert text_block.parsed_output is None
def test_tool_use_turn_no_output_format(self) -> None:
"""Without output_format everything should still work fine."""
from anthropic._types import NOT_GIVEN
msg = _make_message(
stop_reason="tool_use",
content_dicts=[
{"type": "text", "text": ""},
{
"type": "tool_use",
"id": "tool_xyz",
"name": "search",
"input": {},
},
],
)
parsed = parse_response(output_format=NOT_GIVEN, response=msg)
assert parsed.stop_reason == "tool_use"
@pytest.mark.skipif(_compat.PYDANTIC_V1, reason="structured outputs not supported with pydantic v1")
class TestParseBetaResponse:
def test_end_turn_with_valid_json_parses(self) -> None:
msg = _make_beta_message(
stop_reason="end_turn",
content_dicts=[
{"type": "text", "text": '{"city": "Rome", "country": "Italy"}'},
],
)
parsed = parse_beta_response(output_format=Location, response=msg)
text_block = parsed.content[0]
assert text_block.type == "text"
assert isinstance(text_block.parsed_output, Location)
assert text_block.parsed_output.city == "Rome"
def test_tool_use_turn_does_not_parse_text(self) -> None:
msg = _make_beta_message(
stop_reason="tool_use",
content_dicts=[
{"type": "thinking", "thinking": "Calling a tool.", "signature": "sig789"},
{"type": "text", "text": ""},
{
"type": "tool_use",
"id": "tool_def",
"name": "lookup",
"input": {},
},
],
)
parsed = parse_beta_response(output_format=Location, response=msg)
text_block = next(b for b in parsed.content if b.type == "text")
assert text_block.parsed_output is None