Skip to content
Closed
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
45 changes: 45 additions & 0 deletions py/src/braintrust/integrations/google_genai/test_google_genai.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest
from braintrust import logger
from braintrust.integrations.google_genai import setup_genai
from braintrust.integrations.google_genai.tracing import _aggregate_generate_content_chunks
from braintrust.integrations.test_utils import verify_autoinstrument_script
from braintrust.integrations.versioning import detect_module_version, version_satisfies
from braintrust.logger import Attachment
Expand Down Expand Up @@ -1244,6 +1245,50 @@ def test_serialize_content_item_with_content_and_binary_part():
)


@pytest.mark.parametrize("include_url_metadata", [True, False])
def test_stream_preserves_url_context_metadata(include_url_metadata):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generally we prefer tests that don't use mocks/fakes but test directly against the llm call itself, via https://github.com/braintrustdata/braintrust-sdk-python/blob/main/docs/vcr-testing.md. Are you able to record cassettes?

url_metadata = (
types.UrlContextMetadata(
url_metadata=[
types.UrlMetadata(
retrieved_url="https://example.com/article",
url_retrieval_status="URL_RETRIEVAL_STATUS_SUCCESS",
),
types.UrlMetadata(
retrieved_url="https://example.com/unavailable",
url_retrieval_status="URL_RETRIEVAL_STATUS_ERROR",
),
]
)
if include_url_metadata
else None
)
chunks = [
types.GenerateContentResponse(
candidates=[types.Candidate(content=types.Content(parts=[types.Part(text="First ")]))]
),
types.GenerateContentResponse(
candidates=[
types.Candidate(
content=types.Content(parts=[types.Part(text="second")]),
url_context_metadata=url_metadata,
finish_reason="STOP",
)
]
),
]

output, _, _ = _aggregate_generate_content_chunks(chunks, time.time())

assert output["text"] == "First second"
candidate = output["candidates"][0]
assert candidate["content"]["parts"] == [{"text": "First second"}]
if include_url_metadata:
assert candidate["url_context_metadata"] == url_metadata
else:
assert "url_context_metadata" not in candidate


def _assert_grounding_metadata(span_output):
"""Assert that grounding metadata is present and well-structured in span output."""
# The grounding_metadata should be present on the first candidate
Expand Down
2 changes: 2 additions & 0 deletions py/src/braintrust/integrations/google_genai/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,8 @@ def _aggregate_generate_content_chunks(
candidate_dict["safety_ratings"] = candidate.safety_ratings
if hasattr(candidate, "grounding_metadata") and candidate.grounding_metadata:
candidate_dict["grounding_metadata"] = candidate.grounding_metadata
if hasattr(candidate, "url_context_metadata") and candidate.url_context_metadata:
candidate_dict["url_context_metadata"] = candidate.url_context_metadata

candidates.append(candidate_dict)

Expand Down