Skip to content

Commit 1939fb9

Browse files
refactor: Remove internal function usage in test_pydantic_ai
1 parent 5d6cf7e commit 1939fb9

1 file changed

Lines changed: 37 additions & 73 deletions

File tree

tests/integrations/pydantic_ai/test_pydantic_ai.py

Lines changed: 37 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,7 +1637,7 @@ async def test_input_messages_error_handling(sentry_init, capture_events):
16371637
Test that _set_input_messages handles errors gracefully.
16381638
"""
16391639
import sentry_sdk
1640-
from sentry_sdk.integrations.pydantic_ai.spans.ai_client import _set_input_messages
1640+
from sentry_sdk.integrations.pydantic_ai.spans.ai_client import ai_client_span
16411641

16421642
sentry_init(
16431643
integrations=[PydanticAIIntegration()],
@@ -1646,14 +1646,11 @@ async def test_input_messages_error_handling(sentry_init, capture_events):
16461646
)
16471647

16481648
with sentry_sdk.start_transaction(op="test", name="test") as transaction:
1649-
span = sentry_sdk.start_span(op="test_span")
1650-
16511649
# Pass invalid messages that would cause an error
16521650
invalid_messages = [object()] # Plain object without expected attributes
16531651

1654-
# Should not raise, error is caught internally
1655-
_set_input_messages(span, invalid_messages)
1656-
1652+
# Should not raise, error is caught internally via ai_client_span
1653+
span = ai_client_span(invalid_messages, None, None, None)
16571654
span.finish()
16581655

16591656
# Should not crash
@@ -1789,34 +1786,21 @@ async def test_message_parts_with_list_content(sentry_init, capture_events):
17891786
"""
17901787
Test that message parts with list content are handled correctly.
17911788
"""
1792-
import sentry_sdk
1793-
from unittest.mock import MagicMock
1794-
from sentry_sdk.integrations.pydantic_ai.spans.ai_client import _set_input_messages
1789+
from pydantic_ai import Agent
17951790

17961791
sentry_init(
17971792
integrations=[PydanticAIIntegration()],
17981793
traces_sample_rate=1.0,
17991794
send_default_pii=True,
18001795
)
18011796

1802-
with sentry_sdk.start_transaction(op="test", name="test") as transaction:
1803-
span = sentry_sdk.start_span(op="test_span")
1804-
1805-
# Create message with list content
1806-
mock_msg = MagicMock()
1807-
mock_part = MagicMock()
1808-
mock_part.content = ["item1", "item2", {"complex": "item"}]
1809-
mock_msg.parts = [mock_part]
1810-
mock_msg.instructions = None
1811-
1812-
messages = [mock_msg]
1813-
1814-
# Should handle list content
1815-
_set_input_messages(span, messages)
1797+
events = capture_events()
1798+
agent = Agent("test")
18161799

1817-
span.finish()
1800+
# Run with list content
1801+
await agent.run(["item1", "item2"])
18181802

1819-
# Should not crash
1803+
(transaction,) = events
18201804
assert transaction is not None
18211805

18221806

@@ -1896,35 +1880,27 @@ async def test_message_with_system_prompt_part(sentry_init, capture_events):
18961880
"""
18971881
Test that SystemPromptPart is handled with correct role.
18981882
"""
1899-
import sentry_sdk
1900-
from unittest.mock import MagicMock
1901-
from sentry_sdk.integrations.pydantic_ai.spans.ai_client import _set_input_messages
1902-
from pydantic_ai import messages
1883+
from pydantic_ai import Agent, messages
19031884

19041885
sentry_init(
19051886
integrations=[PydanticAIIntegration()],
19061887
traces_sample_rate=1.0,
19071888
send_default_pii=True,
19081889
)
19091890

1910-
with sentry_sdk.start_transaction(op="test", name="test") as transaction:
1911-
span = sentry_sdk.start_span(op="test_span")
1912-
1913-
# Create message with SystemPromptPart
1914-
system_part = messages.SystemPromptPart(content="You are a helpful assistant")
1915-
1916-
mock_msg = MagicMock()
1917-
mock_msg.parts = [system_part]
1918-
mock_msg.instructions = None
1919-
1920-
msgs = [mock_msg]
1891+
events = capture_events()
1892+
agent = Agent("test")
19211893

1922-
# Should handle system prompt
1923-
_set_input_messages(span, msgs)
1894+
# Create message with SystemPromptPart
1895+
system_part = messages.SystemPromptPart(content="You are a helpful assistant")
1896+
history = [
1897+
messages.ModelRequest(parts=[system_part])
1898+
]
19241899

1925-
span.finish()
1900+
# Run with history
1901+
await agent.run("What did I say?", message_history=history)
19261902

1927-
# Should not crash
1903+
(transaction,) = events
19281904
assert transaction is not None
19291905

19301906

@@ -1935,7 +1911,7 @@ async def test_message_with_instructions(sentry_init, capture_events):
19351911
"""
19361912
import sentry_sdk
19371913
from unittest.mock import MagicMock
1938-
from sentry_sdk.integrations.pydantic_ai.spans.ai_client import _set_input_messages
1914+
from sentry_sdk.integrations.pydantic_ai.spans.ai_client import ai_client_span
19391915

19401916
sentry_init(
19411917
integrations=[PydanticAIIntegration()],
@@ -1944,8 +1920,6 @@ async def test_message_with_instructions(sentry_init, capture_events):
19441920
)
19451921

19461922
with sentry_sdk.start_transaction(op="test", name="test") as transaction:
1947-
span = sentry_sdk.start_span(op="test_span")
1948-
19491923
# Create message with instructions
19501924
mock_msg = MagicMock()
19511925
mock_msg.instructions = "System instructions here"
@@ -1955,9 +1929,8 @@ async def test_message_with_instructions(sentry_init, capture_events):
19551929

19561930
msgs = [mock_msg]
19571931

1958-
# Should extract system prompt from instructions
1959-
_set_input_messages(span, msgs)
1960-
1932+
# Should handle system prompt via ai_client_span wrappers
1933+
span = ai_client_span(msgs, None, None, None)
19611934
span.finish()
19621935

19631936
# Should not crash
@@ -1969,25 +1942,21 @@ async def test_set_input_messages_without_prompts(sentry_init, capture_events):
19691942
"""
19701943
Test that _set_input_messages respects _should_send_prompts().
19711944
"""
1972-
import sentry_sdk
1973-
from sentry_sdk.integrations.pydantic_ai.spans.ai_client import _set_input_messages
1945+
from pydantic_ai import Agent
19741946

19751947
sentry_init(
19761948
integrations=[PydanticAIIntegration(include_prompts=False)],
19771949
traces_sample_rate=1.0,
19781950
send_default_pii=True,
19791951
)
19801952

1981-
with sentry_sdk.start_transaction(op="test", name="test") as transaction:
1982-
span = sentry_sdk.start_span(op="test_span")
1953+
events = capture_events()
1954+
agent = Agent("test")
19831955

1984-
# Even with messages, should not set them
1985-
messages = ["test"]
1986-
_set_input_messages(span, messages)
1956+
# Run with prompts disabled
1957+
await agent.run("test")
19871958

1988-
span.finish()
1989-
1990-
# Should not crash and should not set messages
1959+
(transaction,) = events
19911960
assert transaction is not None
19921961

19931962

@@ -2705,28 +2674,23 @@ async def test_binary_content_encoding_image(sentry_init, capture_events):
27052674
@pytest.mark.asyncio
27062675
async def test_binary_content_encoding_mixed_content(sentry_init, capture_events):
27072676
"""Test that BinaryContent mixed with text content is properly handled."""
2677+
from pydantic_ai import Agent
2678+
27082679
sentry_init(
27092680
integrations=[PydanticAIIntegration()],
27102681
traces_sample_rate=1.0,
27112682
send_default_pii=True,
27122683
)
27132684

27142685
events = capture_events()
2686+
agent = Agent("test")
27152687

2716-
with sentry_sdk.start_transaction(op="test", name="test"):
2717-
span = sentry_sdk.start_span(op="test_span")
2718-
binary_content = BinaryContent(
2719-
data=b"fake_image_bytes", media_type="image/jpeg"
2720-
)
2721-
user_part = UserPromptPart(
2722-
content=["Here is an image:", binary_content, "What do you see?"]
2723-
)
2724-
mock_msg = MagicMock()
2725-
mock_msg.parts = [user_part]
2726-
mock_msg.instructions = None
2688+
binary_content = BinaryContent(
2689+
data=b"fake_image_bytes", media_type="image/jpeg"
2690+
)
27272691

2728-
_set_input_messages(span, [mock_msg])
2729-
span.finish()
2692+
# Run with mixed content
2693+
await agent.run(["Here is an image:", binary_content, "What do you see?"])
27302694

27312695
(event,) = events
27322696
span_data = event["spans"][0]["data"]

0 commit comments

Comments
 (0)