Skip to content

Commit 371c1bc

Browse files
add a streaming test
1 parent 2d03ece commit 371c1bc

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

tests/integrations/pydantic_ai/test_pydantic_ai.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,65 @@ async def test_invoke_agent_with_instructions(
12561256
assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS not in chat_span["data"]
12571257

12581258

1259+
@pytest.mark.asyncio
1260+
@pytest.mark.parametrize(
1261+
"send_default_pii, include_prompts",
1262+
[
1263+
(True, True),
1264+
(True, False),
1265+
(False, True),
1266+
(False, False),
1267+
],
1268+
)
1269+
async def test_invoke_agent_streaming_with_instructions(
1270+
sentry_init, capture_events, send_default_pii, include_prompts
1271+
):
1272+
"""
1273+
Test that invoke_agent span handles instructions correctly.
1274+
"""
1275+
from pydantic_ai import Agent
1276+
1277+
# Create agent with instructions (can be string or list)
1278+
agent = Agent(
1279+
"test",
1280+
name="test_instructions",
1281+
)
1282+
1283+
# Add instructions via _instructions attribute (internal API)
1284+
agent._instructions = ["Instruction 1", "Instruction 2"]
1285+
agent._system_prompts = ["System prompt"]
1286+
1287+
sentry_init(
1288+
integrations=[PydanticAIIntegration(include_prompts=include_prompts)],
1289+
traces_sample_rate=1.0,
1290+
send_default_pii=send_default_pii,
1291+
)
1292+
1293+
events = capture_events()
1294+
1295+
async with agent.run_stream("Test input") as result:
1296+
async for _ in result.stream_output():
1297+
pass
1298+
1299+
(transaction,) = events
1300+
spans = transaction["spans"]
1301+
1302+
# The transaction IS the invoke_agent span, check for messages in chat spans instead
1303+
chat_spans = [s for s in spans if s["op"] == "gen_ai.chat"]
1304+
assert len(chat_spans) >= 1
1305+
1306+
chat_span = chat_spans[0]
1307+
1308+
if send_default_pii and include_prompts:
1309+
system_instructions = chat_span["data"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS]
1310+
assert json.loads(system_instructions) == [
1311+
{"type": "text", "content": "System prompt"},
1312+
{"type": "text", "content": "Instruction 1\nInstruction 2"},
1313+
]
1314+
else:
1315+
assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS not in chat_span["data"]
1316+
1317+
12591318
@pytest.mark.asyncio
12601319
async def test_model_name_extraction_with_callable(sentry_init, capture_events):
12611320
"""

0 commit comments

Comments
 (0)