|
1 | 1 | import asyncio |
| 2 | +import json |
2 | 3 | import pytest |
| 4 | +from unittest.mock import MagicMock |
3 | 5 |
|
4 | 6 | from typing import Annotated |
5 | 7 | from pydantic import Field |
6 | 8 |
|
| 9 | +import sentry_sdk |
| 10 | +from sentry_sdk._types import BLOB_DATA_SUBSTITUTE |
7 | 11 | from sentry_sdk.integrations.pydantic_ai import PydanticAIIntegration |
| 12 | +from sentry_sdk.integrations.pydantic_ai.spans.ai_client import _set_input_messages |
8 | 13 |
|
9 | 14 | from pydantic_ai import Agent |
| 15 | +from pydantic_ai.messages import BinaryContent, UserPromptPart |
10 | 16 | from pydantic_ai.models.test import TestModel |
11 | 17 | from pydantic_ai.exceptions import ModelRetry, UnexpectedModelBehavior |
12 | 18 |
|
@@ -2604,3 +2610,123 @@ async def test_ai_client_span_gets_agent_from_scope(sentry_init, capture_events) |
2604 | 2610 |
|
2605 | 2611 | # Should not crash |
2606 | 2612 | assert transaction is not None |
| 2613 | + |
| 2614 | + |
| 2615 | +def _get_messages_from_span(span_data): |
| 2616 | + """Helper to extract and parse messages from span data.""" |
| 2617 | + messages_data = span_data["gen_ai.request.messages"] |
| 2618 | + return ( |
| 2619 | + json.loads(messages_data) if isinstance(messages_data, str) else messages_data |
| 2620 | + ) |
| 2621 | + |
| 2622 | + |
| 2623 | +def _find_binary_content(messages_data, expected_modality, expected_mime_type): |
| 2624 | + """Helper to find and verify binary content in messages.""" |
| 2625 | + for msg in messages_data: |
| 2626 | + if "content" not in msg: |
| 2627 | + continue |
| 2628 | + for content_item in msg["content"]: |
| 2629 | + if content_item.get("type") == "blob": |
| 2630 | + assert content_item["modality"] == expected_modality |
| 2631 | + assert content_item["mime_type"] == expected_mime_type |
| 2632 | + assert content_item["content"] == BLOB_DATA_SUBSTITUTE |
| 2633 | + return True |
| 2634 | + return False |
| 2635 | + |
| 2636 | + |
| 2637 | +@pytest.mark.asyncio |
| 2638 | +async def test_binary_content_encoding_image(sentry_init, capture_events): |
| 2639 | + """Test that BinaryContent with image data is properly encoded in messages.""" |
| 2640 | + sentry_init( |
| 2641 | + integrations=[PydanticAIIntegration()], |
| 2642 | + traces_sample_rate=1.0, |
| 2643 | + send_default_pii=True, |
| 2644 | + ) |
| 2645 | + |
| 2646 | + events = capture_events() |
| 2647 | + |
| 2648 | + with sentry_sdk.start_transaction(op="test", name="test"): |
| 2649 | + span = sentry_sdk.start_span(op="test_span") |
| 2650 | + binary_content = BinaryContent( |
| 2651 | + data=b"fake_image_data_12345", media_type="image/png" |
| 2652 | + ) |
| 2653 | + user_part = UserPromptPart(content=["Look at this image:", binary_content]) |
| 2654 | + mock_msg = MagicMock() |
| 2655 | + mock_msg.parts = [user_part] |
| 2656 | + mock_msg.instructions = None |
| 2657 | + |
| 2658 | + _set_input_messages(span, [mock_msg]) |
| 2659 | + span.finish() |
| 2660 | + |
| 2661 | + (event,) = events |
| 2662 | + span_data = event["spans"][0]["data"] |
| 2663 | + messages_data = _get_messages_from_span(span_data) |
| 2664 | + assert _find_binary_content(messages_data, "image", "image/png") |
| 2665 | + |
| 2666 | + |
| 2667 | +@pytest.mark.asyncio |
| 2668 | +async def test_binary_content_encoding_mixed_content(sentry_init, capture_events): |
| 2669 | + """Test that BinaryContent mixed with text content is properly handled.""" |
| 2670 | + sentry_init( |
| 2671 | + integrations=[PydanticAIIntegration()], |
| 2672 | + traces_sample_rate=1.0, |
| 2673 | + send_default_pii=True, |
| 2674 | + ) |
| 2675 | + |
| 2676 | + events = capture_events() |
| 2677 | + |
| 2678 | + with sentry_sdk.start_transaction(op="test", name="test"): |
| 2679 | + span = sentry_sdk.start_span(op="test_span") |
| 2680 | + binary_content = BinaryContent( |
| 2681 | + data=b"fake_image_bytes", media_type="image/jpeg" |
| 2682 | + ) |
| 2683 | + user_part = UserPromptPart( |
| 2684 | + content=["Here is an image:", binary_content, "What do you see?"] |
| 2685 | + ) |
| 2686 | + mock_msg = MagicMock() |
| 2687 | + mock_msg.parts = [user_part] |
| 2688 | + mock_msg.instructions = None |
| 2689 | + |
| 2690 | + _set_input_messages(span, [mock_msg]) |
| 2691 | + span.finish() |
| 2692 | + |
| 2693 | + (event,) = events |
| 2694 | + span_data = event["spans"][0]["data"] |
| 2695 | + messages_data = _get_messages_from_span(span_data) |
| 2696 | + |
| 2697 | + # Verify both text and binary content are present |
| 2698 | + found_text = any( |
| 2699 | + content_item.get("type") == "text" |
| 2700 | + for msg in messages_data |
| 2701 | + if "content" in msg |
| 2702 | + for content_item in msg["content"] |
| 2703 | + ) |
| 2704 | + assert found_text, "Text content should be found" |
| 2705 | + assert _find_binary_content(messages_data, "image", "image/jpeg") |
| 2706 | + |
| 2707 | + |
| 2708 | +@pytest.mark.asyncio |
| 2709 | +async def test_binary_content_in_agent_run(sentry_init, capture_events): |
| 2710 | + """Test that BinaryContent in actual agent run is properly captured in spans.""" |
| 2711 | + agent = Agent("test", name="test_binary_agent") |
| 2712 | + |
| 2713 | + sentry_init( |
| 2714 | + integrations=[PydanticAIIntegration()], |
| 2715 | + traces_sample_rate=1.0, |
| 2716 | + send_default_pii=True, |
| 2717 | + ) |
| 2718 | + |
| 2719 | + events = capture_events() |
| 2720 | + binary_content = BinaryContent( |
| 2721 | + data=b"fake_image_data_for_testing", media_type="image/png" |
| 2722 | + ) |
| 2723 | + await agent.run(["Analyze this image:", binary_content]) |
| 2724 | + |
| 2725 | + (transaction,) = events |
| 2726 | + chat_spans = [s for s in transaction["spans"] if s["op"] == "gen_ai.chat"] |
| 2727 | + assert len(chat_spans) >= 1 |
| 2728 | + |
| 2729 | + chat_span = chat_spans[0] |
| 2730 | + if "gen_ai.request.messages" in chat_span["data"]: |
| 2731 | + messages_str = str(chat_span["data"]["gen_ai.request.messages"]) |
| 2732 | + assert any(keyword in messages_str for keyword in ["blob", "image", "base64"]) |
0 commit comments