Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions .agents/skills/sdk-integrations/SKILL.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,7 @@ Always read:
- `py/src/braintrust/integrations/base.py`
- `py/src/braintrust/integrations/versioning.py`
- `py/src/braintrust/integrations/__init__.py`
- `py/src/braintrust/integrations/utils.py`
- `py/noxfile.py`

Read these when working on an existing integration:
Expand All@@ -43,8 +44,9 @@ Read these when relevant:
- `py/src/braintrust/auto.py` for `auto_instrument()` changes
- `py/src/braintrust/conftest.py` for VCR behavior
- `py/src/braintrust/integrations/auto_test_scripts/` for subprocess auto-instrument coverage
- `py/src/braintrust/integrations/adk/test_adk.py` and `py/src/braintrust/integrations/anthropic/test_anthropic.py` for test layout patterns
- `py/src/braintrust/integrations/adk/tracing.py` and `py/src/braintrust/integrations/google_genai/tracing.py` when handling multimodal content, binary inputs, or generated media
- `py/src/braintrust/integrations/test_utils.py` when touching shared attachment materialization or multimodal payload shaping
- `py/src/braintrust/integrations/adk/test_adk.py`, `py/src/braintrust/integrations/anthropic/test_anthropic.py`, and `py/src/braintrust/integrations/google_genai/test_google_genai.py` for attachment-focused test layout patterns
- `py/src/braintrust/integrations/adk/tracing.py`, `py/src/braintrust/integrations/anthropic/tracing.py`, and `py/src/braintrust/integrations/google_genai/tracing.py` when handling multimodal content, binary inputs, generated media, or attachment materialization behavior

Do not forget `auto.py` and `auto_test_scripts/`. Import-order and subprocess regressions often only show up there.

Expand All@@ -54,8 +56,8 @@ Start from the nearest current integration:

- ADK: direct method patching, `target_module`, `CompositeFunctionWrapperPatcher`, manual `wrap_*()` helpers, context propagation, inline data to `Attachment`
- Agno: multi-target patching, several related patchers, version-conditional fallbacks with `superseded_by`
- Anthropic: compact constructor patching and a small public surface
- Google GenAI: multimodal tracing, generated media, output-side `Attachment` handling
- Anthropic: compact constructor patching, a small public surface, and multimodal request blocks that distinguish image vs document attachment payloads
- Google GenAI: multimodal tracing, generated media, output-side `Attachment` handling, and nested attachment materialization while preserving non-attachment values

Choose the reference based on the hardest part of the task:

Expand DownExpand Up@@ -188,10 +190,16 @@ def _process_result(result: Any, start: float) -> tuple[dict[str, Any], dict[str

Treat binary payloads as attachments, not logged bytes:

- convert raw `bytes` to `braintrust.logger.Attachment`
- prefer the shared `_materialize_attachment(...)` helper in `py/src/braintrust/integrations/utils.py` over provider-local base64 or file-decoding code
- convert provider-owned raw `bytes`, base64 payloads, data URLs, file inputs, and generated media into `braintrust.logger.Attachment` objects when Braintrust should upload the content
- preserve normal remote URLs as strings
- keep useful metadata such as MIME type, size, or provider ids next to the attachment
- follow existing repo content shapes for multimodal payloads
- use the repo's existing multimodal payload shapes after materialization:
- images -> `{"image_url": {"url": attachment}}`
- non-image media/documents/files -> `{"file": {"file_data": attachment, "filename": resolved.filename}}`
- do not force non-image payloads through `image_url` shims
- if attachment materialization fails, keep the original value instead of dropping it or replacing it with `None`
- preserve non-attachment values while walking nested payloads unless you are intentionally normalizing them for readability
- keep useful metadata such as MIME type, size, safety data, filenames, or provider ids next to the attachment

## Patcher Rules

Expand DownExpand Up@@ -251,14 +259,21 @@ Cover the surfaces that changed:
- idempotence
- failure and error logging
- patcher resolution and duplicate detection when relevant
- attachment conversion for binary inputs or generated media
- attachment conversion for binary inputs or generated media, including assertions that images land under `image_url.url`, non-image payloads land under `file.file_data`, and traced payloads contain `Attachment` objects rather than raw bytes or base64 blobs
- span structure, especially `input`, `output`, `metadata`, and `metrics`

For streaming changes, verify both:

- the provider still returns the expected iterator or async iterator
- the final logged span contains the aggregated `output` and stream-specific `metrics`

Also verify, when relevant:

- the `input` contains the expected model/messages/prompt/config fields
- the `output` contains normalized provider results rather than opaque SDK instances
- the `metadata` contains finish reasons, ids, or annotations in the expected place
- binary payloads are represented as `Attachment` objects where applicable, while remote URLs and non-attachment values remain unchanged and unmaterialized file inputs are preserved rather than dropped

Keep VCR cassettes in `py/src/braintrust/integrations/<provider>/cassettes/`. Re-record only when behavior intentionally changes.

When the provider returns binary HTTP responses or generated media, sanitize cassettes as needed so fixtures do not store raw file bytes.
Expand DownExpand Up@@ -296,3 +311,4 @@ Avoid these failures:
- re-recording cassettes when behavior did not intentionally change
- adding a custom `_instrument_*` helper where `_instrument_integration()` already fits
- forgetting `target_module` for deep or optional patch targets
- forcing non-image attachments through `image_url` shims, dropping unrecognized file inputs, or re-serializing non-attachment values while materializing payloads
13 changes: 7 additions & 6 deletions py/src/braintrust/integrations/adk/test_adk.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -314,8 +314,9 @@ async def generate_content_async(self, llm_request: LlmRequest, stream: bool = F
assert len(new_message["parts"]) == 2

document_part = new_message["parts"][0]
assert "image_url" in document_part
attachment = document_part["image_url"]["url"]
assert "file" in document_part
assert document_part["file"]["filename"] == "file.pdf"
attachment = document_part["file"]["file_data"]
assert isinstance(attachment, Attachment)
assert attachment.reference["content_type"] == "application/pdf"
assert attachment.reference["filename"] == "file.pdf"
Expand All@@ -329,8 +330,8 @@ async def generate_content_async(self, llm_request: LlmRequest, stream: bool = F
llm_span = next(row for row in spans if row["span_attributes"]["type"] == "llm")
llm_contents = llm_span["input"]["contents"]
llm_document_part = llm_contents[0]["parts"][0]
assert isinstance(llm_document_part["image_url"]["url"], Attachment)
assert llm_document_part["image_url"]["url"].reference["content_type"] == "application/pdf"
assert isinstance(llm_document_part["file"]["file_data"], Attachment)
assert llm_document_part["file"]["file_data"].reference["content_type"] == "application/pdf"


@pytest.mark.vcr
Expand DownExpand Up@@ -592,7 +593,7 @@ def __init__(self, inline_data=None, text=None):
assert isinstance(attachment, Attachment), "Should be an Attachment object"
assert attachment.reference["type"] == "braintrust_attachment"
assert attachment.reference["content_type"] == "image/png"
assert attachment.reference["filename"] == "file.png"
assert attachment.reference["filename"] == "image.png"
assert "key" in attachment.reference

# Test serializing a Part with text
Expand DownExpand Up@@ -740,7 +741,7 @@ async def test_adk_binary_data_attachment_conversion(memory_logger):
assert "filename" in ref, "Attachment reference should have a filename"
assert "content_type" in ref, "Attachment reference should have a content_type"
assert ref["content_type"] == "image/png", "Content type should be image/png"
assert ref["filename"] == "file.png", "Filename should be file.png"
assert ref["filename"] == "image.png", "Filename should be image.png"

# Second part should be the text
text_part = new_message["parts"][1]
Expand Down
10 changes: 4 additions & 6 deletions py/src/braintrust/integrations/adk/tracing.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,7 +9,7 @@
from typing import Any, cast

from braintrust.bt_json import bt_safe_deep_copy
from braintrust.integrations.utils import _attachment_from_bytes, _image_url_payload
from braintrust.integrations.utils import _materialize_attachment
from braintrust.logger import start_span
from braintrust.span_types import SpanTypeAttribute

Expand DownExpand Up@@ -58,12 +58,10 @@ def _serialize_part(part: Any) -> Any:
data = inline_data.data
mime_type = inline_data.mime_type

# Convert bytes to Attachment
if isinstance(data, bytes):
attachment = _attachment_from_bytes(data, mime_type)

# Return in image_url format - SDK will replace with AttachmentReference
return _image_url_payload(attachment)
resolved_attachment = _materialize_attachment(data, mime_type=mime_type)
if resolved_attachment is not None:
return resolved_attachment.multimodal_part_payload

# Handle Part objects with file_data (file references)
if hasattr(part, "file_data") and part.file_data:
Expand Down
14 changes: 8 additions & 6 deletions py/src/braintrust/integrations/anthropic/test_anthropic.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -124,9 +124,10 @@ def test_get_input_from_kwargs_converts_multimodal_base64_blocks_to_attachments(

assert document_block["type"] == "document"
assert document_block["source"] == {"type": "base64", "media_type": "application/pdf"}
assert isinstance(document_block["image_url"]["url"], Attachment)
assert document_block["image_url"]["url"].reference["content_type"] == "application/pdf"
assert document_block["image_url"]["url"].reference["filename"] == "document.pdf"
assert document_block["file"]["filename"] == "document.pdf"
assert isinstance(document_block["file"]["file_data"], Attachment)
assert document_block["file"]["file_data"].reference["content_type"] == "application/pdf"
assert document_block["file"]["file_data"].reference["filename"] == "document.pdf"

serialized = str(processed_input)
assert PNG_BASE64 not in serialized
Expand DownExpand Up@@ -365,9 +366,10 @@ def test_anthropic_messages_create_with_document_attachment_input(memory_logger)

assert document_block["type"] == "document"
assert document_block["source"] == {"type": "base64", "media_type": "application/pdf"}
assert isinstance(document_block["image_url"]["url"], Attachment)
assert document_block["image_url"]["url"].reference["content_type"] == "application/pdf"
assert document_block["image_url"]["url"].reference["filename"] == "document.pdf"
assert document_block["file"]["filename"] == "document.pdf"
assert isinstance(document_block["file"]["file_data"], Attachment)
assert document_block["file"]["file_data"].reference["content_type"] == "application/pdf"
assert document_block["file"]["file_data"].reference["filename"] == "document.pdf"
assert PDF_BASE64 not in str(span["input"])


Expand Down
23 changes: 7 additions & 16 deletions py/src/braintrust/integrations/anthropic/tracing.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,11 +4,7 @@

from braintrust.bt_json import bt_safe_deep_copy
from braintrust.integrations.anthropic._utils import Wrapper, extract_anthropic_usage
from braintrust.integrations.utils import (
_attachment_filename_for_mime_type,
_attachment_from_base64_data,
_image_url_payload,
)
from braintrust.integrations.utils import _materialize_attachment
from braintrust.logger import log_exc_info_to_span, start_span


Expand DownExpand Up@@ -415,11 +411,6 @@ def _start_batch_results_span(args, kwargs):
return start_span(name="anthropic.messages.batches.results", type="task", metadata=metadata, input=_input)


def _attachment_filename_for_media_type(media_type: str, block_type: str) -> str:
prefix = "image" if block_type == "image" else "document"
return _attachment_filename_for_mime_type(media_type, prefix=prefix)


def _convert_base64_source_to_attachment(block_type, source):
if not isinstance(source, dict):
return None
Expand All@@ -431,10 +422,10 @@ def _convert_base64_source_to_attachment(block_type, source):
if not isinstance(media_type, str) or not isinstance(data, str):
return None

return _attachment_from_base64_data(
return _materialize_attachment(
data,
media_type,
filename=_attachment_filename_for_media_type(media_type, block_type),
mime_type=media_type,
prefix="image" if block_type == "image" else "document",
)


Expand All@@ -447,11 +438,11 @@ def _process_input_attachments(value):
source = value.get("source")

if block_type in {"image", "document"} and isinstance(source, dict):
attachment = _convert_base64_source_to_attachment(block_type, source)
if attachment is not None:
resolved_attachment = _convert_base64_source_to_attachment(block_type, source)
if resolved_attachment is not None:
processed = {k: _process_input_attachments(v) for k, v in value.items() if k != "source"}
processed["source"] = {k: _process_input_attachments(v) for k, v in source.items() if k != "data"}
processed.update(_image_url_payload(attachment))
processed.update(resolved_attachment.multimodal_part_payload)
return processed

return {k: _process_input_attachments(v) for k, v in value.items()}
Expand Down
49 changes: 46 additions & 3 deletions py/src/braintrust/integrations/google_genai/test_google_genai.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -143,10 +143,16 @@ def _assert_timing_metrics_are_valid(metrics, start=None, end=None):


def _assert_attachment_part(part, *, content_type, filename):
assert "image_url" in part
assert "url" in part["image_url"]
if content_type.startswith("image/"):
assert "image_url" in part
assert "url" in part["image_url"]
attachment = part["image_url"]["url"]
else:
assert "file" in part
assert "file_data" in part["file"]
assert part["file"]["filename"] == filename
attachment = part["file"]["file_data"]

attachment = part["image_url"]["url"]
assert isinstance(attachment, Attachment)
assert attachment.reference["type"] == "braintrust_attachment"
assert attachment.reference["content_type"] == content_type
Expand DownExpand Up@@ -1030,6 +1036,43 @@ class TestModel(BaseModel):
assert copied["context_file"] is attachment


def test_interaction_materialization_only_converts_multimodal_payloads():
"""Interaction helpers should only materialize attachments, not re-serialize values."""
from datetime import datetime
from enum import Enum

from braintrust.integrations.google_genai.tracing import _materialize_interaction_value
from pydantic import BaseModel

class Mode(Enum):
CHAT = "chat"

class InteractionPayload(BaseModel):
created_at: datetime
mode: Mode
media: dict[str, object]

created_at = datetime(2024, 1, 2, 3, 4, 5)
materialized = _materialize_interaction_value(
InteractionPayload(
created_at=created_at,
mode=Mode.CHAT,
media={
"type": "image",
"data": TINY_PNG_BASE64,
"mime_type": "image/png",
"caption": None,
},
)
)

assert materialized["created_at"] == created_at
assert materialized["mode"] is Mode.CHAT
assert materialized["media"]["caption"] is None
assert isinstance(materialized["media"]["data"], Attachment)
assert materialized["media"]["image_url"]["url"] is materialized["media"]["data"]


GROUNDING_MODEL = "gemini-2.0-flash-001"


Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
fix(integrations): unify multimodal attachment materialization by AbhiPrasad · Pull Request #247 · braintrustdata/braintrust-sdk-python · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions .agents/skills/sdk-integrations/SKILL.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,7 @@ Always read:
- `py/src/braintrust/integrations/base.py`
- `py/src/braintrust/integrations/versioning.py`
- `py/src/braintrust/integrations/__init__.py`
- `py/src/braintrust/integrations/utils.py`
- `py/noxfile.py`

Read these when working on an existing integration:
Expand All@@ -43,8 +44,9 @@ Read these when relevant:
- `py/src/braintrust/auto.py` for `auto_instrument()` changes
- `py/src/braintrust/conftest.py` for VCR behavior
- `py/src/braintrust/integrations/auto_test_scripts/` for subprocess auto-instrument coverage
- `py/src/braintrust/integrations/adk/test_adk.py` and `py/src/braintrust/integrations/anthropic/test_anthropic.py` for test layout patterns
- `py/src/braintrust/integrations/adk/tracing.py` and `py/src/braintrust/integrations/google_genai/tracing.py` when handling multimodal content, binary inputs, or generated media
- `py/src/braintrust/integrations/test_utils.py` when touching shared attachment materialization or multimodal payload shaping
- `py/src/braintrust/integrations/adk/test_adk.py`, `py/src/braintrust/integrations/anthropic/test_anthropic.py`, and `py/src/braintrust/integrations/google_genai/test_google_genai.py` for attachment-focused test layout patterns
- `py/src/braintrust/integrations/adk/tracing.py`, `py/src/braintrust/integrations/anthropic/tracing.py`, and `py/src/braintrust/integrations/google_genai/tracing.py` when handling multimodal content, binary inputs, generated media, or attachment materialization behavior

Do not forget `auto.py` and `auto_test_scripts/`. Import-order and subprocess regressions often only show up there.

Expand All@@ -54,8 +56,8 @@ Start from the nearest current integration:

- ADK: direct method patching, `target_module`, `CompositeFunctionWrapperPatcher`, manual `wrap_*()` helpers, context propagation, inline data to `Attachment`
- Agno: multi-target patching, several related patchers, version-conditional fallbacks with `superseded_by`
- Anthropic: compact constructor patching and a small public surface
- Google GenAI: multimodal tracing, generated media, output-side `Attachment` handling
- Anthropic: compact constructor patching, a small public surface, and multimodal request blocks that distinguish image vs document attachment payloads
- Google GenAI: multimodal tracing, generated media, output-side `Attachment` handling, and nested attachment materialization while preserving non-attachment values

Choose the reference based on the hardest part of the task:

Expand DownExpand Up@@ -188,10 +190,16 @@ def _process_result(result: Any, start: float) -> tuple[dict[str, Any], dict[str

Treat binary payloads as attachments, not logged bytes:

- convert raw `bytes` to `braintrust.logger.Attachment`
- prefer the shared `_materialize_attachment(...)` helper in `py/src/braintrust/integrations/utils.py` over provider-local base64 or file-decoding code
- convert provider-owned raw `bytes`, base64 payloads, data URLs, file inputs, and generated media into `braintrust.logger.Attachment` objects when Braintrust should upload the content
- preserve normal remote URLs as strings
- keep useful metadata such as MIME type, size, or provider ids next to the attachment
- follow existing repo content shapes for multimodal payloads
- use the repo's existing multimodal payload shapes after materialization:
- images -> `{"image_url": {"url": attachment}}`
- non-image media/documents/files -> `{"file": {"file_data": attachment, "filename": resolved.filename}}`
- do not force non-image payloads through `image_url` shims
- if attachment materialization fails, keep the original value instead of dropping it or replacing it with `None`
- preserve non-attachment values while walking nested payloads unless you are intentionally normalizing them for readability
- keep useful metadata such as MIME type, size, safety data, filenames, or provider ids next to the attachment

## Patcher Rules

Expand DownExpand Up@@ -251,14 +259,21 @@ Cover the surfaces that changed:
- idempotence
- failure and error logging
- patcher resolution and duplicate detection when relevant
- attachment conversion for binary inputs or generated media
- attachment conversion for binary inputs or generated media, including assertions that images land under `image_url.url`, non-image payloads land under `file.file_data`, and traced payloads contain `Attachment` objects rather than raw bytes or base64 blobs
- span structure, especially `input`, `output`, `metadata`, and `metrics`

For streaming changes, verify both:

- the provider still returns the expected iterator or async iterator
- the final logged span contains the aggregated `output` and stream-specific `metrics`

Also verify, when relevant:

- the `input` contains the expected model/messages/prompt/config fields
- the `output` contains normalized provider results rather than opaque SDK instances
- the `metadata` contains finish reasons, ids, or annotations in the expected place
- binary payloads are represented as `Attachment` objects where applicable, while remote URLs and non-attachment values remain unchanged and unmaterialized file inputs are preserved rather than dropped

Keep VCR cassettes in `py/src/braintrust/integrations/<provider>/cassettes/`. Re-record only when behavior intentionally changes.

When the provider returns binary HTTP responses or generated media, sanitize cassettes as needed so fixtures do not store raw file bytes.
Expand DownExpand Up@@ -296,3 +311,4 @@ Avoid these failures:
- re-recording cassettes when behavior did not intentionally change
- adding a custom `_instrument_*` helper where `_instrument_integration()` already fits
- forgetting `target_module` for deep or optional patch targets
- forcing non-image attachments through `image_url` shims, dropping unrecognized file inputs, or re-serializing non-attachment values while materializing payloads
13 changes: 7 additions & 6 deletions py/src/braintrust/integrations/adk/test_adk.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -314,8 +314,9 @@ async def generate_content_async(self, llm_request: LlmRequest, stream: bool = F
assert len(new_message["parts"]) == 2

document_part = new_message["parts"][0]
assert "image_url" in document_part
attachment = document_part["image_url"]["url"]
assert "file" in document_part
assert document_part["file"]["filename"] == "file.pdf"
attachment = document_part["file"]["file_data"]
assert isinstance(attachment, Attachment)
assert attachment.reference["content_type"] == "application/pdf"
assert attachment.reference["filename"] == "file.pdf"
Expand All@@ -329,8 +330,8 @@ async def generate_content_async(self, llm_request: LlmRequest, stream: bool = F
llm_span = next(row for row in spans if row["span_attributes"]["type"] == "llm")
llm_contents = llm_span["input"]["contents"]
llm_document_part = llm_contents[0]["parts"][0]
assert isinstance(llm_document_part["image_url"]["url"], Attachment)
assert llm_document_part["image_url"]["url"].reference["content_type"] == "application/pdf"
assert isinstance(llm_document_part["file"]["file_data"], Attachment)
assert llm_document_part["file"]["file_data"].reference["content_type"] == "application/pdf"


@pytest.mark.vcr
Expand DownExpand Up@@ -592,7 +593,7 @@ def __init__(self, inline_data=None, text=None):
assert isinstance(attachment, Attachment), "Should be an Attachment object"
assert attachment.reference["type"] == "braintrust_attachment"
assert attachment.reference["content_type"] == "image/png"
assert attachment.reference["filename"] == "file.png"
assert attachment.reference["filename"] == "image.png"
assert "key" in attachment.reference

# Test serializing a Part with text
Expand DownExpand Up@@ -740,7 +741,7 @@ async def test_adk_binary_data_attachment_conversion(memory_logger):
assert "filename" in ref, "Attachment reference should have a filename"
assert "content_type" in ref, "Attachment reference should have a content_type"
assert ref["content_type"] == "image/png", "Content type should be image/png"
assert ref["filename"] == "file.png", "Filename should be file.png"
assert ref["filename"] == "image.png", "Filename should be image.png"

# Second part should be the text
text_part = new_message["parts"][1]
Expand Down
10 changes: 4 additions & 6 deletions py/src/braintrust/integrations/adk/tracing.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,7 +9,7 @@
from typing import Any, cast

from braintrust.bt_json import bt_safe_deep_copy
from braintrust.integrations.utils import _attachment_from_bytes, _image_url_payload
from braintrust.integrations.utils import _materialize_attachment
from braintrust.logger import start_span
from braintrust.span_types import SpanTypeAttribute

Expand DownExpand Up@@ -58,12 +58,10 @@ def _serialize_part(part: Any) -> Any:
data = inline_data.data
mime_type = inline_data.mime_type

# Convert bytes to Attachment
if isinstance(data, bytes):
attachment = _attachment_from_bytes(data, mime_type)

# Return in image_url format - SDK will replace with AttachmentReference
return _image_url_payload(attachment)
resolved_attachment = _materialize_attachment(data, mime_type=mime_type)
if resolved_attachment is not None:
return resolved_attachment.multimodal_part_payload

# Handle Part objects with file_data (file references)
if hasattr(part, "file_data") and part.file_data:
Expand Down
14 changes: 8 additions & 6 deletions py/src/braintrust/integrations/anthropic/test_anthropic.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -124,9 +124,10 @@ def test_get_input_from_kwargs_converts_multimodal_base64_blocks_to_attachments(

assert document_block["type"] == "document"
assert document_block["source"] == {"type": "base64", "media_type": "application/pdf"}
assert isinstance(document_block["image_url"]["url"], Attachment)
assert document_block["image_url"]["url"].reference["content_type"] == "application/pdf"
assert document_block["image_url"]["url"].reference["filename"] == "document.pdf"
assert document_block["file"]["filename"] == "document.pdf"
assert isinstance(document_block["file"]["file_data"], Attachment)
assert document_block["file"]["file_data"].reference["content_type"] == "application/pdf"
assert document_block["file"]["file_data"].reference["filename"] == "document.pdf"

serialized = str(processed_input)
assert PNG_BASE64 not in serialized
Expand DownExpand Up@@ -365,9 +366,10 @@ def test_anthropic_messages_create_with_document_attachment_input(memory_logger)

assert document_block["type"] == "document"
assert document_block["source"] == {"type": "base64", "media_type": "application/pdf"}
assert isinstance(document_block["image_url"]["url"], Attachment)
assert document_block["image_url"]["url"].reference["content_type"] == "application/pdf"
assert document_block["image_url"]["url"].reference["filename"] == "document.pdf"
assert document_block["file"]["filename"] == "document.pdf"
assert isinstance(document_block["file"]["file_data"], Attachment)
assert document_block["file"]["file_data"].reference["content_type"] == "application/pdf"
assert document_block["file"]["file_data"].reference["filename"] == "document.pdf"
assert PDF_BASE64 not in str(span["input"])


Expand Down
23 changes: 7 additions & 16 deletions py/src/braintrust/integrations/anthropic/tracing.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,11 +4,7 @@

from braintrust.bt_json import bt_safe_deep_copy
from braintrust.integrations.anthropic._utils import Wrapper, extract_anthropic_usage
from braintrust.integrations.utils import (
_attachment_filename_for_mime_type,
_attachment_from_base64_data,
_image_url_payload,
)
from braintrust.integrations.utils import _materialize_attachment
from braintrust.logger import log_exc_info_to_span, start_span


Expand DownExpand Up@@ -415,11 +411,6 @@ def _start_batch_results_span(args, kwargs):
return start_span(name="anthropic.messages.batches.results", type="task", metadata=metadata, input=_input)


def _attachment_filename_for_media_type(media_type: str, block_type: str) -> str:
prefix = "image" if block_type == "image" else "document"
return _attachment_filename_for_mime_type(media_type, prefix=prefix)


def _convert_base64_source_to_attachment(block_type, source):
if not isinstance(source, dict):
return None
Expand All@@ -431,10 +422,10 @@ def _convert_base64_source_to_attachment(block_type, source):
if not isinstance(media_type, str) or not isinstance(data, str):
return None

return _attachment_from_base64_data(
return _materialize_attachment(
data,
media_type,
filename=_attachment_filename_for_media_type(media_type, block_type),
mime_type=media_type,
prefix="image" if block_type == "image" else "document",
)


Expand All@@ -447,11 +438,11 @@ def _process_input_attachments(value):
source = value.get("source")

if block_type in {"image", "document"} and isinstance(source, dict):
attachment = _convert_base64_source_to_attachment(block_type, source)
if attachment is not None:
resolved_attachment = _convert_base64_source_to_attachment(block_type, source)
if resolved_attachment is not None:
processed = {k: _process_input_attachments(v) for k, v in value.items() if k != "source"}
processed["source"] = {k: _process_input_attachments(v) for k, v in source.items() if k != "data"}
processed.update(_image_url_payload(attachment))
processed.update(resolved_attachment.multimodal_part_payload)
return processed

return {k: _process_input_attachments(v) for k, v in value.items()}
Expand Down
49 changes: 46 additions & 3 deletions py/src/braintrust/integrations/google_genai/test_google_genai.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -143,10 +143,16 @@ def _assert_timing_metrics_are_valid(metrics, start=None, end=None):


def _assert_attachment_part(part, *, content_type, filename):
assert "image_url" in part
assert "url" in part["image_url"]
if content_type.startswith("image/"):
assert "image_url" in part
assert "url" in part["image_url"]
attachment = part["image_url"]["url"]
else:
assert "file" in part
assert "file_data" in part["file"]
assert part["file"]["filename"] == filename
attachment = part["file"]["file_data"]

attachment = part["image_url"]["url"]
assert isinstance(attachment, Attachment)
assert attachment.reference["type"] == "braintrust_attachment"
assert attachment.reference["content_type"] == content_type
Expand DownExpand Up@@ -1030,6 +1036,43 @@ class TestModel(BaseModel):
assert copied["context_file"] is attachment


def test_interaction_materialization_only_converts_multimodal_payloads():
"""Interaction helpers should only materialize attachments, not re-serialize values."""
from datetime import datetime
from enum import Enum

from braintrust.integrations.google_genai.tracing import _materialize_interaction_value
from pydantic import BaseModel

class Mode(Enum):
CHAT = "chat"

class InteractionPayload(BaseModel):
created_at: datetime
mode: Mode
media: dict[str, object]

created_at = datetime(2024, 1, 2, 3, 4, 5)
materialized = _materialize_interaction_value(
InteractionPayload(
created_at=created_at,
mode=Mode.CHAT,
media={
"type": "image",
"data": TINY_PNG_BASE64,
"mime_type": "image/png",
"caption": None,
},
)
)

assert materialized["created_at"] == created_at
assert materialized["mode"] is Mode.CHAT
assert materialized["media"]["caption"] is None
assert isinstance(materialized["media"]["data"], Attachment)
assert materialized["media"]["image_url"]["url"] is materialized["media"]["data"]


GROUNDING_MODEL = "gemini-2.0-flash-001"


Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' fix(integrations): unify multimodal attachment materialization by AbhiPrasad · Pull Request #247 · braintrustdata/braintrust-sdk-python · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions .agents/skills/sdk-integrations/SKILL.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,7 @@ Always read:
- `py/src/braintrust/integrations/base.py`
- `py/src/braintrust/integrations/versioning.py`
- `py/src/braintrust/integrations/__init__.py`
- `py/src/braintrust/integrations/utils.py`
- `py/noxfile.py`

Read these when working on an existing integration:
Expand All@@ -43,8 +44,9 @@ Read these when relevant:
- `py/src/braintrust/auto.py` for `auto_instrument()` changes
- `py/src/braintrust/conftest.py` for VCR behavior
- `py/src/braintrust/integrations/auto_test_scripts/` for subprocess auto-instrument coverage
- `py/src/braintrust/integrations/adk/test_adk.py` and `py/src/braintrust/integrations/anthropic/test_anthropic.py` for test layout patterns
- `py/src/braintrust/integrations/adk/tracing.py` and `py/src/braintrust/integrations/google_genai/tracing.py` when handling multimodal content, binary inputs, or generated media
- `py/src/braintrust/integrations/test_utils.py` when touching shared attachment materialization or multimodal payload shaping
- `py/src/braintrust/integrations/adk/test_adk.py`, `py/src/braintrust/integrations/anthropic/test_anthropic.py`, and `py/src/braintrust/integrations/google_genai/test_google_genai.py` for attachment-focused test layout patterns
- `py/src/braintrust/integrations/adk/tracing.py`, `py/src/braintrust/integrations/anthropic/tracing.py`, and `py/src/braintrust/integrations/google_genai/tracing.py` when handling multimodal content, binary inputs, generated media, or attachment materialization behavior

Do not forget `auto.py` and `auto_test_scripts/`. Import-order and subprocess regressions often only show up there.

Expand All@@ -54,8 +56,8 @@ Start from the nearest current integration:

- ADK: direct method patching, `target_module`, `CompositeFunctionWrapperPatcher`, manual `wrap_*()` helpers, context propagation, inline data to `Attachment`
- Agno: multi-target patching, several related patchers, version-conditional fallbacks with `superseded_by`
- Anthropic: compact constructor patching and a small public surface
- Google GenAI: multimodal tracing, generated media, output-side `Attachment` handling
- Anthropic: compact constructor patching, a small public surface, and multimodal request blocks that distinguish image vs document attachment payloads
- Google GenAI: multimodal tracing, generated media, output-side `Attachment` handling, and nested attachment materialization while preserving non-attachment values

Choose the reference based on the hardest part of the task:

Expand DownExpand Up@@ -188,10 +190,16 @@ def _process_result(result: Any, start: float) -> tuple[dict[str, Any], dict[str

Treat binary payloads as attachments, not logged bytes:

- convert raw `bytes` to `braintrust.logger.Attachment`
- prefer the shared `_materialize_attachment(...)` helper in `py/src/braintrust/integrations/utils.py` over provider-local base64 or file-decoding code
- convert provider-owned raw `bytes`, base64 payloads, data URLs, file inputs, and generated media into `braintrust.logger.Attachment` objects when Braintrust should upload the content
- preserve normal remote URLs as strings
- keep useful metadata such as MIME type, size, or provider ids next to the attachment
- follow existing repo content shapes for multimodal payloads
- use the repo's existing multimodal payload shapes after materialization:
- images -> `{"image_url": {"url": attachment}}`
- non-image media/documents/files -> `{"file": {"file_data": attachment, "filename": resolved.filename}}`
- do not force non-image payloads through `image_url` shims
- if attachment materialization fails, keep the original value instead of dropping it or replacing it with `None`
- preserve non-attachment values while walking nested payloads unless you are intentionally normalizing them for readability
- keep useful metadata such as MIME type, size, safety data, filenames, or provider ids next to the attachment

## Patcher Rules

Expand DownExpand Up@@ -251,14 +259,21 @@ Cover the surfaces that changed:
- idempotence
- failure and error logging
- patcher resolution and duplicate detection when relevant
- attachment conversion for binary inputs or generated media
- attachment conversion for binary inputs or generated media, including assertions that images land under `image_url.url`, non-image payloads land under `file.file_data`, and traced payloads contain `Attachment` objects rather than raw bytes or base64 blobs
- span structure, especially `input`, `output`, `metadata`, and `metrics`

For streaming changes, verify both:

- the provider still returns the expected iterator or async iterator
- the final logged span contains the aggregated `output` and stream-specific `metrics`

Also verify, when relevant:

- the `input` contains the expected model/messages/prompt/config fields
- the `output` contains normalized provider results rather than opaque SDK instances
- the `metadata` contains finish reasons, ids, or annotations in the expected place
- binary payloads are represented as `Attachment` objects where applicable, while remote URLs and non-attachment values remain unchanged and unmaterialized file inputs are preserved rather than dropped

Keep VCR cassettes in `py/src/braintrust/integrations/<provider>/cassettes/`. Re-record only when behavior intentionally changes.

When the provider returns binary HTTP responses or generated media, sanitize cassettes as needed so fixtures do not store raw file bytes.
Expand DownExpand Up@@ -296,3 +311,4 @@ Avoid these failures:
- re-recording cassettes when behavior did not intentionally change
- adding a custom `_instrument_*` helper where `_instrument_integration()` already fits
- forgetting `target_module` for deep or optional patch targets
- forcing non-image attachments through `image_url` shims, dropping unrecognized file inputs, or re-serializing non-attachment values while materializing payloads
13 changes: 7 additions & 6 deletions py/src/braintrust/integrations/adk/test_adk.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -314,8 +314,9 @@ async def generate_content_async(self, llm_request: LlmRequest, stream: bool = F
assert len(new_message["parts"]) == 2

document_part = new_message["parts"][0]
assert "image_url" in document_part
attachment = document_part["image_url"]["url"]
assert "file" in document_part
assert document_part["file"]["filename"] == "file.pdf"
attachment = document_part["file"]["file_data"]
assert isinstance(attachment, Attachment)
assert attachment.reference["content_type"] == "application/pdf"
assert attachment.reference["filename"] == "file.pdf"
Expand All@@ -329,8 +330,8 @@ async def generate_content_async(self, llm_request: LlmRequest, stream: bool = F
llm_span = next(row for row in spans if row["span_attributes"]["type"] == "llm")
llm_contents = llm_span["input"]["contents"]
llm_document_part = llm_contents[0]["parts"][0]
assert isinstance(llm_document_part["image_url"]["url"], Attachment)
assert llm_document_part["image_url"]["url"].reference["content_type"] == "application/pdf"
assert isinstance(llm_document_part["file"]["file_data"], Attachment)
assert llm_document_part["file"]["file_data"].reference["content_type"] == "application/pdf"


@pytest.mark.vcr
Expand DownExpand Up@@ -592,7 +593,7 @@ def __init__(self, inline_data=None, text=None):
assert isinstance(attachment, Attachment), "Should be an Attachment object"
assert attachment.reference["type"] == "braintrust_attachment"
assert attachment.reference["content_type"] == "image/png"
assert attachment.reference["filename"] == "file.png"
assert attachment.reference["filename"] == "image.png"
assert "key" in attachment.reference

# Test serializing a Part with text
Expand DownExpand Up@@ -740,7 +741,7 @@ async def test_adk_binary_data_attachment_conversion(memory_logger):
assert "filename" in ref, "Attachment reference should have a filename"
assert "content_type" in ref, "Attachment reference should have a content_type"
assert ref["content_type"] == "image/png", "Content type should be image/png"
assert ref["filename"] == "file.png", "Filename should be file.png"
assert ref["filename"] == "image.png", "Filename should be image.png"

# Second part should be the text
text_part = new_message["parts"][1]
Expand Down
10 changes: 4 additions & 6 deletions py/src/braintrust/integrations/adk/tracing.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,7 +9,7 @@
from typing import Any, cast

from braintrust.bt_json import bt_safe_deep_copy
from braintrust.integrations.utils import _attachment_from_bytes, _image_url_payload
from braintrust.integrations.utils import _materialize_attachment
from braintrust.logger import start_span
from braintrust.span_types import SpanTypeAttribute

Expand DownExpand Up@@ -58,12 +58,10 @@ def _serialize_part(part: Any) -> Any:
data = inline_data.data
mime_type = inline_data.mime_type

# Convert bytes to Attachment
if isinstance(data, bytes):
attachment = _attachment_from_bytes(data, mime_type)

# Return in image_url format - SDK will replace with AttachmentReference
return _image_url_payload(attachment)
resolved_attachment = _materialize_attachment(data, mime_type=mime_type)
if resolved_attachment is not None:
return resolved_attachment.multimodal_part_payload

# Handle Part objects with file_data (file references)
if hasattr(part, "file_data") and part.file_data:
Expand Down
14 changes: 8 additions & 6 deletions py/src/braintrust/integrations/anthropic/test_anthropic.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -124,9 +124,10 @@ def test_get_input_from_kwargs_converts_multimodal_base64_blocks_to_attachments(

assert document_block["type"] == "document"
assert document_block["source"] == {"type": "base64", "media_type": "application/pdf"}
assert isinstance(document_block["image_url"]["url"], Attachment)
assert document_block["image_url"]["url"].reference["content_type"] == "application/pdf"
assert document_block["image_url"]["url"].reference["filename"] == "document.pdf"
assert document_block["file"]["filename"] == "document.pdf"
assert isinstance(document_block["file"]["file_data"], Attachment)
assert document_block["file"]["file_data"].reference["content_type"] == "application/pdf"
assert document_block["file"]["file_data"].reference["filename"] == "document.pdf"

serialized = str(processed_input)
assert PNG_BASE64 not in serialized
Expand DownExpand Up@@ -365,9 +366,10 @@ def test_anthropic_messages_create_with_document_attachment_input(memory_logger)

assert document_block["type"] == "document"
assert document_block["source"] == {"type": "base64", "media_type": "application/pdf"}
assert isinstance(document_block["image_url"]["url"], Attachment)
assert document_block["image_url"]["url"].reference["content_type"] == "application/pdf"
assert document_block["image_url"]["url"].reference["filename"] == "document.pdf"
assert document_block["file"]["filename"] == "document.pdf"
assert isinstance(document_block["file"]["file_data"], Attachment)
assert document_block["file"]["file_data"].reference["content_type"] == "application/pdf"
assert document_block["file"]["file_data"].reference["filename"] == "document.pdf"
assert PDF_BASE64 not in str(span["input"])


Expand Down
23 changes: 7 additions & 16 deletions py/src/braintrust/integrations/anthropic/tracing.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,11 +4,7 @@

from braintrust.bt_json import bt_safe_deep_copy
from braintrust.integrations.anthropic._utils import Wrapper, extract_anthropic_usage
from braintrust.integrations.utils import (
_attachment_filename_for_mime_type,
_attachment_from_base64_data,
_image_url_payload,
)
from braintrust.integrations.utils import _materialize_attachment
from braintrust.logger import log_exc_info_to_span, start_span


Expand DownExpand Up@@ -415,11 +411,6 @@ def _start_batch_results_span(args, kwargs):
return start_span(name="anthropic.messages.batches.results", type="task", metadata=metadata, input=_input)


def _attachment_filename_for_media_type(media_type: str, block_type: str) -> str:
prefix = "image" if block_type == "image" else "document"
return _attachment_filename_for_mime_type(media_type, prefix=prefix)


def _convert_base64_source_to_attachment(block_type, source):
if not isinstance(source, dict):
return None
Expand All@@ -431,10 +422,10 @@ def _convert_base64_source_to_attachment(block_type, source):
if not isinstance(media_type, str) or not isinstance(data, str):
return None

return _attachment_from_base64_data(
return _materialize_attachment(
data,
media_type,
filename=_attachment_filename_for_media_type(media_type, block_type),
mime_type=media_type,
prefix="image" if block_type == "image" else "document",
)


Expand All@@ -447,11 +438,11 @@ def _process_input_attachments(value):
source = value.get("source")

if block_type in {"image", "document"} and isinstance(source, dict):
attachment = _convert_base64_source_to_attachment(block_type, source)
if attachment is not None:
resolved_attachment = _convert_base64_source_to_attachment(block_type, source)
if resolved_attachment is not None:
processed = {k: _process_input_attachments(v) for k, v in value.items() if k != "source"}
processed["source"] = {k: _process_input_attachments(v) for k, v in source.items() if k != "data"}
processed.update(_image_url_payload(attachment))
processed.update(resolved_attachment.multimodal_part_payload)
return processed

return {k: _process_input_attachments(v) for k, v in value.items()}
Expand Down
49 changes: 46 additions & 3 deletions py/src/braintrust/integrations/google_genai/test_google_genai.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -143,10 +143,16 @@ def _assert_timing_metrics_are_valid(metrics, start=None, end=None):


def _assert_attachment_part(part, *, content_type, filename):
assert "image_url" in part
assert "url" in part["image_url"]
if content_type.startswith("image/"):
assert "image_url" in part
assert "url" in part["image_url"]
attachment = part["image_url"]["url"]
else:
assert "file" in part
assert "file_data" in part["file"]
assert part["file"]["filename"] == filename
attachment = part["file"]["file_data"]

attachment = part["image_url"]["url"]
assert isinstance(attachment, Attachment)
assert attachment.reference["type"] == "braintrust_attachment"
assert attachment.reference["content_type"] == content_type
Expand DownExpand Up@@ -1030,6 +1036,43 @@ class TestModel(BaseModel):
assert copied["context_file"] is attachment


def test_interaction_materialization_only_converts_multimodal_payloads():
"""Interaction helpers should only materialize attachments, not re-serialize values."""
from datetime import datetime
from enum import Enum

from braintrust.integrations.google_genai.tracing import _materialize_interaction_value
from pydantic import BaseModel

class Mode(Enum):
CHAT = "chat"

class InteractionPayload(BaseModel):
created_at: datetime
mode: Mode
media: dict[str, object]

created_at = datetime(2024, 1, 2, 3, 4, 5)
materialized = _materialize_interaction_value(
InteractionPayload(
created_at=created_at,
mode=Mode.CHAT,
media={
"type": "image",
"data": TINY_PNG_BASE64,
"mime_type": "image/png",
"caption": None,
},
)
)

assert materialized["created_at"] == created_at
assert materialized["mode"] is Mode.CHAT
assert materialized["media"]["caption"] is None
assert isinstance(materialized["media"]["data"], Attachment)
assert materialized["media"]["image_url"]["url"] is materialized["media"]["data"]


GROUNDING_MODEL = "gemini-2.0-flash-001"


Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' fix(integrations): unify multimodal attachment materialization by AbhiPrasad · Pull Request #247 · braintrustdata/braintrust-sdk-python · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions .agents/skills/sdk-integrations/SKILL.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,7 @@ Always read:
- `py/src/braintrust/integrations/base.py`
- `py/src/braintrust/integrations/versioning.py`
- `py/src/braintrust/integrations/__init__.py`
- `py/src/braintrust/integrations/utils.py`
- `py/noxfile.py`

Read these when working on an existing integration:
Expand All@@ -43,8 +44,9 @@ Read these when relevant:
- `py/src/braintrust/auto.py` for `auto_instrument()` changes
- `py/src/braintrust/conftest.py` for VCR behavior
- `py/src/braintrust/integrations/auto_test_scripts/` for subprocess auto-instrument coverage
- `py/src/braintrust/integrations/adk/test_adk.py` and `py/src/braintrust/integrations/anthropic/test_anthropic.py` for test layout patterns
- `py/src/braintrust/integrations/adk/tracing.py` and `py/src/braintrust/integrations/google_genai/tracing.py` when handling multimodal content, binary inputs, or generated media
- `py/src/braintrust/integrations/test_utils.py` when touching shared attachment materialization or multimodal payload shaping
- `py/src/braintrust/integrations/adk/test_adk.py`, `py/src/braintrust/integrations/anthropic/test_anthropic.py`, and `py/src/braintrust/integrations/google_genai/test_google_genai.py` for attachment-focused test layout patterns
- `py/src/braintrust/integrations/adk/tracing.py`, `py/src/braintrust/integrations/anthropic/tracing.py`, and `py/src/braintrust/integrations/google_genai/tracing.py` when handling multimodal content, binary inputs, generated media, or attachment materialization behavior

Do not forget `auto.py` and `auto_test_scripts/`. Import-order and subprocess regressions often only show up there.

Expand All@@ -54,8 +56,8 @@ Start from the nearest current integration:

- ADK: direct method patching, `target_module`, `CompositeFunctionWrapperPatcher`, manual `wrap_*()` helpers, context propagation, inline data to `Attachment`
- Agno: multi-target patching, several related patchers, version-conditional fallbacks with `superseded_by`
- Anthropic: compact constructor patching and a small public surface
- Google GenAI: multimodal tracing, generated media, output-side `Attachment` handling
- Anthropic: compact constructor patching, a small public surface, and multimodal request blocks that distinguish image vs document attachment payloads
- Google GenAI: multimodal tracing, generated media, output-side `Attachment` handling, and nested attachment materialization while preserving non-attachment values

Choose the reference based on the hardest part of the task:

Expand DownExpand Up@@ -188,10 +190,16 @@ def _process_result(result: Any, start: float) -> tuple[dict[str, Any], dict[str

Treat binary payloads as attachments, not logged bytes:

- convert raw `bytes` to `braintrust.logger.Attachment`
- prefer the shared `_materialize_attachment(...)` helper in `py/src/braintrust/integrations/utils.py` over provider-local base64 or file-decoding code
- convert provider-owned raw `bytes`, base64 payloads, data URLs, file inputs, and generated media into `braintrust.logger.Attachment` objects when Braintrust should upload the content
- preserve normal remote URLs as strings
- keep useful metadata such as MIME type, size, or provider ids next to the attachment
- follow existing repo content shapes for multimodal payloads
- use the repo's existing multimodal payload shapes after materialization:
- images -> `{"image_url": {"url": attachment}}`
- non-image media/documents/files -> `{"file": {"file_data": attachment, "filename": resolved.filename}}`
- do not force non-image payloads through `image_url` shims
- if attachment materialization fails, keep the original value instead of dropping it or replacing it with `None`
- preserve non-attachment values while walking nested payloads unless you are intentionally normalizing them for readability
- keep useful metadata such as MIME type, size, safety data, filenames, or provider ids next to the attachment

## Patcher Rules

Expand DownExpand Up@@ -251,14 +259,21 @@ Cover the surfaces that changed:
- idempotence
- failure and error logging
- patcher resolution and duplicate detection when relevant
- attachment conversion for binary inputs or generated media
- attachment conversion for binary inputs or generated media, including assertions that images land under `image_url.url`, non-image payloads land under `file.file_data`, and traced payloads contain `Attachment` objects rather than raw bytes or base64 blobs
- span structure, especially `input`, `output`, `metadata`, and `metrics`

For streaming changes, verify both:

- the provider still returns the expected iterator or async iterator
- the final logged span contains the aggregated `output` and stream-specific `metrics`

Also verify, when relevant:

- the `input` contains the expected model/messages/prompt/config fields
- the `output` contains normalized provider results rather than opaque SDK instances
- the `metadata` contains finish reasons, ids, or annotations in the expected place
- binary payloads are represented as `Attachment` objects where applicable, while remote URLs and non-attachment values remain unchanged and unmaterialized file inputs are preserved rather than dropped

Keep VCR cassettes in `py/src/braintrust/integrations/<provider>/cassettes/`. Re-record only when behavior intentionally changes.

When the provider returns binary HTTP responses or generated media, sanitize cassettes as needed so fixtures do not store raw file bytes.
Expand DownExpand Up@@ -296,3 +311,4 @@ Avoid these failures:
- re-recording cassettes when behavior did not intentionally change
- adding a custom `_instrument_*` helper where `_instrument_integration()` already fits
- forgetting `target_module` for deep or optional patch targets
- forcing non-image attachments through `image_url` shims, dropping unrecognized file inputs, or re-serializing non-attachment values while materializing payloads
13 changes: 7 additions & 6 deletions py/src/braintrust/integrations/adk/test_adk.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -314,8 +314,9 @@ async def generate_content_async(self, llm_request: LlmRequest, stream: bool = F
assert len(new_message["parts"]) == 2

document_part = new_message["parts"][0]
assert "image_url" in document_part
attachment = document_part["image_url"]["url"]
assert "file" in document_part
assert document_part["file"]["filename"] == "file.pdf"
attachment = document_part["file"]["file_data"]
assert isinstance(attachment, Attachment)
assert attachment.reference["content_type"] == "application/pdf"
assert attachment.reference["filename"] == "file.pdf"
Expand All@@ -329,8 +330,8 @@ async def generate_content_async(self, llm_request: LlmRequest, stream: bool = F
llm_span = next(row for row in spans if row["span_attributes"]["type"] == "llm")
llm_contents = llm_span["input"]["contents"]
llm_document_part = llm_contents[0]["parts"][0]
assert isinstance(llm_document_part["image_url"]["url"], Attachment)
assert llm_document_part["image_url"]["url"].reference["content_type"] == "application/pdf"
assert isinstance(llm_document_part["file"]["file_data"], Attachment)
assert llm_document_part["file"]["file_data"].reference["content_type"] == "application/pdf"


@pytest.mark.vcr
Expand DownExpand Up@@ -592,7 +593,7 @@ def __init__(self, inline_data=None, text=None):
assert isinstance(attachment, Attachment), "Should be an Attachment object"
assert attachment.reference["type"] == "braintrust_attachment"
assert attachment.reference["content_type"] == "image/png"
assert attachment.reference["filename"] == "file.png"
assert attachment.reference["filename"] == "image.png"
assert "key" in attachment.reference

# Test serializing a Part with text
Expand DownExpand Up@@ -740,7 +741,7 @@ async def test_adk_binary_data_attachment_conversion(memory_logger):
assert "filename" in ref, "Attachment reference should have a filename"
assert "content_type" in ref, "Attachment reference should have a content_type"
assert ref["content_type"] == "image/png", "Content type should be image/png"
assert ref["filename"] == "file.png", "Filename should be file.png"
assert ref["filename"] == "image.png", "Filename should be image.png"

# Second part should be the text
text_part = new_message["parts"][1]
Expand Down
10 changes: 4 additions & 6 deletions py/src/braintrust/integrations/adk/tracing.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,7 +9,7 @@
from typing import Any, cast

from braintrust.bt_json import bt_safe_deep_copy
from braintrust.integrations.utils import _attachment_from_bytes, _image_url_payload
from braintrust.integrations.utils import _materialize_attachment
from braintrust.logger import start_span
from braintrust.span_types import SpanTypeAttribute

Expand DownExpand Up@@ -58,12 +58,10 @@ def _serialize_part(part: Any) -> Any:
data = inline_data.data
mime_type = inline_data.mime_type

# Convert bytes to Attachment
if isinstance(data, bytes):
attachment = _attachment_from_bytes(data, mime_type)

# Return in image_url format - SDK will replace with AttachmentReference
return _image_url_payload(attachment)
resolved_attachment = _materialize_attachment(data, mime_type=mime_type)
if resolved_attachment is not None:
return resolved_attachment.multimodal_part_payload

# Handle Part objects with file_data (file references)
if hasattr(part, "file_data") and part.file_data:
Expand Down
14 changes: 8 additions & 6 deletions py/src/braintrust/integrations/anthropic/test_anthropic.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -124,9 +124,10 @@ def test_get_input_from_kwargs_converts_multimodal_base64_blocks_to_attachments(

assert document_block["type"] == "document"
assert document_block["source"] == {"type": "base64", "media_type": "application/pdf"}
assert isinstance(document_block["image_url"]["url"], Attachment)
assert document_block["image_url"]["url"].reference["content_type"] == "application/pdf"
assert document_block["image_url"]["url"].reference["filename"] == "document.pdf"
assert document_block["file"]["filename"] == "document.pdf"
assert isinstance(document_block["file"]["file_data"], Attachment)
assert document_block["file"]["file_data"].reference["content_type"] == "application/pdf"
assert document_block["file"]["file_data"].reference["filename"] == "document.pdf"

serialized = str(processed_input)
assert PNG_BASE64 not in serialized
Expand DownExpand Up@@ -365,9 +366,10 @@ def test_anthropic_messages_create_with_document_attachment_input(memory_logger)

assert document_block["type"] == "document"
assert document_block["source"] == {"type": "base64", "media_type": "application/pdf"}
assert isinstance(document_block["image_url"]["url"], Attachment)
assert document_block["image_url"]["url"].reference["content_type"] == "application/pdf"
assert document_block["image_url"]["url"].reference["filename"] == "document.pdf"
assert document_block["file"]["filename"] == "document.pdf"
assert isinstance(document_block["file"]["file_data"], Attachment)
assert document_block["file"]["file_data"].reference["content_type"] == "application/pdf"
assert document_block["file"]["file_data"].reference["filename"] == "document.pdf"
assert PDF_BASE64 not in str(span["input"])


Expand Down
23 changes: 7 additions & 16 deletions py/src/braintrust/integrations/anthropic/tracing.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,11 +4,7 @@

from braintrust.bt_json import bt_safe_deep_copy
from braintrust.integrations.anthropic._utils import Wrapper, extract_anthropic_usage
from braintrust.integrations.utils import (
_attachment_filename_for_mime_type,
_attachment_from_base64_data,
_image_url_payload,
)
from braintrust.integrations.utils import _materialize_attachment
from braintrust.logger import log_exc_info_to_span, start_span


Expand DownExpand Up@@ -415,11 +411,6 @@ def _start_batch_results_span(args, kwargs):
return start_span(name="anthropic.messages.batches.results", type="task", metadata=metadata, input=_input)


def _attachment_filename_for_media_type(media_type: str, block_type: str) -> str:
prefix = "image" if block_type == "image" else "document"
return _attachment_filename_for_mime_type(media_type, prefix=prefix)


def _convert_base64_source_to_attachment(block_type, source):
if not isinstance(source, dict):
return None
Expand All@@ -431,10 +422,10 @@ def _convert_base64_source_to_attachment(block_type, source):
if not isinstance(media_type, str) or not isinstance(data, str):
return None

return _attachment_from_base64_data(
return _materialize_attachment(
data,
media_type,
filename=_attachment_filename_for_media_type(media_type, block_type),
mime_type=media_type,
prefix="image" if block_type == "image" else "document",
)


Expand All@@ -447,11 +438,11 @@ def _process_input_attachments(value):
source = value.get("source")

if block_type in {"image", "document"} and isinstance(source, dict):
attachment = _convert_base64_source_to_attachment(block_type, source)
if attachment is not None:
resolved_attachment = _convert_base64_source_to_attachment(block_type, source)
if resolved_attachment is not None:
processed = {k: _process_input_attachments(v) for k, v in value.items() if k != "source"}
processed["source"] = {k: _process_input_attachments(v) for k, v in source.items() if k != "data"}
processed.update(_image_url_payload(attachment))
processed.update(resolved_attachment.multimodal_part_payload)
return processed

return {k: _process_input_attachments(v) for k, v in value.items()}
Expand Down
49 changes: 46 additions & 3 deletions py/src/braintrust/integrations/google_genai/test_google_genai.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -143,10 +143,16 @@ def _assert_timing_metrics_are_valid(metrics, start=None, end=None):


def _assert_attachment_part(part, *, content_type, filename):
assert "image_url" in part
assert "url" in part["image_url"]
if content_type.startswith("image/"):
assert "image_url" in part
assert "url" in part["image_url"]
attachment = part["image_url"]["url"]
else:
assert "file" in part
assert "file_data" in part["file"]
assert part["file"]["filename"] == filename
attachment = part["file"]["file_data"]

attachment = part["image_url"]["url"]
assert isinstance(attachment, Attachment)
assert attachment.reference["type"] == "braintrust_attachment"
assert attachment.reference["content_type"] == content_type
Expand DownExpand Up@@ -1030,6 +1036,43 @@ class TestModel(BaseModel):
assert copied["context_file"] is attachment


def test_interaction_materialization_only_converts_multimodal_payloads():
"""Interaction helpers should only materialize attachments, not re-serialize values."""
from datetime import datetime
from enum import Enum

from braintrust.integrations.google_genai.tracing import _materialize_interaction_value
from pydantic import BaseModel

class Mode(Enum):
CHAT = "chat"

class InteractionPayload(BaseModel):
created_at: datetime
mode: Mode
media: dict[str, object]

created_at = datetime(2024, 1, 2, 3, 4, 5)
materialized = _materialize_interaction_value(
InteractionPayload(
created_at=created_at,
mode=Mode.CHAT,
media={
"type": "image",
"data": TINY_PNG_BASE64,
"mime_type": "image/png",
"caption": None,
},
)
)

assert materialized["created_at"] == created_at
assert materialized["mode"] is Mode.CHAT
assert materialized["media"]["caption"] is None
assert isinstance(materialized["media"]["data"], Attachment)
assert materialized["media"]["image_url"]["url"] is materialized["media"]["data"]


GROUNDING_MODEL = "gemini-2.0-flash-001"


Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' fix(integrations): unify multimodal attachment materialization by AbhiPrasad · Pull Request #247 · braintrustdata/braintrust-sdk-python · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions .agents/skills/sdk-integrations/SKILL.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,7 @@ Always read:
- `py/src/braintrust/integrations/base.py`
- `py/src/braintrust/integrations/versioning.py`
- `py/src/braintrust/integrations/__init__.py`
- `py/src/braintrust/integrations/utils.py`
- `py/noxfile.py`

Read these when working on an existing integration:
Expand All@@ -43,8 +44,9 @@ Read these when relevant:
- `py/src/braintrust/auto.py` for `auto_instrument()` changes
- `py/src/braintrust/conftest.py` for VCR behavior
- `py/src/braintrust/integrations/auto_test_scripts/` for subprocess auto-instrument coverage
- `py/src/braintrust/integrations/adk/test_adk.py` and `py/src/braintrust/integrations/anthropic/test_anthropic.py` for test layout patterns
- `py/src/braintrust/integrations/adk/tracing.py` and `py/src/braintrust/integrations/google_genai/tracing.py` when handling multimodal content, binary inputs, or generated media
- `py/src/braintrust/integrations/test_utils.py` when touching shared attachment materialization or multimodal payload shaping
- `py/src/braintrust/integrations/adk/test_adk.py`, `py/src/braintrust/integrations/anthropic/test_anthropic.py`, and `py/src/braintrust/integrations/google_genai/test_google_genai.py` for attachment-focused test layout patterns
- `py/src/braintrust/integrations/adk/tracing.py`, `py/src/braintrust/integrations/anthropic/tracing.py`, and `py/src/braintrust/integrations/google_genai/tracing.py` when handling multimodal content, binary inputs, generated media, or attachment materialization behavior

Do not forget `auto.py` and `auto_test_scripts/`. Import-order and subprocess regressions often only show up there.

Expand All@@ -54,8 +56,8 @@ Start from the nearest current integration:

- ADK: direct method patching, `target_module`, `CompositeFunctionWrapperPatcher`, manual `wrap_*()` helpers, context propagation, inline data to `Attachment`
- Agno: multi-target patching, several related patchers, version-conditional fallbacks with `superseded_by`
- Anthropic: compact constructor patching and a small public surface
- Google GenAI: multimodal tracing, generated media, output-side `Attachment` handling
- Anthropic: compact constructor patching, a small public surface, and multimodal request blocks that distinguish image vs document attachment payloads
- Google GenAI: multimodal tracing, generated media, output-side `Attachment` handling, and nested attachment materialization while preserving non-attachment values

Choose the reference based on the hardest part of the task:

Expand DownExpand Up@@ -188,10 +190,16 @@ def _process_result(result: Any, start: float) -> tuple[dict[str, Any], dict[str

Treat binary payloads as attachments, not logged bytes:

- convert raw `bytes` to `braintrust.logger.Attachment`
- prefer the shared `_materialize_attachment(...)` helper in `py/src/braintrust/integrations/utils.py` over provider-local base64 or file-decoding code
- convert provider-owned raw `bytes`, base64 payloads, data URLs, file inputs, and generated media into `braintrust.logger.Attachment` objects when Braintrust should upload the content
- preserve normal remote URLs as strings
- keep useful metadata such as MIME type, size, or provider ids next to the attachment
- follow existing repo content shapes for multimodal payloads
- use the repo's existing multimodal payload shapes after materialization:
- images -> `{"image_url": {"url": attachment}}`
- non-image media/documents/files -> `{"file": {"file_data": attachment, "filename": resolved.filename}}`
- do not force non-image payloads through `image_url` shims
- if attachment materialization fails, keep the original value instead of dropping it or replacing it with `None`
- preserve non-attachment values while walking nested payloads unless you are intentionally normalizing them for readability
- keep useful metadata such as MIME type, size, safety data, filenames, or provider ids next to the attachment

## Patcher Rules

Expand DownExpand Up@@ -251,14 +259,21 @@ Cover the surfaces that changed:
- idempotence
- failure and error logging
- patcher resolution and duplicate detection when relevant
- attachment conversion for binary inputs or generated media
- attachment conversion for binary inputs or generated media, including assertions that images land under `image_url.url`, non-image payloads land under `file.file_data`, and traced payloads contain `Attachment` objects rather than raw bytes or base64 blobs
- span structure, especially `input`, `output`, `metadata`, and `metrics`

For streaming changes, verify both:

- the provider still returns the expected iterator or async iterator
- the final logged span contains the aggregated `output` and stream-specific `metrics`

Also verify, when relevant:

- the `input` contains the expected model/messages/prompt/config fields
- the `output` contains normalized provider results rather than opaque SDK instances
- the `metadata` contains finish reasons, ids, or annotations in the expected place
- binary payloads are represented as `Attachment` objects where applicable, while remote URLs and non-attachment values remain unchanged and unmaterialized file inputs are preserved rather than dropped

Keep VCR cassettes in `py/src/braintrust/integrations/<provider>/cassettes/`. Re-record only when behavior intentionally changes.

When the provider returns binary HTTP responses or generated media, sanitize cassettes as needed so fixtures do not store raw file bytes.
Expand DownExpand Up@@ -296,3 +311,4 @@ Avoid these failures:
- re-recording cassettes when behavior did not intentionally change
- adding a custom `_instrument_*` helper where `_instrument_integration()` already fits
- forgetting `target_module` for deep or optional patch targets
- forcing non-image attachments through `image_url` shims, dropping unrecognized file inputs, or re-serializing non-attachment values while materializing payloads
13 changes: 7 additions & 6 deletions py/src/braintrust/integrations/adk/test_adk.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -314,8 +314,9 @@ async def generate_content_async(self, llm_request: LlmRequest, stream: bool = F
assert len(new_message["parts"]) == 2

document_part = new_message["parts"][0]
assert "image_url" in document_part
attachment = document_part["image_url"]["url"]
assert "file" in document_part
assert document_part["file"]["filename"] == "file.pdf"
attachment = document_part["file"]["file_data"]
assert isinstance(attachment, Attachment)
assert attachment.reference["content_type"] == "application/pdf"
assert attachment.reference["filename"] == "file.pdf"
Expand All@@ -329,8 +330,8 @@ async def generate_content_async(self, llm_request: LlmRequest, stream: bool = F
llm_span = next(row for row in spans if row["span_attributes"]["type"] == "llm")
llm_contents = llm_span["input"]["contents"]
llm_document_part = llm_contents[0]["parts"][0]
assert isinstance(llm_document_part["image_url"]["url"], Attachment)
assert llm_document_part["image_url"]["url"].reference["content_type"] == "application/pdf"
assert isinstance(llm_document_part["file"]["file_data"], Attachment)
assert llm_document_part["file"]["file_data"].reference["content_type"] == "application/pdf"


@pytest.mark.vcr
Expand DownExpand Up@@ -592,7 +593,7 @@ def __init__(self, inline_data=None, text=None):
assert isinstance(attachment, Attachment), "Should be an Attachment object"
assert attachment.reference["type"] == "braintrust_attachment"
assert attachment.reference["content_type"] == "image/png"
assert attachment.reference["filename"] == "file.png"
assert attachment.reference["filename"] == "image.png"
assert "key" in attachment.reference

# Test serializing a Part with text
Expand DownExpand Up@@ -740,7 +741,7 @@ async def test_adk_binary_data_attachment_conversion(memory_logger):
assert "filename" in ref, "Attachment reference should have a filename"
assert "content_type" in ref, "Attachment reference should have a content_type"
assert ref["content_type"] == "image/png", "Content type should be image/png"
assert ref["filename"] == "file.png", "Filename should be file.png"
assert ref["filename"] == "image.png", "Filename should be image.png"

# Second part should be the text
text_part = new_message["parts"][1]
Expand Down
10 changes: 4 additions & 6 deletions py/src/braintrust/integrations/adk/tracing.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,7 +9,7 @@
from typing import Any, cast

from braintrust.bt_json import bt_safe_deep_copy
from braintrust.integrations.utils import _attachment_from_bytes, _image_url_payload
from braintrust.integrations.utils import _materialize_attachment
from braintrust.logger import start_span
from braintrust.span_types import SpanTypeAttribute

Expand DownExpand Up@@ -58,12 +58,10 @@ def _serialize_part(part: Any) -> Any:
data = inline_data.data
mime_type = inline_data.mime_type

# Convert bytes to Attachment
if isinstance(data, bytes):
attachment = _attachment_from_bytes(data, mime_type)

# Return in image_url format - SDK will replace with AttachmentReference
return _image_url_payload(attachment)
resolved_attachment = _materialize_attachment(data, mime_type=mime_type)
if resolved_attachment is not None:
return resolved_attachment.multimodal_part_payload

# Handle Part objects with file_data (file references)
if hasattr(part, "file_data") and part.file_data:
Expand Down
14 changes: 8 additions & 6 deletions py/src/braintrust/integrations/anthropic/test_anthropic.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -124,9 +124,10 @@ def test_get_input_from_kwargs_converts_multimodal_base64_blocks_to_attachments(

assert document_block["type"] == "document"
assert document_block["source"] == {"type": "base64", "media_type": "application/pdf"}
assert isinstance(document_block["image_url"]["url"], Attachment)
assert document_block["image_url"]["url"].reference["content_type"] == "application/pdf"
assert document_block["image_url"]["url"].reference["filename"] == "document.pdf"
assert document_block["file"]["filename"] == "document.pdf"
assert isinstance(document_block["file"]["file_data"], Attachment)
assert document_block["file"]["file_data"].reference["content_type"] == "application/pdf"
assert document_block["file"]["file_data"].reference["filename"] == "document.pdf"

serialized = str(processed_input)
assert PNG_BASE64 not in serialized
Expand DownExpand Up@@ -365,9 +366,10 @@ def test_anthropic_messages_create_with_document_attachment_input(memory_logger)

assert document_block["type"] == "document"
assert document_block["source"] == {"type": "base64", "media_type": "application/pdf"}
assert isinstance(document_block["image_url"]["url"], Attachment)
assert document_block["image_url"]["url"].reference["content_type"] == "application/pdf"
assert document_block["image_url"]["url"].reference["filename"] == "document.pdf"
assert document_block["file"]["filename"] == "document.pdf"
assert isinstance(document_block["file"]["file_data"], Attachment)
assert document_block["file"]["file_data"].reference["content_type"] == "application/pdf"
assert document_block["file"]["file_data"].reference["filename"] == "document.pdf"
assert PDF_BASE64 not in str(span["input"])


Expand Down
23 changes: 7 additions & 16 deletions py/src/braintrust/integrations/anthropic/tracing.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,11 +4,7 @@

from braintrust.bt_json import bt_safe_deep_copy
from braintrust.integrations.anthropic._utils import Wrapper, extract_anthropic_usage
from braintrust.integrations.utils import (
_attachment_filename_for_mime_type,
_attachment_from_base64_data,
_image_url_payload,
)
from braintrust.integrations.utils import _materialize_attachment
from braintrust.logger import log_exc_info_to_span, start_span


Expand DownExpand Up@@ -415,11 +411,6 @@ def _start_batch_results_span(args, kwargs):
return start_span(name="anthropic.messages.batches.results", type="task", metadata=metadata, input=_input)


def _attachment_filename_for_media_type(media_type: str, block_type: str) -> str:
prefix = "image" if block_type == "image" else "document"
return _attachment_filename_for_mime_type(media_type, prefix=prefix)


def _convert_base64_source_to_attachment(block_type, source):
if not isinstance(source, dict):
return None
Expand All@@ -431,10 +422,10 @@ def _convert_base64_source_to_attachment(block_type, source):
if not isinstance(media_type, str) or not isinstance(data, str):
return None

return _attachment_from_base64_data(
return _materialize_attachment(
data,
media_type,
filename=_attachment_filename_for_media_type(media_type, block_type),
mime_type=media_type,
prefix="image" if block_type == "image" else "document",
)


Expand All@@ -447,11 +438,11 @@ def _process_input_attachments(value):
source = value.get("source")

if block_type in {"image", "document"} and isinstance(source, dict):
attachment = _convert_base64_source_to_attachment(block_type, source)
if attachment is not None:
resolved_attachment = _convert_base64_source_to_attachment(block_type, source)
if resolved_attachment is not None:
processed = {k: _process_input_attachments(v) for k, v in value.items() if k != "source"}
processed["source"] = {k: _process_input_attachments(v) for k, v in source.items() if k != "data"}
processed.update(_image_url_payload(attachment))
processed.update(resolved_attachment.multimodal_part_payload)
return processed

return {k: _process_input_attachments(v) for k, v in value.items()}
Expand Down
49 changes: 46 additions & 3 deletions py/src/braintrust/integrations/google_genai/test_google_genai.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -143,10 +143,16 @@ def _assert_timing_metrics_are_valid(metrics, start=None, end=None):


def _assert_attachment_part(part, *, content_type, filename):
assert "image_url" in part
assert "url" in part["image_url"]
if content_type.startswith("image/"):
assert "image_url" in part
assert "url" in part["image_url"]
attachment = part["image_url"]["url"]
else:
assert "file" in part
assert "file_data" in part["file"]
assert part["file"]["filename"] == filename
attachment = part["file"]["file_data"]

attachment = part["image_url"]["url"]
assert isinstance(attachment, Attachment)
assert attachment.reference["type"] == "braintrust_attachment"
assert attachment.reference["content_type"] == content_type
Expand DownExpand Up@@ -1030,6 +1036,43 @@ class TestModel(BaseModel):
assert copied["context_file"] is attachment


def test_interaction_materialization_only_converts_multimodal_payloads():
"""Interaction helpers should only materialize attachments, not re-serialize values."""
from datetime import datetime
from enum import Enum

from braintrust.integrations.google_genai.tracing import _materialize_interaction_value
from pydantic import BaseModel

class Mode(Enum):
CHAT = "chat"

class InteractionPayload(BaseModel):
created_at: datetime
mode: Mode
media: dict[str, object]

created_at = datetime(2024, 1, 2, 3, 4, 5)
materialized = _materialize_interaction_value(
InteractionPayload(
created_at=created_at,
mode=Mode.CHAT,
media={
"type": "image",
"data": TINY_PNG_BASE64,
"mime_type": "image/png",
"caption": None,
},
)
)

assert materialized["created_at"] == created_at
assert materialized["mode"] is Mode.CHAT
assert materialized["media"]["caption"] is None
assert isinstance(materialized["media"]["data"], Attachment)
assert materialized["media"]["image_url"]["url"] is materialized["media"]["data"]


GROUNDING_MODEL = "gemini-2.0-flash-001"


Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' fix(integrations): unify multimodal attachment materialization by AbhiPrasad · Pull Request #247 · braintrustdata/braintrust-sdk-python · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions .agents/skills/sdk-integrations/SKILL.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,7 @@ Always read:
- `py/src/braintrust/integrations/base.py`
- `py/src/braintrust/integrations/versioning.py`
- `py/src/braintrust/integrations/__init__.py`
- `py/src/braintrust/integrations/utils.py`
- `py/noxfile.py`

Read these when working on an existing integration:
Expand All@@ -43,8 +44,9 @@ Read these when relevant:
- `py/src/braintrust/auto.py` for `auto_instrument()` changes
- `py/src/braintrust/conftest.py` for VCR behavior
- `py/src/braintrust/integrations/auto_test_scripts/` for subprocess auto-instrument coverage
- `py/src/braintrust/integrations/adk/test_adk.py` and `py/src/braintrust/integrations/anthropic/test_anthropic.py` for test layout patterns
- `py/src/braintrust/integrations/adk/tracing.py` and `py/src/braintrust/integrations/google_genai/tracing.py` when handling multimodal content, binary inputs, or generated media
- `py/src/braintrust/integrations/test_utils.py` when touching shared attachment materialization or multimodal payload shaping
- `py/src/braintrust/integrations/adk/test_adk.py`, `py/src/braintrust/integrations/anthropic/test_anthropic.py`, and `py/src/braintrust/integrations/google_genai/test_google_genai.py` for attachment-focused test layout patterns
- `py/src/braintrust/integrations/adk/tracing.py`, `py/src/braintrust/integrations/anthropic/tracing.py`, and `py/src/braintrust/integrations/google_genai/tracing.py` when handling multimodal content, binary inputs, generated media, or attachment materialization behavior

Do not forget `auto.py` and `auto_test_scripts/`. Import-order and subprocess regressions often only show up there.

Expand All@@ -54,8 +56,8 @@ Start from the nearest current integration:

- ADK: direct method patching, `target_module`, `CompositeFunctionWrapperPatcher`, manual `wrap_*()` helpers, context propagation, inline data to `Attachment`
- Agno: multi-target patching, several related patchers, version-conditional fallbacks with `superseded_by`
- Anthropic: compact constructor patching and a small public surface
- Google GenAI: multimodal tracing, generated media, output-side `Attachment` handling
- Anthropic: compact constructor patching, a small public surface, and multimodal request blocks that distinguish image vs document attachment payloads
- Google GenAI: multimodal tracing, generated media, output-side `Attachment` handling, and nested attachment materialization while preserving non-attachment values

Choose the reference based on the hardest part of the task:

Expand DownExpand Up@@ -188,10 +190,16 @@ def _process_result(result: Any, start: float) -> tuple[dict[str, Any], dict[str

Treat binary payloads as attachments, not logged bytes:

- convert raw `bytes` to `braintrust.logger.Attachment`
- prefer the shared `_materialize_attachment(...)` helper in `py/src/braintrust/integrations/utils.py` over provider-local base64 or file-decoding code
- convert provider-owned raw `bytes`, base64 payloads, data URLs, file inputs, and generated media into `braintrust.logger.Attachment` objects when Braintrust should upload the content
- preserve normal remote URLs as strings
- keep useful metadata such as MIME type, size, or provider ids next to the attachment
- follow existing repo content shapes for multimodal payloads
- use the repo's existing multimodal payload shapes after materialization:
- images -> `{"image_url": {"url": attachment}}`
- non-image media/documents/files -> `{"file": {"file_data": attachment, "filename": resolved.filename}}`
- do not force non-image payloads through `image_url` shims
- if attachment materialization fails, keep the original value instead of dropping it or replacing it with `None`
- preserve non-attachment values while walking nested payloads unless you are intentionally normalizing them for readability
- keep useful metadata such as MIME type, size, safety data, filenames, or provider ids next to the attachment

## Patcher Rules

Expand DownExpand Up@@ -251,14 +259,21 @@ Cover the surfaces that changed:
- idempotence
- failure and error logging
- patcher resolution and duplicate detection when relevant
- attachment conversion for binary inputs or generated media
- attachment conversion for binary inputs or generated media, including assertions that images land under `image_url.url`, non-image payloads land under `file.file_data`, and traced payloads contain `Attachment` objects rather than raw bytes or base64 blobs
- span structure, especially `input`, `output`, `metadata`, and `metrics`

For streaming changes, verify both:

- the provider still returns the expected iterator or async iterator
- the final logged span contains the aggregated `output` and stream-specific `metrics`

Also verify, when relevant:

- the `input` contains the expected model/messages/prompt/config fields
- the `output` contains normalized provider results rather than opaque SDK instances
- the `metadata` contains finish reasons, ids, or annotations in the expected place
- binary payloads are represented as `Attachment` objects where applicable, while remote URLs and non-attachment values remain unchanged and unmaterialized file inputs are preserved rather than dropped

Keep VCR cassettes in `py/src/braintrust/integrations/<provider>/cassettes/`. Re-record only when behavior intentionally changes.

When the provider returns binary HTTP responses or generated media, sanitize cassettes as needed so fixtures do not store raw file bytes.
Expand DownExpand Up@@ -296,3 +311,4 @@ Avoid these failures:
- re-recording cassettes when behavior did not intentionally change
- adding a custom `_instrument_*` helper where `_instrument_integration()` already fits
- forgetting `target_module` for deep or optional patch targets
- forcing non-image attachments through `image_url` shims, dropping unrecognized file inputs, or re-serializing non-attachment values while materializing payloads
13 changes: 7 additions & 6 deletions py/src/braintrust/integrations/adk/test_adk.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -314,8 +314,9 @@ async def generate_content_async(self, llm_request: LlmRequest, stream: bool = F
assert len(new_message["parts"]) == 2

document_part = new_message["parts"][0]
assert "image_url" in document_part
attachment = document_part["image_url"]["url"]
assert "file" in document_part
assert document_part["file"]["filename"] == "file.pdf"
attachment = document_part["file"]["file_data"]
assert isinstance(attachment, Attachment)
assert attachment.reference["content_type"] == "application/pdf"
assert attachment.reference["filename"] == "file.pdf"
Expand All@@ -329,8 +330,8 @@ async def generate_content_async(self, llm_request: LlmRequest, stream: bool = F
llm_span = next(row for row in spans if row["span_attributes"]["type"] == "llm")
llm_contents = llm_span["input"]["contents"]
llm_document_part = llm_contents[0]["parts"][0]
assert isinstance(llm_document_part["image_url"]["url"], Attachment)
assert llm_document_part["image_url"]["url"].reference["content_type"] == "application/pdf"
assert isinstance(llm_document_part["file"]["file_data"], Attachment)
assert llm_document_part["file"]["file_data"].reference["content_type"] == "application/pdf"


@pytest.mark.vcr
Expand DownExpand Up@@ -592,7 +593,7 @@ def __init__(self, inline_data=None, text=None):
assert isinstance(attachment, Attachment), "Should be an Attachment object"
assert attachment.reference["type"] == "braintrust_attachment"
assert attachment.reference["content_type"] == "image/png"
assert attachment.reference["filename"] == "file.png"
assert attachment.reference["filename"] == "image.png"
assert "key" in attachment.reference

# Test serializing a Part with text
Expand DownExpand Up@@ -740,7 +741,7 @@ async def test_adk_binary_data_attachment_conversion(memory_logger):
assert "filename" in ref, "Attachment reference should have a filename"
assert "content_type" in ref, "Attachment reference should have a content_type"
assert ref["content_type"] == "image/png", "Content type should be image/png"
assert ref["filename"] == "file.png", "Filename should be file.png"
assert ref["filename"] == "image.png", "Filename should be image.png"

# Second part should be the text
text_part = new_message["parts"][1]
Expand Down
10 changes: 4 additions & 6 deletions py/src/braintrust/integrations/adk/tracing.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,7 +9,7 @@
from typing import Any, cast

from braintrust.bt_json import bt_safe_deep_copy
from braintrust.integrations.utils import _attachment_from_bytes, _image_url_payload
from braintrust.integrations.utils import _materialize_attachment
from braintrust.logger import start_span
from braintrust.span_types import SpanTypeAttribute

Expand DownExpand Up@@ -58,12 +58,10 @@ def _serialize_part(part: Any) -> Any:
data = inline_data.data
mime_type = inline_data.mime_type

# Convert bytes to Attachment
if isinstance(data, bytes):
attachment = _attachment_from_bytes(data, mime_type)

# Return in image_url format - SDK will replace with AttachmentReference
return _image_url_payload(attachment)
resolved_attachment = _materialize_attachment(data, mime_type=mime_type)
if resolved_attachment is not None:
return resolved_attachment.multimodal_part_payload

# Handle Part objects with file_data (file references)
if hasattr(part, "file_data") and part.file_data:
Expand Down
14 changes: 8 additions & 6 deletions py/src/braintrust/integrations/anthropic/test_anthropic.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -124,9 +124,10 @@ def test_get_input_from_kwargs_converts_multimodal_base64_blocks_to_attachments(

assert document_block["type"] == "document"
assert document_block["source"] == {"type": "base64", "media_type": "application/pdf"}
assert isinstance(document_block["image_url"]["url"], Attachment)
assert document_block["image_url"]["url"].reference["content_type"] == "application/pdf"
assert document_block["image_url"]["url"].reference["filename"] == "document.pdf"
assert document_block["file"]["filename"] == "document.pdf"
assert isinstance(document_block["file"]["file_data"], Attachment)
assert document_block["file"]["file_data"].reference["content_type"] == "application/pdf"
assert document_block["file"]["file_data"].reference["filename"] == "document.pdf"

serialized = str(processed_input)
assert PNG_BASE64 not in serialized
Expand DownExpand Up@@ -365,9 +366,10 @@ def test_anthropic_messages_create_with_document_attachment_input(memory_logger)

assert document_block["type"] == "document"
assert document_block["source"] == {"type": "base64", "media_type": "application/pdf"}
assert isinstance(document_block["image_url"]["url"], Attachment)
assert document_block["image_url"]["url"].reference["content_type"] == "application/pdf"
assert document_block["image_url"]["url"].reference["filename"] == "document.pdf"
assert document_block["file"]["filename"] == "document.pdf"
assert isinstance(document_block["file"]["file_data"], Attachment)
assert document_block["file"]["file_data"].reference["content_type"] == "application/pdf"
assert document_block["file"]["file_data"].reference["filename"] == "document.pdf"
assert PDF_BASE64 not in str(span["input"])


Expand Down
23 changes: 7 additions & 16 deletions py/src/braintrust/integrations/anthropic/tracing.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,11 +4,7 @@

from braintrust.bt_json import bt_safe_deep_copy
from braintrust.integrations.anthropic._utils import Wrapper, extract_anthropic_usage
from braintrust.integrations.utils import (
_attachment_filename_for_mime_type,
_attachment_from_base64_data,
_image_url_payload,
)
from braintrust.integrations.utils import _materialize_attachment
from braintrust.logger import log_exc_info_to_span, start_span


Expand DownExpand Up@@ -415,11 +411,6 @@ def _start_batch_results_span(args, kwargs):
return start_span(name="anthropic.messages.batches.results", type="task", metadata=metadata, input=_input)


def _attachment_filename_for_media_type(media_type: str, block_type: str) -> str:
prefix = "image" if block_type == "image" else "document"
return _attachment_filename_for_mime_type(media_type, prefix=prefix)


def _convert_base64_source_to_attachment(block_type, source):
if not isinstance(source, dict):
return None
Expand All@@ -431,10 +422,10 @@ def _convert_base64_source_to_attachment(block_type, source):
if not isinstance(media_type, str) or not isinstance(data, str):
return None

return _attachment_from_base64_data(
return _materialize_attachment(
data,
media_type,
filename=_attachment_filename_for_media_type(media_type, block_type),
mime_type=media_type,
prefix="image" if block_type == "image" else "document",
)


Expand All@@ -447,11 +438,11 @@ def _process_input_attachments(value):
source = value.get("source")

if block_type in {"image", "document"} and isinstance(source, dict):
attachment = _convert_base64_source_to_attachment(block_type, source)
if attachment is not None:
resolved_attachment = _convert_base64_source_to_attachment(block_type, source)
if resolved_attachment is not None:
processed = {k: _process_input_attachments(v) for k, v in value.items() if k != "source"}
processed["source"] = {k: _process_input_attachments(v) for k, v in source.items() if k != "data"}
processed.update(_image_url_payload(attachment))
processed.update(resolved_attachment.multimodal_part_payload)
return processed

return {k: _process_input_attachments(v) for k, v in value.items()}
Expand Down
49 changes: 46 additions & 3 deletions py/src/braintrust/integrations/google_genai/test_google_genai.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -143,10 +143,16 @@ def _assert_timing_metrics_are_valid(metrics, start=None, end=None):


def _assert_attachment_part(part, *, content_type, filename):
assert "image_url" in part
assert "url" in part["image_url"]
if content_type.startswith("image/"):
assert "image_url" in part
assert "url" in part["image_url"]
attachment = part["image_url"]["url"]
else:
assert "file" in part
assert "file_data" in part["file"]
assert part["file"]["filename"] == filename
attachment = part["file"]["file_data"]

attachment = part["image_url"]["url"]
assert isinstance(attachment, Attachment)
assert attachment.reference["type"] == "braintrust_attachment"
assert attachment.reference["content_type"] == content_type
Expand DownExpand Up@@ -1030,6 +1036,43 @@ class TestModel(BaseModel):
assert copied["context_file"] is attachment


def test_interaction_materialization_only_converts_multimodal_payloads():
"""Interaction helpers should only materialize attachments, not re-serialize values."""
from datetime import datetime
from enum import Enum

from braintrust.integrations.google_genai.tracing import _materialize_interaction_value
from pydantic import BaseModel

class Mode(Enum):
CHAT = "chat"

class InteractionPayload(BaseModel):
created_at: datetime
mode: Mode
media: dict[str, object]

created_at = datetime(2024, 1, 2, 3, 4, 5)
materialized = _materialize_interaction_value(
InteractionPayload(
created_at=created_at,
mode=Mode.CHAT,
media={
"type": "image",
"data": TINY_PNG_BASE64,
"mime_type": "image/png",
"caption": None,
},
)
)

assert materialized["created_at"] == created_at
assert materialized["mode"] is Mode.CHAT
assert materialized["media"]["caption"] is None
assert isinstance(materialized["media"]["data"], Attachment)
assert materialized["media"]["image_url"]["url"] is materialized["media"]["data"]


GROUNDING_MODEL = "gemini-2.0-flash-001"


Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' fix(integrations): unify multimodal attachment materialization by AbhiPrasad · Pull Request #247 · braintrustdata/braintrust-sdk-python · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions .agents/skills/sdk-integrations/SKILL.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,7 @@ Always read:
- `py/src/braintrust/integrations/base.py`
- `py/src/braintrust/integrations/versioning.py`
- `py/src/braintrust/integrations/__init__.py`
- `py/src/braintrust/integrations/utils.py`
- `py/noxfile.py`

Read these when working on an existing integration:
Expand All@@ -43,8 +44,9 @@ Read these when relevant:
- `py/src/braintrust/auto.py` for `auto_instrument()` changes
- `py/src/braintrust/conftest.py` for VCR behavior
- `py/src/braintrust/integrations/auto_test_scripts/` for subprocess auto-instrument coverage
- `py/src/braintrust/integrations/adk/test_adk.py` and `py/src/braintrust/integrations/anthropic/test_anthropic.py` for test layout patterns
- `py/src/braintrust/integrations/adk/tracing.py` and `py/src/braintrust/integrations/google_genai/tracing.py` when handling multimodal content, binary inputs, or generated media
- `py/src/braintrust/integrations/test_utils.py` when touching shared attachment materialization or multimodal payload shaping
- `py/src/braintrust/integrations/adk/test_adk.py`, `py/src/braintrust/integrations/anthropic/test_anthropic.py`, and `py/src/braintrust/integrations/google_genai/test_google_genai.py` for attachment-focused test layout patterns
- `py/src/braintrust/integrations/adk/tracing.py`, `py/src/braintrust/integrations/anthropic/tracing.py`, and `py/src/braintrust/integrations/google_genai/tracing.py` when handling multimodal content, binary inputs, generated media, or attachment materialization behavior

Do not forget `auto.py` and `auto_test_scripts/`. Import-order and subprocess regressions often only show up there.

Expand All@@ -54,8 +56,8 @@ Start from the nearest current integration:

- ADK: direct method patching, `target_module`, `CompositeFunctionWrapperPatcher`, manual `wrap_*()` helpers, context propagation, inline data to `Attachment`
- Agno: multi-target patching, several related patchers, version-conditional fallbacks with `superseded_by`
- Anthropic: compact constructor patching and a small public surface
- Google GenAI: multimodal tracing, generated media, output-side `Attachment` handling
- Anthropic: compact constructor patching, a small public surface, and multimodal request blocks that distinguish image vs document attachment payloads
- Google GenAI: multimodal tracing, generated media, output-side `Attachment` handling, and nested attachment materialization while preserving non-attachment values

Choose the reference based on the hardest part of the task:

Expand DownExpand Up@@ -188,10 +190,16 @@ def _process_result(result: Any, start: float) -> tuple[dict[str, Any], dict[str

Treat binary payloads as attachments, not logged bytes:

- convert raw `bytes` to `braintrust.logger.Attachment`
- prefer the shared `_materialize_attachment(...)` helper in `py/src/braintrust/integrations/utils.py` over provider-local base64 or file-decoding code
- convert provider-owned raw `bytes`, base64 payloads, data URLs, file inputs, and generated media into `braintrust.logger.Attachment` objects when Braintrust should upload the content
- preserve normal remote URLs as strings
- keep useful metadata such as MIME type, size, or provider ids next to the attachment
- follow existing repo content shapes for multimodal payloads
- use the repo's existing multimodal payload shapes after materialization:
- images -> `{"image_url": {"url": attachment}}`
- non-image media/documents/files -> `{"file": {"file_data": attachment, "filename": resolved.filename}}`
- do not force non-image payloads through `image_url` shims
- if attachment materialization fails, keep the original value instead of dropping it or replacing it with `None`
- preserve non-attachment values while walking nested payloads unless you are intentionally normalizing them for readability
- keep useful metadata such as MIME type, size, safety data, filenames, or provider ids next to the attachment

## Patcher Rules

Expand DownExpand Up@@ -251,14 +259,21 @@ Cover the surfaces that changed:
- idempotence
- failure and error logging
- patcher resolution and duplicate detection when relevant
- attachment conversion for binary inputs or generated media
- attachment conversion for binary inputs or generated media, including assertions that images land under `image_url.url`, non-image payloads land under `file.file_data`, and traced payloads contain `Attachment` objects rather than raw bytes or base64 blobs
- span structure, especially `input`, `output`, `metadata`, and `metrics`

For streaming changes, verify both:

- the provider still returns the expected iterator or async iterator
- the final logged span contains the aggregated `output` and stream-specific `metrics`

Also verify, when relevant:

- the `input` contains the expected model/messages/prompt/config fields
- the `output` contains normalized provider results rather than opaque SDK instances
- the `metadata` contains finish reasons, ids, or annotations in the expected place
- binary payloads are represented as `Attachment` objects where applicable, while remote URLs and non-attachment values remain unchanged and unmaterialized file inputs are preserved rather than dropped

Keep VCR cassettes in `py/src/braintrust/integrations/<provider>/cassettes/`. Re-record only when behavior intentionally changes.

When the provider returns binary HTTP responses or generated media, sanitize cassettes as needed so fixtures do not store raw file bytes.
Expand DownExpand Up@@ -296,3 +311,4 @@ Avoid these failures:
- re-recording cassettes when behavior did not intentionally change
- adding a custom `_instrument_*` helper where `_instrument_integration()` already fits
- forgetting `target_module` for deep or optional patch targets
- forcing non-image attachments through `image_url` shims, dropping unrecognized file inputs, or re-serializing non-attachment values while materializing payloads
13 changes: 7 additions & 6 deletions py/src/braintrust/integrations/adk/test_adk.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -314,8 +314,9 @@ async def generate_content_async(self, llm_request: LlmRequest, stream: bool = F
assert len(new_message["parts"]) == 2

document_part = new_message["parts"][0]
assert "image_url" in document_part
attachment = document_part["image_url"]["url"]
assert "file" in document_part
assert document_part["file"]["filename"] == "file.pdf"
attachment = document_part["file"]["file_data"]
assert isinstance(attachment, Attachment)
assert attachment.reference["content_type"] == "application/pdf"
assert attachment.reference["filename"] == "file.pdf"
Expand All@@ -329,8 +330,8 @@ async def generate_content_async(self, llm_request: LlmRequest, stream: bool = F
llm_span = next(row for row in spans if row["span_attributes"]["type"] == "llm")
llm_contents = llm_span["input"]["contents"]
llm_document_part = llm_contents[0]["parts"][0]
assert isinstance(llm_document_part["image_url"]["url"], Attachment)
assert llm_document_part["image_url"]["url"].reference["content_type"] == "application/pdf"
assert isinstance(llm_document_part["file"]["file_data"], Attachment)
assert llm_document_part["file"]["file_data"].reference["content_type"] == "application/pdf"


@pytest.mark.vcr
Expand DownExpand Up@@ -592,7 +593,7 @@ def __init__(self, inline_data=None, text=None):
assert isinstance(attachment, Attachment), "Should be an Attachment object"
assert attachment.reference["type"] == "braintrust_attachment"
assert attachment.reference["content_type"] == "image/png"
assert attachment.reference["filename"] == "file.png"
assert attachment.reference["filename"] == "image.png"
assert "key" in attachment.reference

# Test serializing a Part with text
Expand DownExpand Up@@ -740,7 +741,7 @@ async def test_adk_binary_data_attachment_conversion(memory_logger):
assert "filename" in ref, "Attachment reference should have a filename"
assert "content_type" in ref, "Attachment reference should have a content_type"
assert ref["content_type"] == "image/png", "Content type should be image/png"
assert ref["filename"] == "file.png", "Filename should be file.png"
assert ref["filename"] == "image.png", "Filename should be image.png"

# Second part should be the text
text_part = new_message["parts"][1]
Expand Down
10 changes: 4 additions & 6 deletions py/src/braintrust/integrations/adk/tracing.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,7 +9,7 @@
from typing import Any, cast

from braintrust.bt_json import bt_safe_deep_copy
from braintrust.integrations.utils import _attachment_from_bytes, _image_url_payload
from braintrust.integrations.utils import _materialize_attachment
from braintrust.logger import start_span
from braintrust.span_types import SpanTypeAttribute

Expand DownExpand Up@@ -58,12 +58,10 @@ def _serialize_part(part: Any) -> Any:
data = inline_data.data
mime_type = inline_data.mime_type

# Convert bytes to Attachment
if isinstance(data, bytes):
attachment = _attachment_from_bytes(data, mime_type)

# Return in image_url format - SDK will replace with AttachmentReference
return _image_url_payload(attachment)
resolved_attachment = _materialize_attachment(data, mime_type=mime_type)
if resolved_attachment is not None:
return resolved_attachment.multimodal_part_payload

# Handle Part objects with file_data (file references)
if hasattr(part, "file_data") and part.file_data:
Expand Down
14 changes: 8 additions & 6 deletions py/src/braintrust/integrations/anthropic/test_anthropic.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -124,9 +124,10 @@ def test_get_input_from_kwargs_converts_multimodal_base64_blocks_to_attachments(

assert document_block["type"] == "document"
assert document_block["source"] == {"type": "base64", "media_type": "application/pdf"}
assert isinstance(document_block["image_url"]["url"], Attachment)
assert document_block["image_url"]["url"].reference["content_type"] == "application/pdf"
assert document_block["image_url"]["url"].reference["filename"] == "document.pdf"
assert document_block["file"]["filename"] == "document.pdf"
assert isinstance(document_block["file"]["file_data"], Attachment)
assert document_block["file"]["file_data"].reference["content_type"] == "application/pdf"
assert document_block["file"]["file_data"].reference["filename"] == "document.pdf"

serialized = str(processed_input)
assert PNG_BASE64 not in serialized
Expand DownExpand Up@@ -365,9 +366,10 @@ def test_anthropic_messages_create_with_document_attachment_input(memory_logger)

assert document_block["type"] == "document"
assert document_block["source"] == {"type": "base64", "media_type": "application/pdf"}
assert isinstance(document_block["image_url"]["url"], Attachment)
assert document_block["image_url"]["url"].reference["content_type"] == "application/pdf"
assert document_block["image_url"]["url"].reference["filename"] == "document.pdf"
assert document_block["file"]["filename"] == "document.pdf"
assert isinstance(document_block["file"]["file_data"], Attachment)
assert document_block["file"]["file_data"].reference["content_type"] == "application/pdf"
assert document_block["file"]["file_data"].reference["filename"] == "document.pdf"
assert PDF_BASE64 not in str(span["input"])


Expand Down
23 changes: 7 additions & 16 deletions py/src/braintrust/integrations/anthropic/tracing.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,11 +4,7 @@

from braintrust.bt_json import bt_safe_deep_copy
from braintrust.integrations.anthropic._utils import Wrapper, extract_anthropic_usage
from braintrust.integrations.utils import (
_attachment_filename_for_mime_type,
_attachment_from_base64_data,
_image_url_payload,
)
from braintrust.integrations.utils import _materialize_attachment
from braintrust.logger import log_exc_info_to_span, start_span


Expand DownExpand Up@@ -415,11 +411,6 @@ def _start_batch_results_span(args, kwargs):
return start_span(name="anthropic.messages.batches.results", type="task", metadata=metadata, input=_input)


def _attachment_filename_for_media_type(media_type: str, block_type: str) -> str:
prefix = "image" if block_type == "image" else "document"
return _attachment_filename_for_mime_type(media_type, prefix=prefix)


def _convert_base64_source_to_attachment(block_type, source):
if not isinstance(source, dict):
return None
Expand All@@ -431,10 +422,10 @@ def _convert_base64_source_to_attachment(block_type, source):
if not isinstance(media_type, str) or not isinstance(data, str):
return None

return _attachment_from_base64_data(
return _materialize_attachment(
data,
media_type,
filename=_attachment_filename_for_media_type(media_type, block_type),
mime_type=media_type,
prefix="image" if block_type == "image" else "document",
)


Expand All@@ -447,11 +438,11 @@ def _process_input_attachments(value):
source = value.get("source")

if block_type in {"image", "document"} and isinstance(source, dict):
attachment = _convert_base64_source_to_attachment(block_type, source)
if attachment is not None:
resolved_attachment = _convert_base64_source_to_attachment(block_type, source)
if resolved_attachment is not None:
processed = {k: _process_input_attachments(v) for k, v in value.items() if k != "source"}
processed["source"] = {k: _process_input_attachments(v) for k, v in source.items() if k != "data"}
processed.update(_image_url_payload(attachment))
processed.update(resolved_attachment.multimodal_part_payload)
return processed

return {k: _process_input_attachments(v) for k, v in value.items()}
Expand Down
49 changes: 46 additions & 3 deletions py/src/braintrust/integrations/google_genai/test_google_genai.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -143,10 +143,16 @@ def _assert_timing_metrics_are_valid(metrics, start=None, end=None):


def _assert_attachment_part(part, *, content_type, filename):
assert "image_url" in part
assert "url" in part["image_url"]
if content_type.startswith("image/"):
assert "image_url" in part
assert "url" in part["image_url"]
attachment = part["image_url"]["url"]
else:
assert "file" in part
assert "file_data" in part["file"]
assert part["file"]["filename"] == filename
attachment = part["file"]["file_data"]

attachment = part["image_url"]["url"]
assert isinstance(attachment, Attachment)
assert attachment.reference["type"] == "braintrust_attachment"
assert attachment.reference["content_type"] == content_type
Expand DownExpand Up@@ -1030,6 +1036,43 @@ class TestModel(BaseModel):
assert copied["context_file"] is attachment


def test_interaction_materialization_only_converts_multimodal_payloads():
"""Interaction helpers should only materialize attachments, not re-serialize values."""
from datetime import datetime
from enum import Enum

from braintrust.integrations.google_genai.tracing import _materialize_interaction_value
from pydantic import BaseModel

class Mode(Enum):
CHAT = "chat"

class InteractionPayload(BaseModel):
created_at: datetime
mode: Mode
media: dict[str, object]

created_at = datetime(2024, 1, 2, 3, 4, 5)
materialized = _materialize_interaction_value(
InteractionPayload(
created_at=created_at,
mode=Mode.CHAT,
media={
"type": "image",
"data": TINY_PNG_BASE64,
"mime_type": "image/png",
"caption": None,
},
)
)

assert materialized["created_at"] == created_at
assert materialized["mode"] is Mode.CHAT
assert materialized["media"]["caption"] is None
assert isinstance(materialized["media"]["data"], Attachment)
assert materialized["media"]["image_url"]["url"] is materialized["media"]["data"]


GROUNDING_MODEL = "gemini-2.0-flash-001"


Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); fix(integrations): unify multimodal attachment materialization by AbhiPrasad · Pull Request #247 · braintrustdata/braintrust-sdk-python · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions .agents/skills/sdk-integrations/SKILL.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,7 @@ Always read:
- `py/src/braintrust/integrations/base.py`
- `py/src/braintrust/integrations/versioning.py`
- `py/src/braintrust/integrations/__init__.py`
- `py/src/braintrust/integrations/utils.py`
- `py/noxfile.py`

Read these when working on an existing integration:
Expand All@@ -43,8 +44,9 @@ Read these when relevant:
- `py/src/braintrust/auto.py` for `auto_instrument()` changes
- `py/src/braintrust/conftest.py` for VCR behavior
- `py/src/braintrust/integrations/auto_test_scripts/` for subprocess auto-instrument coverage
- `py/src/braintrust/integrations/adk/test_adk.py` and `py/src/braintrust/integrations/anthropic/test_anthropic.py` for test layout patterns
- `py/src/braintrust/integrations/adk/tracing.py` and `py/src/braintrust/integrations/google_genai/tracing.py` when handling multimodal content, binary inputs, or generated media
- `py/src/braintrust/integrations/test_utils.py` when touching shared attachment materialization or multimodal payload shaping
- `py/src/braintrust/integrations/adk/test_adk.py`, `py/src/braintrust/integrations/anthropic/test_anthropic.py`, and `py/src/braintrust/integrations/google_genai/test_google_genai.py` for attachment-focused test layout patterns
- `py/src/braintrust/integrations/adk/tracing.py`, `py/src/braintrust/integrations/anthropic/tracing.py`, and `py/src/braintrust/integrations/google_genai/tracing.py` when handling multimodal content, binary inputs, generated media, or attachment materialization behavior

Do not forget `auto.py` and `auto_test_scripts/`. Import-order and subprocess regressions often only show up there.

Expand All@@ -54,8 +56,8 @@ Start from the nearest current integration:

- ADK: direct method patching, `target_module`, `CompositeFunctionWrapperPatcher`, manual `wrap_*()` helpers, context propagation, inline data to `Attachment`
- Agno: multi-target patching, several related patchers, version-conditional fallbacks with `superseded_by`
- Anthropic: compact constructor patching and a small public surface
- Google GenAI: multimodal tracing, generated media, output-side `Attachment` handling
- Anthropic: compact constructor patching, a small public surface, and multimodal request blocks that distinguish image vs document attachment payloads
- Google GenAI: multimodal tracing, generated media, output-side `Attachment` handling, and nested attachment materialization while preserving non-attachment values

Choose the reference based on the hardest part of the task:

Expand DownExpand Up@@ -188,10 +190,16 @@ def _process_result(result: Any, start: float) -> tuple[dict[str, Any], dict[str

Treat binary payloads as attachments, not logged bytes:

- convert raw `bytes` to `braintrust.logger.Attachment`
- prefer the shared `_materialize_attachment(...)` helper in `py/src/braintrust/integrations/utils.py` over provider-local base64 or file-decoding code
- convert provider-owned raw `bytes`, base64 payloads, data URLs, file inputs, and generated media into `braintrust.logger.Attachment` objects when Braintrust should upload the content
- preserve normal remote URLs as strings
- keep useful metadata such as MIME type, size, or provider ids next to the attachment
- follow existing repo content shapes for multimodal payloads
- use the repo's existing multimodal payload shapes after materialization:
- images -> `{"image_url": {"url": attachment}}`
- non-image media/documents/files -> `{"file": {"file_data": attachment, "filename": resolved.filename}}`
- do not force non-image payloads through `image_url` shims
- if attachment materialization fails, keep the original value instead of dropping it or replacing it with `None`
- preserve non-attachment values while walking nested payloads unless you are intentionally normalizing them for readability
- keep useful metadata such as MIME type, size, safety data, filenames, or provider ids next to the attachment

## Patcher Rules

Expand DownExpand Up@@ -251,14 +259,21 @@ Cover the surfaces that changed:
- idempotence
- failure and error logging
- patcher resolution and duplicate detection when relevant
- attachment conversion for binary inputs or generated media
- attachment conversion for binary inputs or generated media, including assertions that images land under `image_url.url`, non-image payloads land under `file.file_data`, and traced payloads contain `Attachment` objects rather than raw bytes or base64 blobs
- span structure, especially `input`, `output`, `metadata`, and `metrics`

For streaming changes, verify both:

- the provider still returns the expected iterator or async iterator
- the final logged span contains the aggregated `output` and stream-specific `metrics`

Also verify, when relevant:

- the `input` contains the expected model/messages/prompt/config fields
- the `output` contains normalized provider results rather than opaque SDK instances
- the `metadata` contains finish reasons, ids, or annotations in the expected place
- binary payloads are represented as `Attachment` objects where applicable, while remote URLs and non-attachment values remain unchanged and unmaterialized file inputs are preserved rather than dropped

Keep VCR cassettes in `py/src/braintrust/integrations/<provider>/cassettes/`. Re-record only when behavior intentionally changes.

When the provider returns binary HTTP responses or generated media, sanitize cassettes as needed so fixtures do not store raw file bytes.
Expand DownExpand Up@@ -296,3 +311,4 @@ Avoid these failures:
- re-recording cassettes when behavior did not intentionally change
- adding a custom `_instrument_*` helper where `_instrument_integration()` already fits
- forgetting `target_module` for deep or optional patch targets
- forcing non-image attachments through `image_url` shims, dropping unrecognized file inputs, or re-serializing non-attachment values while materializing payloads
13 changes: 7 additions & 6 deletions py/src/braintrust/integrations/adk/test_adk.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -314,8 +314,9 @@ async def generate_content_async(self, llm_request: LlmRequest, stream: bool = F
assert len(new_message["parts"]) == 2

document_part = new_message["parts"][0]
assert "image_url" in document_part
attachment = document_part["image_url"]["url"]
assert "file" in document_part
assert document_part["file"]["filename"] == "file.pdf"
attachment = document_part["file"]["file_data"]
assert isinstance(attachment, Attachment)
assert attachment.reference["content_type"] == "application/pdf"
assert attachment.reference["filename"] == "file.pdf"
Expand All@@ -329,8 +330,8 @@ async def generate_content_async(self, llm_request: LlmRequest, stream: bool = F
llm_span = next(row for row in spans if row["span_attributes"]["type"] == "llm")
llm_contents = llm_span["input"]["contents"]
llm_document_part = llm_contents[0]["parts"][0]
assert isinstance(llm_document_part["image_url"]["url"], Attachment)
assert llm_document_part["image_url"]["url"].reference["content_type"] == "application/pdf"
assert isinstance(llm_document_part["file"]["file_data"], Attachment)
assert llm_document_part["file"]["file_data"].reference["content_type"] == "application/pdf"


@pytest.mark.vcr
Expand DownExpand Up@@ -592,7 +593,7 @@ def __init__(self, inline_data=None, text=None):
assert isinstance(attachment, Attachment), "Should be an Attachment object"
assert attachment.reference["type"] == "braintrust_attachment"
assert attachment.reference["content_type"] == "image/png"
assert attachment.reference["filename"] == "file.png"
assert attachment.reference["filename"] == "image.png"
assert "key" in attachment.reference

# Test serializing a Part with text
Expand DownExpand Up@@ -740,7 +741,7 @@ async def test_adk_binary_data_attachment_conversion(memory_logger):
assert "filename" in ref, "Attachment reference should have a filename"
assert "content_type" in ref, "Attachment reference should have a content_type"
assert ref["content_type"] == "image/png", "Content type should be image/png"
assert ref["filename"] == "file.png", "Filename should be file.png"
assert ref["filename"] == "image.png", "Filename should be image.png"

# Second part should be the text
text_part = new_message["parts"][1]
Expand Down
10 changes: 4 additions & 6 deletions py/src/braintrust/integrations/adk/tracing.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,7 +9,7 @@
from typing import Any, cast

from braintrust.bt_json import bt_safe_deep_copy
from braintrust.integrations.utils import _attachment_from_bytes, _image_url_payload
from braintrust.integrations.utils import _materialize_attachment
from braintrust.logger import start_span
from braintrust.span_types import SpanTypeAttribute

Expand DownExpand Up@@ -58,12 +58,10 @@ def _serialize_part(part: Any) -> Any:
data = inline_data.data
mime_type = inline_data.mime_type

# Convert bytes to Attachment
if isinstance(data, bytes):
attachment = _attachment_from_bytes(data, mime_type)

# Return in image_url format - SDK will replace with AttachmentReference
return _image_url_payload(attachment)
resolved_attachment = _materialize_attachment(data, mime_type=mime_type)
if resolved_attachment is not None:
return resolved_attachment.multimodal_part_payload

# Handle Part objects with file_data (file references)
if hasattr(part, "file_data") and part.file_data:
Expand Down
14 changes: 8 additions & 6 deletions py/src/braintrust/integrations/anthropic/test_anthropic.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -124,9 +124,10 @@ def test_get_input_from_kwargs_converts_multimodal_base64_blocks_to_attachments(

assert document_block["type"] == "document"
assert document_block["source"] == {"type": "base64", "media_type": "application/pdf"}
assert isinstance(document_block["image_url"]["url"], Attachment)
assert document_block["image_url"]["url"].reference["content_type"] == "application/pdf"
assert document_block["image_url"]["url"].reference["filename"] == "document.pdf"
assert document_block["file"]["filename"] == "document.pdf"
assert isinstance(document_block["file"]["file_data"], Attachment)
assert document_block["file"]["file_data"].reference["content_type"] == "application/pdf"
assert document_block["file"]["file_data"].reference["filename"] == "document.pdf"

serialized = str(processed_input)
assert PNG_BASE64 not in serialized
Expand DownExpand Up@@ -365,9 +366,10 @@ def test_anthropic_messages_create_with_document_attachment_input(memory_logger)

assert document_block["type"] == "document"
assert document_block["source"] == {"type": "base64", "media_type": "application/pdf"}
assert isinstance(document_block["image_url"]["url"], Attachment)
assert document_block["image_url"]["url"].reference["content_type"] == "application/pdf"
assert document_block["image_url"]["url"].reference["filename"] == "document.pdf"
assert document_block["file"]["filename"] == "document.pdf"
assert isinstance(document_block["file"]["file_data"], Attachment)
assert document_block["file"]["file_data"].reference["content_type"] == "application/pdf"
assert document_block["file"]["file_data"].reference["filename"] == "document.pdf"
assert PDF_BASE64 not in str(span["input"])


Expand Down
23 changes: 7 additions & 16 deletions py/src/braintrust/integrations/anthropic/tracing.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,11 +4,7 @@

from braintrust.bt_json import bt_safe_deep_copy
from braintrust.integrations.anthropic._utils import Wrapper, extract_anthropic_usage
from braintrust.integrations.utils import (
_attachment_filename_for_mime_type,
_attachment_from_base64_data,
_image_url_payload,
)
from braintrust.integrations.utils import _materialize_attachment
from braintrust.logger import log_exc_info_to_span, start_span


Expand DownExpand Up@@ -415,11 +411,6 @@ def _start_batch_results_span(args, kwargs):
return start_span(name="anthropic.messages.batches.results", type="task", metadata=metadata, input=_input)


def _attachment_filename_for_media_type(media_type: str, block_type: str) -> str:
prefix = "image" if block_type == "image" else "document"
return _attachment_filename_for_mime_type(media_type, prefix=prefix)


def _convert_base64_source_to_attachment(block_type, source):
if not isinstance(source, dict):
return None
Expand All@@ -431,10 +422,10 @@ def _convert_base64_source_to_attachment(block_type, source):
if not isinstance(media_type, str) or not isinstance(data, str):
return None

return _attachment_from_base64_data(
return _materialize_attachment(
data,
media_type,
filename=_attachment_filename_for_media_type(media_type, block_type),
mime_type=media_type,
prefix="image" if block_type == "image" else "document",
)


Expand All@@ -447,11 +438,11 @@ def _process_input_attachments(value):
source = value.get("source")

if block_type in {"image", "document"} and isinstance(source, dict):
attachment = _convert_base64_source_to_attachment(block_type, source)
if attachment is not None:
resolved_attachment = _convert_base64_source_to_attachment(block_type, source)
if resolved_attachment is not None:
processed = {k: _process_input_attachments(v) for k, v in value.items() if k != "source"}
processed["source"] = {k: _process_input_attachments(v) for k, v in source.items() if k != "data"}
processed.update(_image_url_payload(attachment))
processed.update(resolved_attachment.multimodal_part_payload)
return processed

return {k: _process_input_attachments(v) for k, v in value.items()}
Expand Down
49 changes: 46 additions & 3 deletions py/src/braintrust/integrations/google_genai/test_google_genai.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -143,10 +143,16 @@ def _assert_timing_metrics_are_valid(metrics, start=None, end=None):


def _assert_attachment_part(part, *, content_type, filename):
assert "image_url" in part
assert "url" in part["image_url"]
if content_type.startswith("image/"):
assert "image_url" in part
assert "url" in part["image_url"]
attachment = part["image_url"]["url"]
else:
assert "file" in part
assert "file_data" in part["file"]
assert part["file"]["filename"] == filename
attachment = part["file"]["file_data"]

attachment = part["image_url"]["url"]
assert isinstance(attachment, Attachment)
assert attachment.reference["type"] == "braintrust_attachment"
assert attachment.reference["content_type"] == content_type
Expand DownExpand Up@@ -1030,6 +1036,43 @@ class TestModel(BaseModel):
assert copied["context_file"] is attachment


def test_interaction_materialization_only_converts_multimodal_payloads():
"""Interaction helpers should only materialize attachments, not re-serialize values."""
from datetime import datetime
from enum import Enum

from braintrust.integrations.google_genai.tracing import _materialize_interaction_value
from pydantic import BaseModel

class Mode(Enum):
CHAT = "chat"

class InteractionPayload(BaseModel):
created_at: datetime
mode: Mode
media: dict[str, object]

created_at = datetime(2024, 1, 2, 3, 4, 5)
materialized = _materialize_interaction_value(
InteractionPayload(
created_at=created_at,
mode=Mode.CHAT,
media={
"type": "image",
"data": TINY_PNG_BASE64,
"mime_type": "image/png",
"caption": None,
},
)
)

assert materialized["created_at"] == created_at
assert materialized["mode"] is Mode.CHAT
assert materialized["media"]["caption"] is None
assert isinstance(materialized["media"]["data"], Attachment)
assert materialized["media"]["image_url"]["url"] is materialized["media"]["data"]


GROUNDING_MODEL = "gemini-2.0-flash-001"


Expand Down
Loading
Loading