Skip to content

Commit 03fe909

Browse files
Copybara import of the project:
-- b781880 by Stephen Smith <stephen.smith@newfront.com>: Telemetry unit test for non-serializable data. -- 179da9d by Stephen Smith <stephen.smith@newfront.com>: When converting the llm_request to JSON, skip non-serializable data. -- 5dc68f4 by Stephen Smith <stephen.smith@newfront.com>: Update _create_invocation_context() return type to InvocationContext. -- 23a33f7 by Stephen Smith <stephen.smith@newfront.com>: Oops, remove unnecessary import. COPYBARA_INTEGRATE_REVIEW=#879 from stephensmithnewfront:main f71e195 PiperOrigin-RevId: 763922003
1 parent b794001 commit 03fe909

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

src/google/adk/telemetry.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ def trace_call_llm(
117117
# Consider removing once GenAI SDK provides a way to record this info.
118118
span.set_attribute(
119119
'gcp.vertex.agent.llm_request',
120-
json.dumps(_build_llm_request_for_trace(llm_request)),
120+
json.dumps(
121+
_build_llm_request_for_trace(llm_request),
122+
default=lambda o: '<not serializable>',
123+
),
121124
)
122125
# Consider removing once GenAI SDK provides a way to record this info.
123126
span.set_attribute(

tests/unittests/test_telemetry.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from typing import Any
2+
from typing import Optional
3+
4+
from google.adk.sessions import InMemorySessionService
5+
from google.adk.agents.invocation_context import InvocationContext
6+
from google.adk.agents.llm_agent import LlmAgent
7+
from google.adk.models.llm_request import LlmRequest
8+
from google.adk.models.llm_response import LlmResponse
9+
from google.adk.telemetry import trace_call_llm
10+
from google.genai import types
11+
import pytest
12+
13+
14+
async def _create_invocation_context(
15+
agent: LlmAgent, state: Optional[dict[str, Any]] = None
16+
) -> InvocationContext:
17+
session_service = InMemorySessionService()
18+
session = await session_service.create_session(
19+
app_name='test_app', user_id='test_user', state=state
20+
)
21+
invocation_context = InvocationContext(
22+
invocation_id='test_id',
23+
agent=agent,
24+
session=session,
25+
session_service=session_service,
26+
)
27+
return invocation_context
28+
29+
30+
@pytest.mark.asyncio
31+
async def test_trace_call_llm_function_response_includes_part_from_bytes():
32+
agent = LlmAgent(name='test_agent')
33+
invocation_context = await _create_invocation_context(agent)
34+
llm_request = LlmRequest(
35+
contents=[
36+
types.Content(
37+
role="user",
38+
parts=[
39+
types.Part.from_function_response(
40+
name="test_function_1",
41+
response={
42+
"result": b"test_data",
43+
},
44+
),
45+
],
46+
),
47+
types.Content(
48+
role="user",
49+
parts=[
50+
types.Part.from_function_response(
51+
name="test_function_2",
52+
response={
53+
"result": types.Part.from_bytes(data=b"test_data", mime_type="application/octet-stream"),
54+
},
55+
),
56+
],
57+
),
58+
],
59+
config=types.GenerateContentConfig(system_instruction=""),
60+
)
61+
llm_response = LlmResponse(turn_complete=True)
62+
trace_call_llm(invocation_context, 'test_event_id', llm_request, llm_response)

0 commit comments

Comments
 (0)