Skip to content

Commit 1356704

Browse files
remove some local vars in tests
1 parent fc69b80 commit 1356704

2 files changed

Lines changed: 65 additions & 95 deletions

File tree

‎sentry_sdk/integrations/anthropic.py‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,16 @@ def _collect_ai_data(
387387
content_blocks.append({"type": "thinking", "thinking": ""})
388388
elif event.content_block.type in ("text", "tool_use"):
389389
content_blocks.append({"type": "text", "text": ""})
390-
elif event.type == "content_block_delta" and content_blocks:
391-
delta = event.delta
392-
if ThinkingDelta is not None and isinstance(delta, ThinkingDelta):
393-
content_blocks[-1]["thinking"] += delta.thinking
394-
elif hasattr(delta, "text"):
395-
content_blocks[-1]["text"] += delta.text
396-
elif hasattr(delta, "partial_json"):
397-
content_blocks[-1]["text"] += delta.partial_json
390+
else:
391+
content_blocks.append({"type": event.content_block.type})
392+
elif event.type == "content_block_delta":
393+
block = content_blocks[event.index]
394+
if ThinkingDelta is not None and isinstance(event.delta, ThinkingDelta):
395+
block["thinking"] += event.delta.thinking
396+
elif hasattr(event.delta, "text"):
397+
block["text"] += event.delta.text
398+
elif hasattr(event.delta, "partial_json"):
399+
block["text"] += event.delta.partial_json
398400
elif event.type == "content_block_stop":
399401
pass
400402

‎tests/integrations/anthropic/test_anthropic.py‎

Lines changed: 55 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -222,42 +222,46 @@ def inner(text_deltas, stop_reason):
222222

223223
@pytest.fixture
224224
def streaming_anthropic_text_response():
225-
def inner(text_deltas, stop_reason):
226-
return [
227-
MessageStartEvent(
228-
message=Message(
229-
id="msg_01XFDUDYJgAACzvnptvVoYEL",
230-
model="model",
231-
role="assistant",
232-
content=[TextBlock(type="text", text="")],
233-
type="message",
234-
stop_reason="end_turn",
235-
usage=Usage(input_tokens=10, output_tokens=20),
236-
),
237-
type="message_start",
238-
),
239-
ContentBlockStartEvent(
240-
type="content_block_start",
241-
index=0,
242-
content_block=TextBlock(type="text", text=""),
243-
),
244-
*[
245-
ContentBlockDeltaEvent(
246-
delta=TextDelta(text=text, type="text_delta"),
247-
index=0,
248-
type="content_block_delta",
249-
)
250-
for text in text_deltas
251-
],
252-
ContentBlockStopEvent(type="content_block_stop", index=0),
253-
MessageDeltaEvent(
254-
delta=Delta(stop_reason=stop_reason),
255-
usage=MessageDeltaUsage(output_tokens=10),
256-
type="message_delta",
225+
return [
226+
MessageStartEvent(
227+
message=Message(
228+
id="msg_01XFDUDYJgAACzvnptvVoYEL",
229+
model="model",
230+
role="assistant",
231+
content=[TextBlock(type="text", text="")],
232+
type="message",
233+
stop_reason="end_turn",
234+
usage=Usage(input_tokens=10, output_tokens=20),
257235
),
258-
]
259-
260-
return inner
236+
type="message_start",
237+
),
238+
ContentBlockStartEvent(
239+
type="content_block_start",
240+
index=0,
241+
content_block=TextBlock(type="text", text=""),
242+
),
243+
ContentBlockDeltaEvent(
244+
delta=TextDelta(text="Hi", type="text_delta"),
245+
index=0,
246+
type="content_block_delta",
247+
),
248+
ContentBlockDeltaEvent(
249+
delta=TextDelta(text="!", type="text_delta"),
250+
index=0,
251+
type="content_block_delta",
252+
),
253+
ContentBlockDeltaEvent(
254+
delta=TextDelta(text=" I'm Claude!", type="text_delta"),
255+
index=0,
256+
type="content_block_delta",
257+
),
258+
ContentBlockStopEvent(type="content_block_stop", index=0),
259+
MessageDeltaEvent(
260+
delta=Delta(stop_reason="max_tokens"),
261+
usage=MessageDeltaUsage(output_tokens=10),
262+
type="message_delta",
263+
),
264+
]
261265

262266

263267
@pytest.fixture
@@ -1868,12 +1872,7 @@ def test_streaming_create_message_close(
18681872
client = Anthropic(api_key="z")
18691873

18701874
response = get_model_response(
1871-
server_side_event_chunks(
1872-
streaming_anthropic_text_response(
1873-
text_deltas=("Hi", "!", " I'm Claude!"),
1874-
stop_reason="max_tokens",
1875-
)
1876-
)
1875+
server_side_event_chunks(streaming_anthropic_text_response)
18771876
)
18781877

18791878
sentry_init(
@@ -1926,9 +1925,8 @@ def test_streaming_create_message_close(
19261925
span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
19271926
== '[{"role": "user", "content": "Hello, Claude"}]'
19281927
)
1929-
expected_parts = [{"type": "text", "content": "Hi!"}]
19301928
assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [
1931-
{"role": "assistant", "parts": expected_parts}
1929+
{"role": "assistant", "parts": [{"type": "text", "content": "Hi!"}]}
19321930
]
19331931

19341932
assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10
@@ -1975,9 +1973,8 @@ def test_streaming_create_message_close(
19751973
span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
19761974
== '[{"role": "user", "content": "Hello, Claude"}]'
19771975
)
1978-
expected_parts = [{"type": "text", "content": "Hi!"}]
19791976
assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [
1980-
{"role": "assistant", "parts": expected_parts}
1977+
{"role": "assistant", "parts": [{"type": "text", "content": "Hi!"}]}
19811978
]
19821979

19831980
assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10
@@ -2023,9 +2020,8 @@ def test_streaming_create_message_close(
20232020
span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
20242021
== '[{"role": "user", "content": "Hello, Claude"}]'
20252022
)
2026-
expected_parts = [{"type": "text", "content": "Hi!"}]
20272023
assert json.loads(span["data"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [
2028-
{"role": "assistant", "parts": expected_parts}
2024+
{"role": "assistant", "parts": [{"type": "text", "content": "Hi!"}]}
20292025
]
20302026

20312027
assert span["data"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10
@@ -2636,12 +2632,7 @@ def test_stream_messages_close(
26362632
client = Anthropic(api_key="z")
26372633

26382634
response = get_model_response(
2639-
server_side_event_chunks(
2640-
streaming_anthropic_text_response(
2641-
text_deltas=("Hi", "!", " I'm Claude!"),
2642-
stop_reason="max_tokens",
2643-
)
2644-
)
2635+
server_side_event_chunks(streaming_anthropic_text_response)
26452636
)
26462637

26472638
sentry_init(
@@ -2698,9 +2689,8 @@ def test_stream_messages_close(
26982689
span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
26992690
== '[{"role": "user", "content": "Hello, Claude"}]'
27002691
)
2701-
expected_parts = [{"type": "text", "content": "Hi!"}]
27022692
assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [
2703-
{"role": "assistant", "parts": expected_parts}
2693+
{"role": "assistant", "parts": [{"type": "text", "content": "Hi!"}]}
27042694
]
27052695

27062696
assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10
@@ -2751,9 +2741,8 @@ def test_stream_messages_close(
27512741
span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
27522742
== '[{"role": "user", "content": "Hello, Claude"}]'
27532743
)
2754-
expected_parts = [{"type": "text", "content": "Hi!"}]
27552744
assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [
2756-
{"role": "assistant", "parts": expected_parts}
2745+
{"role": "assistant", "parts": [{"type": "text", "content": "Hi!"}]}
27572746
]
27582747

27592748
assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10
@@ -2803,9 +2792,8 @@ def test_stream_messages_close(
28032792
span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
28042793
== '[{"role": "user", "content": "Hello, Claude"}]'
28052794
)
2806-
expected_parts = [{"type": "text", "content": "Hi!"}]
28072795
assert json.loads(span["data"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [
2808-
{"role": "assistant", "parts": expected_parts}
2796+
{"role": "assistant", "parts": [{"type": "text", "content": "Hi!"}]}
28092797
]
28102798

28112799
assert span["data"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10
@@ -3428,14 +3416,7 @@ async def test_streaming_create_message_async_close(
34283416
client = AsyncAnthropic(api_key="z")
34293417

34303418
response = get_model_response(
3431-
async_iterator(
3432-
server_side_event_chunks(
3433-
streaming_anthropic_text_response(
3434-
text_deltas=("Hi", "!", " I'm Claude!"),
3435-
stop_reason="max_tokens",
3436-
)
3437-
)
3438-
)
3419+
async_iterator(server_side_event_chunks(streaming_anthropic_text_response))
34393420
)
34403421

34413422
sentry_init(
@@ -3487,9 +3468,8 @@ async def test_streaming_create_message_async_close(
34873468
span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
34883469
== '[{"role": "user", "content": "Hello, Claude"}]'
34893470
)
3490-
expected_parts = [{"type": "text", "content": "Hi!"}]
34913471
assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [
3492-
{"role": "assistant", "parts": expected_parts}
3472+
{"role": "assistant", "parts": [{"type": "text", "content": "Hi!"}]}
34933473
]
34943474

34953475
assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10
@@ -3535,9 +3515,8 @@ async def test_streaming_create_message_async_close(
35353515
span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
35363516
== '[{"role": "user", "content": "Hello, Claude"}]'
35373517
)
3538-
expected_parts = [{"type": "text", "content": "Hi!"}]
35393518
assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [
3540-
{"role": "assistant", "parts": expected_parts}
3519+
{"role": "assistant", "parts": [{"type": "text", "content": "Hi!"}]}
35413520
]
35423521

35433522
assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10
@@ -3582,9 +3561,8 @@ async def test_streaming_create_message_async_close(
35823561
span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
35833562
== '[{"role": "user", "content": "Hello, Claude"}]'
35843563
)
3585-
expected_parts = [{"type": "text", "content": "Hi!"}]
35863564
assert json.loads(span["data"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [
3587-
{"role": "assistant", "parts": expected_parts}
3565+
{"role": "assistant", "parts": [{"type": "text", "content": "Hi!"}]}
35883566
]
35893567

35903568
assert span["data"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10
@@ -4386,14 +4364,7 @@ async def test_stream_messages_async_close(
43864364
client = AsyncAnthropic(api_key="z")
43874365

43884366
response = get_model_response(
4389-
async_iterator(
4390-
server_side_event_chunks(
4391-
streaming_anthropic_text_response(
4392-
text_deltas=("Hi", "!", " I'm Claude!"),
4393-
stop_reason="max_tokens",
4394-
)
4395-
)
4396-
)
4367+
async_iterator(server_side_event_chunks(streaming_anthropic_text_response))
43974368
)
43984369

43994370
sentry_init(
@@ -4453,9 +4424,8 @@ async def test_stream_messages_async_close(
44534424
span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
44544425
== '[{"role": "user", "content": "Hello, Claude"}]'
44554426
)
4456-
expected_parts = [{"type": "text", "content": "Hi!"}]
44574427
assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [
4458-
{"role": "assistant", "parts": expected_parts}
4428+
{"role": "assistant", "parts": [{"type": "text", "content": "Hi!"}]}
44594429
]
44604430

44614431
assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10
@@ -4506,9 +4476,8 @@ async def test_stream_messages_async_close(
45064476
span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
45074477
== '[{"role": "user", "content": "Hello, Claude"}]'
45084478
)
4509-
expected_parts = [{"type": "text", "content": "Hi!"}]
45104479
assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [
4511-
{"role": "assistant", "parts": expected_parts}
4480+
{"role": "assistant", "parts": [{"type": "text", "content": "Hi!"}]}
45124481
]
45134482

45144483
assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10
@@ -4561,9 +4530,8 @@ async def test_stream_messages_async_close(
45614530
span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
45624531
== '[{"role": "user", "content": "Hello, Claude"}]'
45634532
)
4564-
expected_parts = [{"type": "text", "content": "Hi!"}]
45654533
assert json.loads(span["data"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [
4566-
{"role": "assistant", "parts": expected_parts}
4534+
{"role": "assistant", "parts": [{"type": "text", "content": "Hi!"}]}
45674535
]
45684536

45694537
assert span["data"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10

0 commit comments

Comments
 (0)