adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

OTEL span attributes fail with dict type for llm_request/llm_response

Open democodex opened this issue 11 months ago • 2 comments

Description

When running agents on Vertex AI Agent Engine (Reasoning Engine), OpenTelemetry tracing emits warnings about invalid attribute types for LLM request/response data.

Warning Messages

WARNING: Invalid type dict for attribute 'gcp.vertex.agent.llm_request' value. Expected one of ['bool', 'str', 'bytes', 'int', 'float'] or a sequence of those types

WARNING: Invalid type dict for attribute 'gcp.vertex.agent.llm_response' value. Expected one of ['bool', 'str', 'bytes', 'int', 'float'] or a sequence of those types

Environment

  • ADK Version: google-adk 1.21.0
  • Platform: Vertex AI Agent Engine (Reasoning Engine)
  • Python: 3.12
  • Region: us-central1

Root Cause

OpenTelemetry span attributes only support primitive types (bool, str, bytes, int, float) or sequences of those types. The ADK tracing code is attempting to set gcp.vertex.agent.llm_request and gcp.vertex.agent.llm_response as dict objects, which OTEL rejects.

Expected Behavior

LLM request/response data should either be:

  1. Serialized to JSON string before being set as span attributes
  2. Set as span events instead of attributes (events support structured data)
  3. Logged separately rather than attached to spans

Impact

  • Warnings clutter logs on every LLM call
  • Tracing data for LLM requests/responses may be incomplete or missing in Cloud Trace
  • Not blocking execution but reduces observability quality

Suggested Fix

In the tracing code that sets these attributes, serialize dict values to JSON strings:

import json

# Instead of:
span.set_attribute("gcp.vertex.agent.llm_request", llm_request_dict)

# Use:
span.set_attribute("gcp.vertex.agent.llm_request", json.dumps(llm_request_dict, default=str))

Or use span events for structured data:

span.add_event("llm_request", attributes={"data": json.dumps(llm_request_dict, default=str)})

Reproduction

  1. Deploy an agent to Vertex AI Agent Engine
  2. Send a request that triggers an LLM call
  3. Check Cloud Logging stderr logs for the warning messages

Related Issues

  • #4043 - Similar serialization issue with bytes type for thought_signature

Both issues stem from ADK's telemetry not properly handling non-primitive types when setting OTEL span attributes.

democodex avatar Jan 06 '26 14:01 democodex

@sasha-gitg would this issue be fixed by a similar fix for issue #4043 ?

wuliang229 avatar Jan 07 '26 22:01 wuliang229

This seems to be duplicate of https://github.com/google/adk-python/issues/4094 and should be fixed by https://github.com/google/adk-python/commit/5880109ab13456d5ec511e991de9271fbbec41d8.

jawoszek avatar Jan 15 '26 15:01 jawoszek