-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtracing.py
More file actions
549 lines (437 loc) · 19.2 KB
/
Copy pathtracing.py
File metadata and controls
549 lines (437 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
"""ADK-specific span creation, metadata extraction, stream handling, and output normalization."""
import asyncio
import contextvars
import inspect
import logging
import time
from contextlib import aclosing, contextmanager
from functools import lru_cache
from itertools import chain
from typing import Any
from braintrust.bt_json import bt_safe_deep_copy
from braintrust.integrations.utils import (
_extract_google_usage_metadata_metrics,
_extract_google_usage_metadata_provider_metadata,
_materialize_attachment,
)
from braintrust.logger import start_span as _bt_start_span
_INSTRUMENTATION = "adk-auto"
def start_span(*args, **kwargs):
internal = dict(kwargs.get("internal") or {})
internal.setdefault("instrumentation", _INSTRUMENTATION)
kwargs["internal"] = internal
return _bt_start_span(*args, **kwargs)
@contextmanager
def _start_stream_span(*args, **kwargs):
"""Keep ADK stream cleanup signals from being logged as span failures."""
cleanup_signal = None
with start_span(*args, **kwargs) as span:
try:
yield span
except (GeneratorExit, asyncio.CancelledError) as exc:
cleanup_signal = exc
if cleanup_signal is not None:
raise cleanup_signal
from braintrust.span_types import SpanTypeAttribute
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Serialization helpers
# ---------------------------------------------------------------------------
def _serialize_content(content: Any) -> Any:
"""Serialize Google ADK Content/Part objects, converting binary data to Attachments."""
if content is None:
return None
# Handle Content objects with parts
if hasattr(content, "parts") and content.parts:
serialized_parts = []
for part in content.parts:
serialized_parts.append(_serialize_part(part))
result = {"parts": serialized_parts}
if hasattr(content, "role"):
result["role"] = content.role
return result
# Handle single Part
return _serialize_part(content)
def _serialize_part(part: Any) -> Any:
"""Serialize a single Part object, handling binary data."""
if part is None:
return None
# If it's already a dict, return as-is
if isinstance(part, dict):
return part
# Handle Part objects with inline_data (binary data like images)
if hasattr(part, "inline_data") and part.inline_data:
inline_data = part.inline_data
if hasattr(inline_data, "data") and hasattr(inline_data, "mime_type"):
data = inline_data.data
mime_type = inline_data.mime_type
if isinstance(data, bytes):
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:
file_data = part.file_data
result = {"file_data": {}}
if hasattr(file_data, "file_uri"):
result["file_data"]["file_uri"] = file_data.file_uri
if hasattr(file_data, "mime_type"):
result["file_data"]["mime_type"] = file_data.mime_type
return result
# Handle text parts
if hasattr(part, "text") and part.text is not None:
result = {"text": part.text}
if hasattr(part, "thought") and part.thought:
result["thought"] = part.thought
return result
# Try standard serialization methods
return bt_safe_deep_copy(part)
@lru_cache(maxsize=128)
def _serialize_pydantic_schema(schema_class: Any) -> dict[str, Any]:
"""
Serialize a Pydantic model class to its full JSON schema.
Returns the complete schema including descriptions, constraints, and nested definitions
so engineers can see exactly what structured output schema was used.
"""
try:
from pydantic import BaseModel
if inspect.isclass(schema_class) and issubclass(schema_class, BaseModel):
# Return the full JSON schema - includes all field info, descriptions, constraints, etc.
return schema_class.model_json_schema()
except (ImportError, AttributeError, TypeError):
pass
# If not a Pydantic model, return class name
return {"__class__": schema_class.__name__ if inspect.isclass(schema_class) else str(type(schema_class).__name__)}
_CAPTURED_CONFIG_FIELDS = (
"system_instruction",
"response_mime_type",
"response_schema",
"response_json_schema",
"input_schema",
"output_schema",
"max_output_tokens",
"temperature",
"top_p",
"top_k",
"stop_sequences",
"candidate_count",
)
def _capture_config(config: Any) -> dict[str, Any] | Any:
"""Capture the ADK config fields that make LLM spans readable."""
if config is None or not config:
return config
captured: dict[str, Any] = {}
for field in _CAPTURED_CONFIG_FIELDS:
value = getattr(config, field, None)
if value is None:
continue
if inspect.isclass(value):
try:
from pydantic import BaseModel
if issubclass(value, BaseModel):
captured[field] = _serialize_pydantic_schema(value)
continue
except (TypeError, ImportError):
pass
captured[field] = value
return captured or config
def _extract_metrics(response: Any) -> dict[str, Any] | None:
"""Extract token usage metrics from Google GenAI response."""
if not response:
return None
usage_metadata = getattr(response, "usage_metadata", None)
if not usage_metadata:
return None
metrics = _extract_google_usage_metadata_metrics(usage_metadata)
return metrics if metrics else None
def _extract_model_name(response: Any, llm_request: Any, instance: Any) -> str | None:
"""Extract model name from Google GenAI response, request, or flow instance."""
# Try to get from response first
if response:
model_version = getattr(response, "model_version", None)
if model_version:
return model_version
# Try to get from llm_request
if llm_request:
if hasattr(llm_request, "model") and llm_request.model:
return str(llm_request.model)
# Try to get from instance (flow's llm)
if instance:
if hasattr(instance, "llm"):
llm = instance.llm
if hasattr(llm, "model") and llm.model:
return str(llm.model)
# Try to get model from instance directly
if hasattr(instance, "model") and instance.model:
return str(instance.model)
return None
def _part_has_field(part: Any, *field_names: str) -> bool:
return any(getattr(part, field_name, None) is not None for field_name in field_names)
def _capture_llm_request_input(llm_request: Any) -> Any:
"""Capture the ADK request fields that make LLM spans readable."""
if llm_request is None:
return None
contents = getattr(llm_request, "contents", None)
config = getattr(llm_request, "config", None)
model = getattr(llm_request, "model", None)
captured: dict[str, Any] = {}
if model:
captured["model"] = model
if contents:
captured["contents"] = (
[_serialize_content(c) for c in contents] if isinstance(contents, list) else _serialize_content(contents)
)
if config:
captured["config"] = _capture_config(config)
if hasattr(llm_request, "live_connect_config"):
captured["live_connect_config"] = getattr(llm_request, "live_connect_config", None)
return captured or llm_request
def _extract_tool_metadata(llm_request: Any) -> dict[str, Any]:
"""Extract tool definitions and tool_config from an ADK LLM request for metadata.tools.
Google-native shape is preserved; ADK is Google-backed and metadata.provider="google"
lets the UI apply the Google normalizer.
"""
if llm_request is None:
return {}
config = getattr(llm_request, "config", None)
if config is None:
return {}
result: dict[str, Any] = {}
tools = getattr(config, "tools", None)
if tools:
result["tools"] = tools
tool_config = getattr(config, "tool_config", None)
if tool_config:
result["tool_config"] = tool_config
return result
def _event_output_with_content(last_event: Any, event_with_content: Any | None) -> Any:
if event_with_content is None or getattr(last_event, "content", None) is not None:
return last_event
content = getattr(event_with_content, "content", None)
if content is None:
return last_event
# Keep the original event instead of recursively serializing it; add the
# captured content alongside it so Braintrust can serialize both values.
return {"event": last_event, "content": content}
def _determine_llm_call_type(llm_request: Any, model_response: Any = None) -> str:
"""
Determine the type of LLM call based on the request and response content.
Returns:
- "tool_selection" if the LLM selected a tool to call in its response
- "response_generation" if the LLM is generating a response after tool execution
- "direct_response" if there are no tools involved or tools available but not used
"""
try:
has_function_response = any(
_part_has_field(part, "function_response", "functionResponse")
for content in (getattr(llm_request, "contents", None) or [])
for part in (getattr(content, "parts", None) or [])
)
response_has_function_call = False
if model_response:
if hasattr(model_response, "get_function_calls"):
try:
function_calls = model_response.get_function_calls()
response_has_function_call = bool(function_calls)
except Exception:
pass
if not response_has_function_call:
content = getattr(model_response, "content", None)
response_has_function_call = any(
_part_has_field(part, "function_call", "functionCall")
for part in chain(
getattr(content, "parts", None) or [], getattr(model_response, "parts", None) or []
)
)
if has_function_response:
return "response_generation"
elif response_has_function_call:
return "tool_selection"
else:
return "direct_response"
except Exception:
return "unknown"
# ---------------------------------------------------------------------------
# Thread-bridge helper (wrapt-style wrapper)
# ---------------------------------------------------------------------------
def _create_thread_wrapper(wrapped: Any, instance: Any, args: Any, kwargs: Any) -> Any:
"""wrapt wrapper for ``create_thread`` that copies context into new threads."""
ctx = contextvars.copy_context()
# ``create_thread(target, ...)`` — target may be positional or keyword.
if args:
target = args[0]
rest_args = args[1:]
else:
target = kwargs.pop("target")
rest_args = args
def _run_in_context(*target_args: Any, **target_kwargs: Any) -> Any:
return ctx.run(target, *target_args, **target_kwargs)
return wrapped(_run_in_context, *rest_args, **kwargs)
# ---------------------------------------------------------------------------
# wrapt wrapper functions (used by patchers)
# ---------------------------------------------------------------------------
async def _agent_run_async_wrapper(wrapped: Any, instance: Any, args: Any, kwargs: Any):
with _start_stream_span(
name=f"agent_run [{instance.name}]",
type=SpanTypeAttribute.TASK,
metadata={"agent_name": instance.name},
) as agent_span:
last_event = None
async with aclosing(wrapped(*args, **kwargs)) as agen:
async for event in agen:
if event.is_final_response():
last_event = event
yield event
if last_event:
agent_span.log(output=last_event)
async def _flow_run_async_wrapper(wrapped: Any, instance: Any, args: Any, kwargs: Any):
async def _trace():
with _start_stream_span(
name="call_llm",
type=SpanTypeAttribute.TASK,
metadata={"flow_class": instance.__class__.__name__},
) as llm_span:
last_event = None
async with aclosing(wrapped(*args, **kwargs)) as agen:
async for event in agen:
last_event = event
yield event
if last_event:
llm_span.log(output=last_event)
async with aclosing(_trace()) as agen:
async for event in agen:
yield event
async def _flow_call_llm_async_wrapper(wrapped: Any, instance: Any, args: Any, kwargs: Any):
llm_request = args[1] if len(args) > 1 else kwargs.get("llm_request")
async def _trace():
# Capture only the fields we need to alter: contents may contain binary
# data that should become Attachments, and config may contain Pydantic
# schema classes that are clearer as JSON schema.
captured_request = _capture_llm_request_input(llm_request)
# Extract model name from request or instance
model_name = _extract_model_name(None, llm_request, instance)
metadata: dict[str, Any] = {
"flow_class": instance.__class__.__name__,
"model": model_name,
"provider": "google",
}
metadata.update(_extract_tool_metadata(llm_request))
# Create span BEFORE execution so child spans (like mcp_tool) have proper parent
# Start with generic name - we'll update it after we see the response
with _start_stream_span(
name="llm_call",
type=SpanTypeAttribute.LLM,
input=captured_request,
metadata=metadata,
) as llm_span:
# Execute the LLM call and yield events while span is active
last_event = None
event_with_content = None
start_time = time.time()
first_token_time = None
async with aclosing(wrapped(*args, **kwargs)) as agen:
async for event in agen:
# Record time to first token
if first_token_time is None:
first_token_time = time.time()
last_event = event
if hasattr(event, "content") and event.content is not None:
event_with_content = event
yield event
# After execution, update span with correct call type and output
if last_event:
# We need to check if we should merge content from an earlier event.
output = _event_output_with_content(last_event, event_with_content)
# Extract metrics from response
metrics = _extract_metrics(last_event)
# Add time to first token if we captured it
if first_token_time is not None:
if metrics is None:
metrics = {}
metrics["time_to_first_token"] = first_token_time - start_time
# Determine the actual call type based on the response
call_type = _determine_llm_call_type(llm_request, last_event)
# Update span name with the specific call type now that we know it
llm_span.set_attributes(
name=f"llm_call [{call_type}]",
span_attributes={"llm_call_type": call_type},
)
# Log output, metrics, and provider-specific usage details.
llm_span.log(
output=output,
metrics=metrics,
metadata=_extract_google_usage_metadata_provider_metadata(
getattr(last_event, "usage_metadata", None)
),
)
async with aclosing(_trace()) as agen:
async for event in agen:
yield event
async def _runner_run_async_wrapper(wrapped: Any, instance: Any, args: Any, kwargs: Any):
user_id = kwargs.get("user_id")
session_id = kwargs.get("session_id")
new_message = kwargs.get("new_message")
state_delta = kwargs.get("state_delta")
# Serialize new_message before any dict conversion to handle binary data
serialized_message = _serialize_content(new_message) if new_message else None
async def _trace():
with _start_stream_span(
name=f"invocation [{instance.app_name}]",
type=SpanTypeAttribute.TASK,
input={"new_message": serialized_message},
metadata={
"app_name": instance.app_name,
"user_id": user_id,
"session_id": session_id,
"state_delta": state_delta,
},
) as runner_span:
last_event = None
async with aclosing(wrapped(*args, **kwargs)) as agen:
async for event in agen:
if event.is_final_response():
last_event = event
yield event
if last_event:
runner_span.log(output=last_event)
async with aclosing(_trace()) as agen:
async for event in agen:
yield event
async def _tool_call_async_wrapper(wrapped: Any, instance: Any, args: Any, kwargs: Any):
tool = args[0] if len(args) > 0 else kwargs.get("tool")
tool_args = args[1] if len(args) > 1 else kwargs.get("args", {})
# MCP tools already have a dedicated wrapper. Skip here to avoid duplicate tool spans.
if tool is not None and getattr(tool.__class__, "__module__", "").startswith("google.adk.tools.mcp_tool"):
return await wrapped(*args, **kwargs)
tool_name = getattr(tool, "name", tool.__class__.__name__ if tool is not None else "unknown")
with start_span(
name=f"tool [{tool_name}]",
type=SpanTypeAttribute.TOOL,
input={"tool_name": tool_name, "arguments": tool_args},
metadata={"tool_class": tool.__class__.__name__ if tool is not None else None},
) as tool_span:
try:
result = await wrapped(*args, **kwargs)
tool_span.log(output=result)
return result
except Exception as e:
tool_span.log(error=e)
raise
async def _mcp_tool_run_async_wrapper_async(wrapped: Any, instance: Any, args: Any, kwargs: Any):
# Extract tool information
tool_name = instance.name
tool_args = kwargs.get("args", {})
with start_span(
name=f"mcp_tool [{tool_name}]",
type=SpanTypeAttribute.TOOL,
input={"tool_name": tool_name, "arguments": tool_args},
metadata={"tool_class": instance.__class__.__name__},
) as tool_span:
try:
result = await wrapped(*args, **kwargs)
tool_span.log(output=result)
return result
except Exception as e:
# Log error to span but re-raise for ADK to handle
tool_span.log(error=e)
raise